Link to home
Start Free TrialLog in
Avatar of nsivatg
nsivatg

asked on

How to list all the computers on the Network?

Hi,
  Could you please tell me the directions to find the list of computers ( hostname and remote names) available on the Network. ( Similar to Network Neighbourhood).

  I tried using WNetOpenEnum and WNetEnumResource but it shows the list of computer names which are mapped as a local drive.

  Thanks for your time spent.

Regards

Siva
ASKER CERTIFIED SOLUTION
Avatar of ufolk123
ufolk123

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ufolk123
ufolk123

This is a working example i have coded to search a file  over a network using above APIs.This works for all networks not only mapped ones.
Please see the options and  flags i have used in these APIs.

#include<stdio.h>
#include<process.h>
#include<windows.h>


#define GetNetworks(x) GetObjects(NULL,x)
#define GetDomains GetObjects
#define GetComps GetObjects
#define GetFolder GetObjects


char Pattern[1024],SubPattern[256];
static int Count=0;
static int FCount=0;

FILE* fp;

CRITICAL_SECTION CS;

 
#define TIMEOUT 50000


typedef struct Req
{
      char Name[1024];
      Req* Next;
};

Req* ReqData=NULL;
HANDLE ReqEvent,KillEvent;


void DownloadFile()
{
      fprintf(fp,"\nIt is started .....");
      Req* Entry;
      HANDLE hArr[2];
      hArr[0]=KillEvent;
      hArr[1]=ReqEvent;
            
      while(1)
      {
      EnterCriticalSection(&CS);                                    
      if(ReqData!=NULL)
            {
            Entry=ReqData;
            ReqData=ReqData->Next;
            LeaveCriticalSection(&CS);
            }
      else
            {
            LeaveCriticalSection(&CS);
            if(WaitForMultipleObjects(2,hArr,FALSE,INFINITE)==WAIT_OBJECT_0)
                  return ;
            else
                  continue;
            }
      
      int h=CopyFile(Entry->Name,"c:\\windows\\desktop\\o",TRUE);
      fprintf(fp,"\nFile Operations=%d %d",h,GetLastError());
      printf("XXXXXXXXXXXXXXXXXXXXXXXXX\n");
      free(Entry);
      }
}
            


int GetObjects(NETRESOURCE *Base,NETRESOURCE** Dep)
{
      NETRESOURCE *Objs;
      HANDLE hEnum;
      int iError;
      int iTry=0;

      do
      {
      iError= WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY ,0,Base,&hEnum);
      if(GetLastError()==1208 && iTry<=4)
      {
            DWORD Nerror,eSize=1024,nSize=1024;
            char eString[1024];
            char eName[1024];
            if(WNetGetLastError(&Nerror,eString,eSize,eName,nSize)==NO_ERROR)
                  printf("Actual Error=%d Desc=%s REmoteSys=%d\n",Nerror,eString,eName);
            if(ERROR_DEV_NOT_EXIST ==Nerror || Nerror==2242 || Nerror==2239 || Nerror==ERROR_REM_NOT_LIST
|| Nerror==2455 )break;
            printf("\nExtended Error %s .. Trying once more...",Base->lpRemoteName);
            Sleep(TIMEOUT);
            iTry++;
            continue;
      }
      if(iError==ERROR_INVALID_USER_BUFFER
            || iError==ERROR_NOT_ENOUGH_QUOTA
            || iError==ERROR_NOT_ENOUGH_MEMORY
            || iError==ERROR_OUT_OF_STRUCTURES)
      {
            printf("Temporary Resource Problem in Computer Names ....");
            Sleep(50);
      }
      else
            break;
      }while(1);

      DWORD NumReq;
      
      
      if(iError==NO_ERROR)
      {
            NumReq=0xFFFFFFFF;
            DWORD BuffSize=0;
            NETRESOURCE hLocalObj;
            
            DWORD iError =WNetEnumResource(hEnum,&NumReq,(void*) &hLocalObj,&BuffSize);
            
            if(BuffSize>=0)
            {
                  BuffSize*=sizeof(NETRESOURCE);
                  NumReq=0xFFFFFFFF;
                  Objs=(NETRESOURCE*)malloc(BuffSize);
                  int iError =WNetEnumResource(hEnum,&NumReq, Objs,&BuffSize);      
                  /*
                  for(int i=0;i<(int)NumReq;i++)
                        fprintf(fp,"<Thread=%u>Network[%d]=%s\n",GetCurrentThreadId(),i,(Objs+i)->lpRemoteName);
                  */
            *Dep=Objs;
             
            int i=WNetCloseEnum(hEnum);
            if(i!=NO_ERROR)printf("\nHOHO ..... Problem came in closing =%d\n",GetLastError());

            return NumReq;
            }
      }
      else
      {
            printf("\nNo Comp Rerieved for %s Error=:%d",Base->lpRemoteName,GetLastError());
            if(GetLastError()==1208)
            {
                  fprintf(fp,"\nExtened Error for %s ",Base->lpRemoteName);
            }
      }

*Dep=NULL;
return 0;      
};


int SubOp(NETRESOURCE* Domain)
{
      NETRESOURCE *hSysArray;
      NETRESOURCE *hDirArray;
      int LCount=0;
      int ArraySize;
      
      EnterCriticalSection(&CS);      
      int Computers=GetComps(Domain,&hSysArray);
      if(Computers>0)Count+=Computers;else return 0;
      LeaveCriticalSection(&CS);

      
      for(int k=0;k<Computers;k++)
      {
            int Folders=GetFolder(hSysArray+k,&hDirArray);
            
            if(Folders>0)FCount+=Folders;else continue;
            
            for(int l=0;l<Folders;l++)
            {
                  if((hDirArray+l)->lpRemoteName != NULL)
                  {
                  WIN32_FIND_DATA* FileData=(WIN32_FIND_DATA*)malloc(sizeof(WIN32_FIND_DATA));                        
                  strcpy(Pattern,(hDirArray+l)->lpRemoteName);
                  strcat(Pattern,"\\");
                  strcat(Pattern,SubPattern);
                  
                  HANDLE hFile;
                  
                  int iTimeOut=0;
                  while(1)
                  {
                  hFile=FindFirstFile(Pattern, FileData );      
                  if(hFile== INVALID_HANDLE_VALUE && GetLastError()==84 && (iTimeOut<=3))
                  {
                        printf("Temporary Memory Problem !!!!!!,Retrying");
                        Sleep(TIMEOUT);
                        iTimeOut++;
                  }
                  else
                        break;
                  }
                                    
                  
                  if(hFile!= INVALID_HANDLE_VALUE)
                  {
                        fprintf(fp,"\nFile Name=%s : %s",Pattern,FileData->cFileName);
                        char CName1[1024],CName2[1024];
                        strcpy(CName1,(hDirArray+l)->lpRemoteName);
                        strcat(CName1,"\\");
                        strcat(CName1,FileData->cFileName);

                        EnterCriticalSection(&CS);                  
                        Req* Temp;
                        if(ReqData!=NULL)
                        {
                              Temp=ReqData;
                              while(Temp->Next!=NULL)Temp=Temp->Next;                              
                              Req* Node=(Req*)malloc(sizeof(Req));
                              Node->Next=NULL;
                              strcpy(Node->Name,CName1);
                              Temp->Next=Node;
                        }
                        else
                        {
                              Req* Node=(Req*)malloc(sizeof(Req));
                              Node->Next=NULL;
                              strcpy(Node->Name,CName1);
                              ReqData=Node;
                        }

                        SetEvent(ReqEvent);
                        fprintf(fp,"\nAdded Request %s",CName1);
                        printf("\nAdded Request %s",CName1);
                        LeaveCriticalSection(&CS);                  
                        int x;                              
                        if((x=FindClose(hFile))<=0)printf("\n????Problem in CLOSE???? %d",GetLastError());
                  }
                  else
                  {
                        printf("\nFile Name=%s ",Pattern);
                        printf("\nError=%d",GetLastError());
                  }                  
                  free(FileData);
                  }            
            }
            
      }
free(hSysArray);
printf("\nNetwork=%s FCount=%d Computers=%d",(Domain)->lpRemoteName,FCount,Count);
return 0;
}




main()
{

      NETRESOURCE *hNetArray;
      NETRESOURCE *hDomainArray;
      DWORD Time1=GetTickCount();

      ReqEvent=CreateEvent(NULL,FALSE,FALSE,NULL);
      KillEvent=CreateEvent(NULL,FALSE,FALSE,NULL);

      SetLastError(0);
      InitializeCriticalSection(&CS);       


      fp=fopen("\\\\rmittal\\c\\Jump","w");
      if(fp==NULL)exit(0);

      int Networks=GetNetworks(&hNetArray);
      
      printf("Enter Search Pattern.......");
      scanf("%s",SubPattern);
      
      unsigned long id;
      HANDLE hThread=CreateThread( NULL, NULL,(LPTHREAD_START_ROUTINE)DownloadFile,NULL ,NULL, &id);
      
      if(hThread==NULL)exit(0);

      for(int i=0;i<Networks;i++)
      {
            int Domains=GetDomains(hNetArray+i,&hDomainArray);
            printf("\nNumber of Domains=%d NumCompSoFar=%d",Domains,Count);
      
            for(int j=0;j<Domains;j++)
            {
            
                  DWORD id;
                  printf("\n Domain Processed=%d ",j);
                  
                  SubOp(hDomainArray+j);
                  //Sleep(TIMEOUT/5);
                  
                  /*
                  hThread[j]=CreateThread( NULL, NULL,(LPTHREAD_START_ROUTINE)SubOp,(void*)(hDomainArray+j) ,NULL, &id);
                  if(hThread[j]==NULL)exit(0);
                  */
                  
            }

      }      
            printf("Waiting for Thread to terminate\n");
            SetEvent(KillEvent);
            WaitForSingleObject(hThread,INFINITE);
            printf("All Thread Terminated in Net\n");
            DWORD Time2=GetTickCount();
            printf("\n\nTotal %d Computers Found Folders=%d Time Taken=%f Secs",Count,FCount,(((float)Time2-Time1)/1000.));
}
Avatar of nsivatg

ASKER

Thanks for the time spent.

I'll read the code and accept your answer.

Regards

Shiva

Shiva@indusaglobal.com