📄 neighbordlg.cpp
字号:
// NetErrorHandler(hwnd, dwResultEnum, (LPSTR)"WNetEnumResource");
break;
}
}
//
// End do.
//
while(dwResultEnum != ERROR_NO_MORE_ITEMS);
//
// Call the GlobalFree function to free the memory.
//
GlobalFree((HGLOBAL)lpnrLocal);
//
// Call WNetCloseEnum to end the enumeration.
//
dwResult = WNetCloseEnum(hEnum);
if(dwResult != NO_ERROR)
{
//
// Process errors.
//
// NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetCloseEnum");
return FALSE;
}
return TRUE;
}
//映射网络驱动器
void CNeighborDlg::Redirect(const char *LocalName,const char *RemoteName,const char * UserName,const char *Password)
{
NETRESOURCE nr;
DWORD res;
char szUserName[32],
szPassword[32],
szLocalName[32],
szRemoteName[MAX_PATH];
strcpy(szUserName,UserName);
strcpy(szPassword,Password);
strcpy(szLocalName,LocalName);
strcpy(szRemoteName,RemoteName);
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = szLocalName;
nr.lpRemoteName = szRemoteName;
nr.lpProvider = NULL;
//
res = WNetAddConnection2(&nr, szPassword, szUserName, FALSE);
//
switch(res)
{
case NO_ERROR:
AfxMessageBox("网络驱动器映射成功");
break;
case ERROR_BAD_PROFILE:
AfxMessageBox("ERROR_BAD_PROFILE");
break;
case ERROR_CANNOT_OPEN_PROFILE:
AfxMessageBox("ERROR_CANNOT_OPEN_PROFILE");
break;
case ERROR_DEVICE_IN_USE:
AfxMessageBox("ERROR_DEVICE_IN_USE");
break;
case ERROR_EXTENDED_ERROR:
AfxMessageBox("ERROR_EXTENDED_ERROR");
break;
case ERROR_NOT_CONNECTED:
AfxMessageBox("ERROR_NOT_CONNECTED");
break;
case ERROR_OPEN_FILES:
AfxMessageBox("ERROR_OPEN_FILES");
break;
default:
AfxMessageBox("未知错误,可能需要帐号和密码认证,或者该主机或文件不存在");
break;
}
return;
}
//断开网络驱动器映射
void CNeighborDlg::DisConnectDirect(const char * localname)
{
// CHAR szDeviceName[80];
// DWORD cchBuff = sizeof(szDeviceName);
/*
dwResult = WNetGetConnection(localname,
(LPSTR) szDeviceName,
&cchBuff);
*/
DWORD dwResult;
dwResult = WNetCancelConnection2(localname,
CONNECT_UPDATE_PROFILE, // remove connection from profile
FALSE); // fail if open files or jobs
switch(dwResult)
{
case NO_ERROR:
AfxMessageBox("成功断开网络驱动器映射");
break;
case ERROR_BAD_PROFILE:
AfxMessageBox("ERROR_BAD_PROFILE");
break;
case ERROR_CANNOT_OPEN_PROFILE:
AfxMessageBox("ERROR_CANNOT_OPEN_PROFILE");
break;
case ERROR_DEVICE_IN_USE:
AfxMessageBox("ERROR_DEVICE_IN_USE");
break;
case ERROR_EXTENDED_ERROR:
AfxMessageBox("ERROR_EXTENDED_ERROR");
break;
case ERROR_NOT_CONNECTED:
AfxMessageBox("ERROR_NOT_CONNECTED");
break;
case ERROR_OPEN_FILES:
AfxMessageBox("ERROR_OPEN_FILES");
break;
default:
AfxMessageBox("未知错误");
break;
}
}
//获得本地计算机名称
int CNeighborDlg::GetLocalHostName(CString &sHostName)
{
char szHostName[256];
int nRetCode;
nRetCode=gethostname(szHostName,sizeof(szHostName));
if(nRetCode!=0)
{
//产生错误
sHostName=_T("没有取得");
return GetLastError();
}
sHostName=szHostName;
return 0;
}
int CNeighborDlg::GetIpAddress(const CString &sHostName, CStringArray &sIpAddress)//获得本地IP
{
struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
sIpAddress.RemoveAll();
if(lpHostEnt==NULL)
{
//产生错误
return GetLastError();
}
//获取IP
int i=0;
LPSTR lpAddr=lpHostEnt->h_addr_list[i];
CString temp;
while(lpAddr)
{
i++;
struct in_addr inAddr;
memmove(&inAddr,lpAddr,4);
//转换为标准格式
temp=inet_ntoa(inAddr);
if(temp.IsEmpty())
{
break;
}
sIpAddress.Add(temp);
lpAddr=lpHostEnt->h_addr_list[i];
}
return 0;
}
int CNeighborDlg::GetIpAddress(const CString &sHostName, BYTE f0[],BYTE f1[],BYTE f2[],BYTE f3[],int &count)//获得本地IP
{
struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
if(lpHostEnt==NULL)
{
//产生错误
f0[0]=f1[0]=f2[0]=f3[0]=0;
return GetLastError();
}
//获取IP
count=0;
LPSTR lpAddr=lpHostEnt->h_addr_list[count];
while(lpAddr)
{
struct in_addr inAddr;
memmove(&inAddr,lpAddr,4);
f0[count]=inAddr.S_un.S_un_b.s_b1;
f1[count]=inAddr.S_un.S_un_b.s_b2;
f2[count]=inAddr.S_un.S_un_b.s_b3;
f3[count]=inAddr.S_un.S_un_b.s_b4;
count++;
lpAddr=lpHostEnt->h_addr_list[count];
}
return 0;
}
//由主机名称得到IP地址
void CNeighborDlg::OnIpbutton()
{
// TODO: Add your control notification handler code here
CStringArray ip;
CHostNameDlg dlg;
dlg.DoModal();
GetIpAddress(dlg.m_sHostName,ip);//获得本地IP
CString temp;
int i=0;
while(i<ip.GetSize())
{
temp+=ip[i];
temp+=";\n";
i++;
}
AfxMessageBox(dlg.m_sHostName+"的IP是\n"+temp);
}
//网上邻居的浏览:这是利用CFileDlg的情况
void CNeighborDlg::OnNavigator()
{
// TODO: Add your control notification handler code here
CFileDialog filedlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,"All Files (*.*)|*.*||",this);
TRACE("%d",filedlg.m_ofn.nMaxFile);
TRACE("%d",sizeof(filedlg.m_ofn.lpstrFile));
//扩大多选文件名的缓冲区,这是为OFN_ALLOWMULTISELECT服务的
filedlg.m_ofn.lpstrTitle="浏览网上邻居";
filedlg.m_ofn.nMaxFile=1024;
filedlg.m_ofn.lpstrFile=new TCHAR[1024];
filedlg.m_ofn.lpstrFile[0]=NULL;
char path[MAX_PATH];
(SHGetSpecialFolderPath(this->GetSafeHwnd(),path,CSIDL_NETWORK,FALSE));
filedlg.m_ofn.lpstrInitialDir=path;
//如何让这个窗口定位在网络邻居上???
filedlg.DoModal();
POSITION pos=filedlg.GetStartPosition();
CListBox *listbox=(CListBox*)GetDlgItem(IDC_LIST);
listbox->ResetContent();
CString pathname;
while(pos!=NULL)
{
pathname=filedlg.GetNextPathName(pos);
listbox->AddString(pathname);
}
//得到了所选的文件后,可以调用其它API函数进行拷贝等操作
delete filedlg.m_ofn.lpstrFile;
}
void CNeighborDlg::OnRedirect()
{
// TODO: Add your control notification handler code here
CDirectDriver direct;
if(direct.DoModal()==IDOK)
{
Redirect(LPCSTR(direct.m_localname),LPCSTR(direct.m_remotename),LPCSTR(direct.m_username),LPCSTR(direct.m_password));
}
}
void CNeighborDlg::OnDisconnectdirect()
{
// TODO: Add your control notification handler code here
CLocalNameDlg dlg;
if(dlg.DoModal()==IDOK)
{
DisConnectDirect(LPCSTR(dlg.m_sLocalName));
}
}
void CNeighborDlg::OnIptohostname()
{
// TODO: Add your control notification handler code here
CString ip,name;
CHostNameDlg dlg(FALSE);
dlg.DoModal();
unsigned long a;
ip=dlg.m_sHostName;
a=inet_addr(ip);
struct hostent FAR *host=gethostbyaddr((char *)(&a),4,AF_INET);
if(host==NULL)
{
AfxMessageBox("错误的IP地址");
return;
}
int b=GetLastError();
ip=host->h_name;
AfxMessageBox(dlg.m_sHostName+"的主机名是"+ip);
}
void CNeighborDlg::OnNetsend()
{
CString strComputer,strMessage;
//先检测传送内容
GetDlgItemText(IDC_MESSAGE,strMessage);
if(strMessage.IsEmpty())
{
AfxMessageBox("传送的内容不能为空!");
return;
}
//收集发送的目的地
GetDlgItemText(IDC_SEND_ADDRESS,strComputer);
if(strComputer.IsEmpty())
{
CListBox *list=(CListBox*)GetDlgItem(IDC_LIST);
if(list->GetCurSel()<0)
{
AfxMessageBox("请在发送地址框中输入发送目的地!\r\n或者请从左边的列表中选择相应的机器,如果该列表为空,则点击\"列取网上邻居\"按钮");
return;
}
list->GetText(list->GetCurSel(),strComputer);
strComputer.TrimLeft(" ");
if(strComputer.IsEmpty())
{
AfxMessageBox("请在发送地址框中输入发送目的地!\r\n或者请从左边的列表中选择相应的机器,如果该列表为空,则点击\"列取网上邻居\"按钮");
return;
}
}
strComputer.TrimLeft(" ");
strComputer.TrimLeft("\\");
strComputer.TrimRight(" ");
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
ZeroMemory( &pi, sizeof(pi) );
CString strCommand=
CString("net send ")+strComputer+" "+strMessage;
BeginWaitCursor();
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
(LPSTR)LPCSTR(strCommand), // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
AfxMessageBox( "创建发送进程失败." );
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
EndWaitCursor();
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -