WNetX函数族,能实现这一功能。详细参考MSDN

解决方案 »

  1.   

    试一试WNetEnumResource函数,msdn上有详细介绍。
      

  2.   

    用DsGetDcName函数:
    DWORD DsGetDcName(
      LPCTSTR ComputerName,
      LPCTSTR DomainName,
      GUID *DomainGuid,
      LPCTSTR SiteName,
      ULONG Flags,
      PDOMAIN_CONTROLLER_INFO
      );
     
      

  3.   

    DsGetDcName()是NT5专用函数,9X不支持。
      

  4.   

    #include <winnetwk.h>
    //获取网上邻居的列表,存储在串表中
    bool EnumNetResource(TStrings *rcList,LPNETRESOURCE lpNR, DWORD dwScope,DWORD dwType)
    {
        HANDLE hEnum = 0;
        DWORD dwResult = WNetOpenEnum(
                dwScope,    // scope of enumeration
                dwType,        // resource types to list
                0,        // enumerate all resources
                lpNR,        // pointer to resource structure (NULL at first time)
                &hEnum        // handle to resource
                ) ;
        if( dwResult != NO_ERROR ) return false;    bool bRet=true;
        DWORD dwEntries = 0xFFFFFFFF ;    // enumerate all possible entries
        NETRESOURCE NR[1024];
        DWORD dwBuffer=1024*sizeof(NETRESOURCE);    while(1) {
            dwResult =    WNetEnumResource(
                    hEnum,        // resource-handle
                &dwEntries,
                (LPVOID)NR,
                &dwBuffer
                ) ;
            if( dwResult == ERROR_NO_MORE_ITEMS ) break;
        else if( dwResult != NO_ERROR ) { bRet=false; break;}        for(DWORD i = 0 ; i < dwEntries ; i++ ) {
                if(NR[i].dwDisplayType==RESOURCEDISPLAYTYPE_SERVER) {
                    char *p=NR[i].lpRemoteName;
                    while(*p=='\\') p++;
            if(*p) rcList->Add(p);
                } else if((NR[i].dwUsage&RESOURCEUSAGE_CONTAINER)==
                                              RESOURCEUSAGE_CONTAINER) {
            bRet=EnumNetResource(rcList,&NR[i],dwScope,dwType);
                    if(bRet==false) break;
                }
            }
            if(bRet==false) break;
        }    WNetCloseEnum(hEnum) ;
        return bRet;
    }
    //------获取某主机的IP地址,如果主机名为空,则返回本机的名和IP地址---------AnsiString gethostip(AnsiString &host)
    {
        WSADATA wsaData;
        AnsiString IP;    WSAStartup(MAKEWORD(2,0),&wsaData);
        if(host.IsEmpty()) {
            char hostname[128];
            if(gethostname(hostname,128)!=0) return AnsiString("");
        host=hostname;
        }
        try{
            struct hostent *hp=gethostbyname(host.c_str());
            IP=inet_ntoa(*(struct in_addr*)hp->h_addr_list[0]);
        }
        catch(...){
            IP="";
        }
        WSACleanup();
        return IP;
    }//----------------------Example-------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        ListBox1->Clear();
        Screen->Cursor=crHourGlass;
        EnumNetResource(ListBox1->Items,NULL,RESOURCE_GLOBALNET,RESOURCETYPE_ANY);
        Screen->Cursor=crDefault;
    }
    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
        if(ListBox1->ItemIndex==-1) return;
        Screen->Cursor=crHourGlass;
        AnsiString IP=gethostip(ListBox1->Items->Strings[ListBox1->ItemIndex]);
        Screen->Cursor=crDefault;
        Edit1->Text=IP;
        if(IP=="") ShowMessage("无法与主机联系!");