Auslesen der Computer im Netzwerk mit NetServerEnum()



  • Im folgendem Code werden alle Benutzer im Netzwerk angezeigt.
    Wie kann ich neben den einzelnen Computern noch die dazugehörige IP-Adresse anhängen?

    #include <winsock.h>
    #include <lm.h>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #pragma comment(lib, "Netapi32.lib")
    
    void list() 
    {
    	LPSERVER_INFO_101 pBuf = NULL;
       LPSERVER_INFO_101 pTmpBuf;
       DWORD dwLevel = 101;
       DWORD dwPrefMaxLen = -1;
       DWORD dwEntriesRead = 0;
       DWORD dwTotalEntries = 0;
       DWORD dwTotalCount = 0;
       DWORD dwServerType = SV_TYPE_SERVER; // all servers
       DWORD dwResumeHandle = 0;
       NET_API_STATUS nStatus;
       LPTSTR pszServerName = NULL; //LPCWSTR
       DWORD i;
    
       string str;
    
       nStatus = NetServerEnum(NULL,
    							dwLevel,
                               (LPBYTE *) &pBuf,
                               dwPrefMaxLen,
                               &dwEntriesRead,
                               &dwTotalEntries,
                               dwServerType,
                               NULL,
                               &dwResumeHandle);
       //
       // If the call succeeds,
       //
    
       if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
       {
          if ((pTmpBuf = pBuf) != NULL)
          {
             //
             // Loop through the entries and 
             //  print the data for all server types.
             //
    
             for (i = 0; i < dwEntriesRead; i++)
             {
                //ASSERT(pTmpBuf != NULL);
    
                if (pTmpBuf == NULL)
                {
                   cout << "An access violation has occurred" << endl;
                   break;
                }
    
    			else
    			{
    				char szBuf[MAX_PATH];
    				wchar_t buff[100];
    				swprintf(buff,L"%s",pTmpBuf->sv101_name);
    
    				//convert to byte format
    
    				WideCharToMultiByte(CP_ACP,0,buff,-1,szBuf,
    							MAX_PATH+1,NULL,NULL);
    				//m_clients.InsertItem(0,szBuf);
    				//cout << szBuf << endl;
    
    				str += szBuf;
    				str += "\n";
    
    				pTmpBuf++;
    				dwTotalCount++;
    			}
             }
             // Display a warning if all available entries were
             //  not enumerated, print the number actually 
             //  enumerated, and the total number available.
    
             if (nStatus == ERROR_MORE_DATA)
             {
                cout << "More entries available!!!" << endl;
    			fprintf(stderr, "\nMore entries available!!!\n");
    			fprintf(stderr, "Total entries: %d", dwTotalEntries);
             }
    
    		 char total[20];
    		 wsprintf(total,"%d",dwTotalCount);
             cout << "Es wurden [" << total << "] Computer gefunden:" << endl;
    		 cout << "--------------------------------" << endl;
    		 cout << str << endl;
    
          }
       }
       else
          cout << "A system error has occurred 2 " << endl;
       //
       // Free the allocated buffer.
       //
       if (pBuf != NULL)
          NetApiBufferFree(pBuf);
    
    }
    
    int main()
    {
    	list();
    	return 0;
    }
    


  • habs hinbekommen!



  • Und wie?



  • hab mir eine Funktion HostIP() erstellt, die ein string mit der IP zurückgibt 😃



  • Wow, ich hoffe du bekommst auf deine künftigen Fragen ähnliche Antworten.



  • Wäare sinnvoll, wenn du die Funktion hier posten könntest, dann könnten auch andere die das gleiche Problem haben, diese verwenden.



  • Da wir wohl keine Antwort erwarten dürfen:

    hostent *hEnt = 0;
        hEnt = gethostbyname( name1 );//bekannter hostname in name1
    
        CString m_ip;
        m_ip=inet_ntoa( *(LPIN_ADDR)*hEnt->h_addr_list );
        MessageBox(m_ip);
    


  • Sorry hier kommt der code:

    string HostIP(string hname)
    {
    	#define h_addr h_addr_list[0]	//für getHostIP
    
    	WSADATA WsaDat;	
    	hostent *myhost;	
    	in_addr myaddr;	
    
        myhost = NULL;
    
        if (WSAStartup(MAKEWORD(1,1),&WsaDat)==0)
    	{			
    		myhost = gethostbyname(hname.c_str());	
    
            if (myhost!=NULL)
    		{      			
    			myaddr.S_un.S_un_b.s_b1 = myhost->h_addr[0];
    			myaddr.S_un.S_un_b.s_b2 = myhost->h_addr[1];
    			myaddr.S_un.S_un_b.s_b3 = myhost->h_addr[2];
    			myaddr.S_un.S_un_b.s_b4 = myhost->h_addr[3];
    
    			//printf("%s - %s\n",host, inet_ntoa(myaddr));
    			return inet_ntoa(myaddr);
    		}		
    
    		else
    		return "Unbekannter Host";
    		WSACleanup();
    	}//if WSAStartup
    
    	else
    	return "ERROR";
    
    	return 0;
    }
    

Anmelden zum Antworten