NowCan及各位对系统熟系的朋友请来帮忙。
以下代码是从NowCan的个人主页上copy下来并改的,功能是查找网络上的所有计算机。能在win98,win2000上执行,到winnt 4.0(sp6)就执行不了,头晕中。
代码:
--------------------------------------------------------------------
bool TfrmMain::Enumerate(LPNETRESOURCE lpNetRC_p)
{
//查找网上邻居的所有计算机
HANDLE hEnum = 0;
DWORD dwScope = RESOURCE_GLOBALNET ;
DWORD dwType = RESOURCETYPE_ANY ;
DWORD dwResult = WNetOpenEnum(
dwScope, // scope of enumeration
dwType, // resource types to list
0, // enumerate all resources
lpNetRC_p, // pointer to resource structure (NULL at first time)
&hEnum // handle to resource
);
if (dwResult != NO_ERROR)
return false;
DWORD dwBuffer = 16384 ; // 16K is reasonable size
DWORD dwEntries = 0xFFFFFFFF ; // enumerate all possible entries
LPNETRESOURCE lpnrLocal = 0;
bool bRet = true;
AnsiString sComputerName;
try
{
do
{
// first allocate buffer for NETRESOURCE structures ...
lpnrLocal = (LPNETRESOURCE) GlobalAlloc( GPTR, dwBuffer ) ;
dwResult = WNetEnumResource(
hEnum, // resource-handle
&dwEntries,
lpnrLocal,
&dwBuffer
);
if (dwResult == NO_ERROR)
{
for (DWORD i=0; i<dwEntries; i++)
{
if (RESOURCEUSAGE_CONTAINER ==
(lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONTAINER) &&
lpnrLocal[i].dwDisplayType != RESOURCEDISPLAYTYPE_SERVER)
{
if (!Enumerate(&lpnrLocal[i])) {
throw Exception("在网上查找计算机失败!");
}
}else{
sComputerName = lpnrLocal[i].lpRemoteName; //计算机名
sComputerName.Delete(1, 2);
// AddListviewItem(sComputerName, lpnrLocal[i].lpComment);
}
}
}
} while (dwResult != ERROR_NO_MORE_ITEMS);
}catch(...){
bRet = false;
}
if (lpnrLocal)
GlobalFree((HGLOBAL)lpnrLocal) ;
WNetCloseEnum(hEnum) ;
return bRet;
}