这里使用了三个函数
- #include "IPHlpApi.h"
- #pragma comment(lib,"IPHLPAPI.LIB")
- #pragma comment(lib,"netapi32.lib ")
- #pragma comment(lib,"Mpr.lib")
- void CGkbc8Dlg::OnButton2()
- {
- CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST1);
- pListBox->ResetContent();
- EnumNet(NULL, 0);
- }
- void CGkbc8Dlg::EnumNet(NETRESOURCE* pResource, int nItem)
- {
- HANDLE hEnum = NULL;
- DWORD dwResult = 0;
- //打开网上资源的枚举
- dwResult = WNetOpenEnum(
- RESOURCE_GLOBALNET,
- RESOURCETYPE_ANY,
- 0,
- pResource,
- &hEnum);
-
- if (dwResult != NO_ERROR)
- {
- return;
- }
- DWORD nCount = -1;
- DWORD nSize = 16384;
- pResource = (NETRESOURCE*)new BYTE[nSize];
- do
- {
- ZeroMemory(pResource, nSize);
- //枚举网上资源
- dwResult = WNetEnumResource(
- hEnum,
- &nCount,
- pResource,
- &nSize);
- if (dwResult == NO_ERROR)
- {
- for (UINT n = 0 ; n < nCount; n++)
- {
- ShowNet(&pResource[n], nItem);
-
- //如果网络资源是容器资源则递归调用枚举函数
- if ((pResource[n].dwUsage & RESOURCEUSAGE_CONTAINER)
- == RESOURCEUSAGE_CONTAINER)
- {
- EnumNet(&pResource[n], nItem + 1);
- }
- }
- }
- else if(dwResult != ERROR_NO_MORE_ITEMS)
- {
- break;
- }
- } while(dwResult != ERROR_NO_MORE_ITEMS);
- delete[] pResource;
- //关闭网上资源的枚举
- WNetCloseEnum(hEnum);
- }
- void CGkbc8Dlg::ShowNet(NETRESOURCE* pResource, int nItem)
- {
- CString strText = _T("");
- strText += _T("");
- strText += pResource->lpRemoteName;
- strText += _T("");
- strText += pResource->lpLocalName;
- strText += _T("");
- strText += pResource->lpComment;
- strText += _T("");
- for (int n = 0; n < nItem; n++)
- {
- strText = _T(" ") + strText;
- }
-
- CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST1);
- pListBox->AddString(strText);
- }
复制代码 OnButton2() 为按钮的点击函数,在函数内 依次调用了两个函数EnumNet,ShowNet。
IDC_LIST1为我们添加的列表框控件。
上位机MFC如何获得网上邻居
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|