最新消息:

WNET获取共享信息

C/C++ admin 2655浏览 0评论

char RemoteName[] = “\\192.168.105.9″;
DWORD ret;
NETRESOURCE nr;
memset(&nr, 0, sizeof(nr));
nr.dwScope = RESOURCE_CONNECTED;
nr.dwType = RESOURCETYPE_ANY;
nr.dwDisplayType = RESOURCEDISPLAYTYPE_GENERIC;
nr.dwUsage = RESOURCEUSAGE_CONNECTABLE;
nr.lpRemoteName = RemoteName;
ret = WNetAddConnection2(&nr, NULL, NULL, CONNECT_UPDATE_PROFILE);

if(ret != NO_ERROR)
{
MessageBox(“失败!”);
return;
}
HANDLE hEnum;
ret = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, &nr, &hEnum);
if(ret != NO_ERROR)
{
MessageBox(“失败!”);
return;
}

DWORD dwResultEnum;
BYTE buf[16384];
DWORD cbBuffer = 16384; // 16K is a good size
LPNETRESOURCE lpnrLocal = (LPNETRESOURCE)buf;
DWORD cEntries = -1;

do
{
cbBuffer = 16384;
memset(lpnrLocal, 0, 16384);
dwResultEnum = WNetEnumResource(hEnum, // resource handle
&cEntries, // defined locally as -1
lpnrLocal, // LPNETRESOURCE
&cbBuffer); // buffer size
if (dwResultEnum == NO_ERROR)
{
for (DWORD i=0; i<cEntries; i++)
{
//CString str_remote = lpnrLocal[i].lpRemoteName;//得到共享名称
if(lpnrLocal[i].dwType == RESOURCETYPE_PRINT)
MessageBox(lpnrLocal[i].lpRemoteName);
}
}
else if (dwResultEnum != ERROR_NO_MORE_ITEMS)
{
break;
}
}
while (dwResultEnum != ERROR_NO_MORE_ITEMS);
WNetCloseEnum(hEnum);

转载请注明:爱开源 » WNET获取共享信息

您必须 登录 才能发表评论!