Google
 
Web ppczone.net

View Full Version : WNET API to find all PC's in network


Veena
03-05-2007, 11:26 PM
Hello All,
I have to find all the PC's connected to the network either while i am
connected through ActiveSync or through WLAN. I tried using the WNET
API for the same. The code i used is this:

BOOL EnumeratePCs()
{
HANDLE hEnum;
DWORD dwResultEnum=NULL;
DWORD dwIndex,dwResult;

NETRESOURCE nr;

nr.dwScope = RESOURCE_GLOBALNET;
nr.dwType = RESOURCETYPE_DISK;
nr.dwUsage = RESOURCEUSAGE_CONTAINER;
nr.lpLocalName = TEXT("");
nr.lpRemoteName = TEXT("");
nr.lpComment = TEXT("");
nr.lpProvider = TEXT("Microsoft Windows Network");



dwResult = WNetOpenEnum(
RESOURCE_GLOBALNET, // All resources on the
network.
RESOURCETYPE_DISK, // All resources.
0, // Enumerate all resources.
&nr, // The container to enumerate.
&hEnum); // Handle to resource.

if (dwResult != ERROR_SUCCESS)
{
MessageBox(NULL, TEXT("Info"), TEXT("WNetOpenEnum failed"), MB_OK);
return FALSE;
}
else
MessageBox(NULL, TEXT("Info"), TEXT("WNetOpenEnum success"),
MB_OK);

DWORD cbBuffer = 16384; // 16K is a good size
DWORD cEntries = -1; // enumerate all possible entries
LPNETRESOURCE lpnrLocal; // pointer to enumerated structures

lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
if (lpnrLocal == NULL)
return FALSE;

// do
//{
ZeroMemory(lpnrLocal, cbBuffer);
dwResultEnum = WNetEnumResource(hEnum, // resource handle
&cEntries, // defined locally as
-1
lpnrLocal, // LPNETRESOURCE
&cbBuffer); // buffer size
//
// If the call succeeds, loop through the structures.
//
if (dwResultEnum == NO_ERROR)
MessageBox(NULL, TEXT("Info"), TEXT("WNetEnumResource Success"),
MB_OK);
else
{
MessageBox(NULL, TEXT("Info"), TEXT("WNetEnumResource failed"),
MB_OK);
/* for(int i = 0; i < cEntries; i++)
{
WriteToFile(&lpnrLocal[i]);
}*/
}
//}
// while(dwResultEnum != ERROR_NO_MORE_ITEMS);

GlobalFree((HGLOBAL)lpnrLocal);
dwResult = WNetCloseEnum(hEnum);
//return TRUE;
}

This code works fine on Desktop application. But when executed for
PocketPC's it does not work. Any idea why this is happening? what am i
doing wrong.
Any help will be greatly appreciated.
Thanks.