获取网上邻居及其资源

来源:互联网 发布:咏春实战 知乎 编辑:程序博客网 时间:2024/06/06 23:15
::OnTest() 
{
CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST);
pListBox->ResetContent();


EnumNet(NULL, 0);
}
::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);
}


::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_LIST);
pListBox->AddString(strText);
}

原创粉丝点击