您的位置:首页 > 理论基础

如何获取远程计算机的用户列表?

2009-10-06 14:56 399 查看
如何获取远程计算机的用户列表?



c++buider的: (以后用到再翻译为delphi的)

TForm1   *Form1;   
  //---------------------------------------------------------------------------   
  __fastcall   TForm1::TForm1(TComponent*   Owner)   
                  :   TForm(Owner)   
  {   
  }   
  //---------------------------------------------------------------------------   
    
    
  AnsiString   gethostip(AnsiString   &host)   
  {   
          WSADATA   wsaData;   
          AnsiString         IP;   
          char     temp[255];   
          strcpy(temp,host.c_str());   
          int     pos=0;   
          while(temp[pos++]=='//');   
          pos--;   
          //                 temp+=i-1;   
          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(temp+pos);//host.c_str());   
                  if(hp   ==   NULL)   
                  {   
                          IP="";   
                  }   
                  else   
                  {   
                          IP=inet_ntoa(*(struct         in_addr*)hp->     h_addr_list[0]);   
                  }   
          }   
          catch(...)   
          {   
                  IP="";   
          }   
          WSACleanup();   
          return   IP;   
  }   
  //---------------------------------------------------------------------------   
  bool   EnumNetResource(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++)   
                          {   
                                      //ShowMessage("OK");   
                                      if(NR[i].dwDisplayType==RESOURCEDISPLAYTYPE_SERVER)   
                                      {   
                                                  char     *p=NR[i].lpRemoteName;   
                                                  while(*p=='//')     p++;   
                                                  if(*p)   
                                                  {   
                                                          AnsiString   IP   =   gethostip(p);   
                                                        //   TListItem   *Item1;   
                                                        //   Item1   =   Form1->ListView1->Items->Add();   
                                                        //   Item1->Caption   =   p;   
                                                          //   Item1->SubItems->Add(IP);   
                                                          Form1->ListBox1->Items->Add(IP+"==="+p);   
                                                  }   
                                      }   
                                      else   
                                      {   
                                                  if((NR[i].dwUsage&     RESOURCEUSAGE_CONTAINER)==RESOURCEUSAGE_CONTAINER)   
                                                  {   
                                                              bRet=EnumNetResource(&NR[i],dwScope,dwType);   
                                                              if(bRet==false)     break;   
                                                  }   
                                      }   
                          }   
                          if(bRet==false)     break;   
                    }   
    
                    WNetCloseEnum(hEnum)     ;   
                    return     bRet;   
  }   
    
    
    
    
  //---------------------------------------------------------------------------   
    
  void   __fastcall   TForm1::Button1Click(TObject   *Sender)   
  {   
          ListView1->Clear();   
          Screen->Cursor=crHourGlass;   
          EnumNetResource(NULL,RESOURCE_GLOBALNET,RESOURCETYPE_ANY);   
          Screen->Cursor=crDefault;   
  }   
  //---------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: