怎样获得指定IP所对应的工作站、组
这个只能得到局域网中的所有共享资源
HANDLE hEnum;
//NetResource 为 NULL 的话表明要遍历所有的资源
if(WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,
RESOURCEUSAGE_CONTAINER|RESOURCEUSAGE_CONNECTABLE
,nr,&hEnum)!=NO_ERROR)
{
// 出错表明拒绝访问
return;
//如果不想继续遍历, 可以扔出一个意外
//RaiseLastWin32Error();
}
//遍历所有的资源
DWORD Count=-1;
DWORD BufferSize=0;
DWORD ReturnVal=0;
//为 NetResource 数组分配空间
NETRESOURCE *NetRes=(NETRESOURCE*)new char[1024];
ZeroMemory(NetRes,sizeof(NETRESOURCE));
for(;;)
{
ReturnVal=WNetEnumResource(hEnum,&Count,NetRes,&BufferSize);
if(ReturnVal==ERROR_MORE_DATA) //由我们设置 NetRes 的大小
{
//但如果数据量超出了我们设置的大小
Count=-1;
delete[] NetRes;
NetRes=(NETRESOURCE*)new char[BufferSize];
ZeroMemory(NetRes,sizeof(NETRESOURCE));
ReturnVal=WNetEnumResource(hEnum,&Count,NetRes,&BufferSize);
}
if(ReturnVal!=NO_ERROR)break;
// 加到 ListBox
for (unsigned int i=0;i<Count;i++)
{
//ListBox1->Items->Add(NetRes[i].lpRemoteName);
afxDump << "Share resuro" << NetRes[i].lpRemoteName << "\n";
//如果不希望程序完全挂起的话, 可以加上
//Application->ProcessMessages();
if((NetRes[i].dwUsage & RESOURCEUSAGE_CONTAINER)==RESOURCEUSAGE_CONTAINER)
EnumNetRes(&NetRes[i]);
}
if(ReturnVal==ERROR_NO_MORE_ITEMS)
{
//遍历完成
delete[] NetRes;
WNetCloseEnum(hEnum);
//RaiseLastWin32Error();
}
}
delete[] NetRes;
WNetCloseEnum(hEnum);
你用NetGetJoinInformation试试
gz
BOOL Enumerate()
{
//get this machines host name
char szHostname[256];
WSADATA wsaData;
WORD wVersionRequested;
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if (gethostname(szHostname, sizeof(szHostname)))
{
TRACE(_T("Failed in call to gethostname, WSAGetLastError returns %d\n"), WSAGetLastError());
return FALSE;
}
//get host information from the host name
HOSTENT* pHostEnt = gethostbyname(szHostname);
if (pHostEnt == NULL)
{
TRACE(_T("Failed in call to gethostbyname, WSAGetLastError returns %d\n"), WSAGetLastError());
return FALSE;
}
//check the length of the IP adress
if (pHostEnt->h_length != 4)
{
TRACE(_T("IP address returned is not 32 bits !!\n"));
return FALSE;
}
//call the virtual callback function in a loop
int nAdapter = 0;
BOOL bContinue = TRUE;
while (pHostEnt->h_addr_list[nAdapter] && bContinue)
{
in_addr address;
CopyMemory(&address.S_un.S_addr, pHostEnt->h_addr_list[nAdapter], pHostEnt->h_length);
bContinue = EnumCallbackFunction(nAdapter, address);
nAdapter++;
}
WSACleanup( );
return TRUE;
}
}
枚举组内的所以计算机名 你可以改成IP显示