连接远程电脑并执行系统命令systeminfo>>test.txt(这样获取信息比较方便)
以下有两个参考程序(网上抄来的)和一个地址
http://www.codeguru.com/Cpp/W-P/system/misc/article.php/c5675
注意: 使用argv 远程传递IP username password
#define   POLARITY  
  #define   _WIN32_DCOM  
   
  #include   <afx.h>  
   
  #include   <iostream>  
  using   namespace   std;  
  #include   <iomanip>  
  #include   <windows.h>  
  #include   <chstring.h>  
  #include   <chstrarr.h>  
  #include   <assert.h>  
  #include   <comdef.h>  
  #include   <wbemcli.h>  
   
  #include   <winbase.h>  
   
  int   main(int   argc,   char   **argv)  
  {  
  HRESULT   hres;  
  hres   =     CoInitializeEx(0,   COINIT_MULTITHREADED);   //   Initialize   COM.  
  if   (FAILED(hres))  
  {  
  cout   <<   "Failed   to   initialize   COM   library.   Error   code   =   0x"    
  <<   hex   <<   hres   <<   endl;  
  return   1;                                     //   Program   has   failed.  
  }  
   
  hres   =     CoInitializeSecurity(NULL,   -1,   NULL,   NULL,  
                  RPC_C_AUTHN_LEVEL_CONNECT,    
                  RPC_C_IMP_LEVEL_IDENTIFY,    
                  NULL,   EOAC_NONE,   0  
                  );  
   
  if   (FAILED(hres))  
  {  
  cout   <<   "Failed   to   initialize   security.   Error   code   =   0x"    
  <<   hex   <<   hres   <<   endl;  
  CoUninitialize();  
  return   1;                                     //   Program   has   failed.  
  }  
          IWbemLocator   *pLoc   =   0;  
   
          hres   =   CoCreateInstance(CLSID_WbemLocator,   0,    
                  CLSCTX_INPROC_SERVER,   IID_IWbemLocator,   (LPVOID   *)   &pLoc);  
   
          if   (FAILED(hres))  
          {  
                  cout   <<   "Failed   to   create   IWbemLocator   object.   Err   code   =   0x"  
  <<   hex   <<   hres   <<   endl;  
                  CoUninitialize();  
                  return   1;           //   Program   has   failed.  
          }  
   
          IWbemServices   *pSvc   =   0;  
   
          //   Connect   to   the   root\default   namespace   with   the   current   user.  
          hres   =   pLoc->ConnectServer(  
  _bstr_t(L"\\\\192.168.0.21\\ROOT\\cimv2"),    
  _bstr_t(L"Administrator"),  
  _bstr_t(L"1234"),  
  0,                                                                      
  NULL,  
  0,                                                                      
  0,                                                                      
  &pSvc  
  );  
   
          if   (FAILED(hres))  
          {  
                  cout   <<   "Could   not   connect.   Error   code   =   0x"    
  <<   hex   <<   hres   <<   endl;  
                  pLoc->Release();            
                  CoUninitialize();  
                  return   1;             //   Program   has   failed.  
          }  
   
          cout   <<   "Connected   to   WMI"   <<   endl;  
   
          //   Set   the   proxy   so   that   impersonation   of   the   client   occurs.  
          hres   =   CoSetProxyBlanket(pSvc,  
  RPC_C_AUTHN_WINNT,  
  RPC_C_AUTHZ_NONE,  
  NULL,  
  RPC_C_AUTHN_LEVEL_CALL,  
  RPC_C_IMP_LEVEL_IMPERSONATE,  
  NULL,  
  EOAC_NONE  
  );  
   
          if   (FAILED(hres))  
          {  
                  cout   <<   "Could   not   set   proxy   blanket.   Error   code   =   0x"    
  <<   hex   <<   hres   <<   endl;  
  pSvc->Release();  
  pLoc->Release();            
                  CoUninitialize();  
                  return   1;             //   Program   has   failed.  
          }  
   
   
  //   Use   the   IWbemServices   pointer   to   make   requests   of   WMI.    
  //   Make   requests   here:  
                    //   为什么以下语句会出错?总是存取禁止。  
  IWbemClassObject   *pProcess   =   NULL;  
  hres   =   pSvc->GetObject(_bstr_t(L"Win32_NetworkAdapterConfiguration"),    
  0,    
  NULL,    
  &pProcess,    
  NULL);  
  if(FAILED(hres))  
  {  
                  cout   <<   "Could   not   get   object   Win32_NetworkAdapterConfiguration.   Error   code   =   0x"    
  <<   hex   <<   hres   <<   endl;  
  pSvc->Release();  
  pLoc->Release();            
                  CoUninitialize();  
   
  return   1;  
  }  
   
  //    
   
          //   Cleanup  
          //   ========  
   
          pSvc->Release();  
          pLoc->Release();            
          CoUninitialize();  
   
          return   0;       //   Program   successfully   completed.  
  }   ///////////////////////////////////////////////////////////////////////

解决方案 »

  1.   

    我写了个小程序,利用135端口共享远程主机的c盘为temp,我在xp,2003下能正确达到预期的效果,但是,如果目标的主机是2000的话,在getobject   class的时候会提示访问拒绝,我已经确认输入的帐号为管理员权限.郁闷啊!
    付原代码,望高手指教了!!!另,这份代码做测试的时候,只能针对远程的.
    //   WMI_SHARE.cpp   :   定义控制台应用程序的入口点。
    //
    //#define   _WIN32_DCOM,在设置里定义,
    #include   "stdafx.h "
    #include   "windows.h "
    #include   "comdef.h "
    #include   "atlcomcli.h "
    #include   "atlstr.h "#include   "objbase.h "
    #include   "Wbemidl.h "
    #include   "stdio.h "
    //#   pragma   comment(lib,   "wbemuuid.lib "),在设置里定义。int   main(int   argc,char   *argv[])
    {
    HRESULT   hres;
    CString   csHostName=argv[1];
    CString   UserName=argv[2];
    CString   PassWord=argv[3];
    CComBSTR   User=UserName.AllocSysString();
    CComBSTR   Pass=PassWord.AllocSysString();
    CString   NetRess;
    CString   _sig= "\\\\ ";
    NetRess=_sig+csHostName+ "\\root\\cimv2 ";
    CComBSTR   RemoteHost=NetRess.AllocSysString();
    hres=CoInitializeEx(0,COINIT_MULTITHREADED);
      if   (FAILED(hres))
      {
      printf( "failed   to   initialize   COM   library ");
      CoUninitialize();
      return   1;
      }
      hres=CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_DEFAULT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL);
    if(FAILED(hres))
    {
    printf( "failed   to   initialize   security ");
    CoUninitialize();
    return   1;
    }
    IWbemLocator   *pLoc=NULL;
    hres=CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER,IID_IWbemLocator,(LPVOID   *)&pLoc);
    if(FAILED(hres))
    {
    printf( "failed   to   create   IWbemLocator   object. ");
    CoUninitialize();
    return   1;
    }
    IWbemServices   *pSvc=NULL;
    //if(argc=1)
    //hres=pLoc-> ConnectServer(_bstr_t(L "\\\\.\\ROOT\\CIMV2 "),NULL,NULL,0,NULL,0,0,&pSvc);
    hres=pLoc-> ConnectServer(RemoteHost,User,Pass,0,NULL,0,0,&pSvc);
    if   (FAILED(hres))
    {
    printf( "could   not   connect,the   hres   is   %x ",hres);
    pLoc-> Release();
    CoUninitialize();
    return   1;
    }
    hres=CoSetProxyBlanket(pSvc,
    RPC_C_AUTHN_WINNT,
    RPC_C_AUTHZ_NONE,
    NULL,
    RPC_C_AUTHN_LEVEL_CALL,
    RPC_C_IMP_LEVEL_IMPERSONATE,
    NULL,
    EOAC_NONE
    );
    if(FAILED(hres))
    {
    printf( "set   proxy   fail ");
    pSvc-> Release();
    pLoc-> Release();
    CoUninitialize();
    return   1;  
    }
    CComBSTR   bstrMethod( "Create ");
    //BSTR   ClassName   =   SysAllocString(L "Win32_Share ");
    CComBSTR   bstrClass( "Win32_Share ");/*本来按照ch07-sample的代码是弄成CComBSTR   bstrClass( "Win32_Share.Name=\ "E$\ " ");
        但是这样执行在getMethod会出错*/IWbemClassObject   *pClass=NULL;
    hres=pSvc-> GetObject(bstrClass,0,NULL,&pClass,NULL);
    if(FAILED(hres))
    {
    printf( "fail   to   get   class   adress   ,the   error   is   %x ",hres);
    return   1;
    }
    IWbemClassObject   *pMethod=NULL;
    hres=pClass-> GetMethod(bstrMethod,0,&pMethod,NULL);
    if(FAILED(hres))
    {
    printf( "fail   to   get   method   adress   ,the   error   is   %x ",hres);
    return   1;
    }
    IWbemClassObject   *pInParams=NULL;
    pMethod-> SpawnInstance(0,&pInParams);
    CComVariant   varName( "Temp ");hres=pInParams-> Put(L "Name ",0,&varName,0);
    CComVariant   varPath( "C:\\ ");
    hres=pInParams-> Put(L "Path ",0,&varPath,0);
    CComVariant   varType(int(0));
    hres=pInParams-> Put(L "Type ",0,&varType,0);
    IWbemClassObject   *pOutParams=NULL;
    hres=pSvc-> ExecMethod(bstrClass,bstrMethod,0,NULL,pInParams,&pOutParams,NULL);
    if(FAILED(hres))
    {
    printf( "fail   to   exec   method\n\r   the   hres   is   %d   ",hres);VariantClear(&varName);
    VariantClear(&varPath);
    VariantClear(&varType);
    pOutParams-> Release();
    pInParams-> Release();
    pMethod-> Release();
    pClass-> Release();
    pSvc-> Release();
    pLoc-> Release();
    CoUninitialize();
    return   1;
    }
    CComVariant   varReturnValue;
    hres=pOutParams-> Get(_bstr_t(L "ReturnValue "),0,&varReturnValue,   NULL,   0);
    printf( "the   return   is   %s\n\r ",hres);
    VariantClear(&varName);
    VariantClear(&varReturnValue);
    VariantClear(&varPath);
    VariantClear(&varType);
    pOutParams-> Release();
    pInParams-> Release();
    pMethod-> Release();
    pClass-> Release();
    pSvc-> Release();
    pLoc-> Release();
    CoUninitialize();
    return   0;
      

  2.   

    WMI的咚咚都是xp之后才支持的好吧