?
Solved

How to list all the computers on the Network?

Posted on 2000-02-08
3
Medium Priority
?
1,029 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:nsivatg
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
ufolk123 earned 40 total points
ID: 2502749
Hi nstivatq,

Please see the WNetOpenEnum() and WNetEnumResource().

WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY ,0,Base,&hEnum);


Will give the handle to a resource and use of

WNetEnumResource(hEnum,&NumReq,(void*) &hLocalObj,&BuffSize);

on hEnum will give you list of all resources on that level in hLocalObj array.

So
step1: Pass Base as NULL to WNetOpenEnum() and gret hEnum.
Passing this to WNetEnumResource() will give you list of all Networks.

Again use each of  network name in hLocalobj as base to WNetOpenEnum()
and passing the handle hEnum to WNetEnumResource() you will go down one hiererchy down so will get all domains.You sme technique and you will
get computers under each domain.

for more info please see MS help on these APIs.
If you have some doubts please get back.

Regards,
ufolk123
0
 
LVL 3

Expert Comment

by:ufolk123
ID: 2502758
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.));
}
0
 

Author Comment

by:nsivatg
ID: 2503890
Thanks for the time spent.

I'll read the code and accept your answer.

Regards

Shiva

Shiva@indusaglobal.com
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question