WMI 编程之 Win32_Process::Create 方法调用

来源:互联网 发布:nginx端口转发配置 编辑:程序博客网 时间:2024/06/07 03:18

 

  创建 ATL  项目,  重写新生成模板类的 winmain 成员,添加下列代码
 //
 // 初始化 COM
 //
 HRESULT hres;

 hres =  CoInitializeSecurity( NULL,
  -1,                          // COM authentication
  NULL,                        // Authentication services
  NULL,                        // Reserved
  RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication
  RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation 
  NULL,                        // Authentication info
  EOAC_NONE,                   // Additional capabilities
  NULL                         // Reserved
  );

 if (FAILED(hres))
 {
  cout << "Failed to initialize security. Error code = 0x"
   << hex << hres << endl;
  CoUninitialize();
  return 1;                    // Program has failed.
 }


 //
 // CoCreateInstance 创建 Locator 实例 .
 //
 CComQIPtr< IWbemLocator> pLoc;


 hres = CoCreateInstance(
    CLSID_WbemLocator,      
    0,
    CLSCTX_INPROC_SERVER,
    IID_IWbemLocator, (LPVOID *) &pLoc);

 if (FAILED(hres))
 {
  CoUninitialize();
  return 1;                 // Program has failed.
 }

 //
 // 连接到 "root/cimv2" 命名空间,获得 IWbemServices 类型的指针
 //
 CComQIPtr< IWbemServices >  pSvc;
 
 hres = pLoc->ConnectServer(
         _bstr_t(L"ROOT//CIMV2"), // Object path of WMI namespace
         NULL,                    // User name. NULL = current user
         NULL,                    // User password. NULL = current
         0,                       // Locale. NULL indicates current
         NULL,                    // Security flags.
         0,                       // Authority (e.g. Kerberos)
         0,                       // Context object
         &pSvc                    // pointer to IWbemServices proxy
         );
   
    if (FAILED(hres))
    {  
        CoUninitialize();
        return 1;                // Program has failed.
    }


 // 
 // 设置 IWbemServices 代理安全, 这样 WMI 服务相当于客户端使用
 //
 hres = CoSetProxyBlanket(
       pSvc,                        // Indicates the proxy to set
       RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
       RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
       NULL,                        // Server principal name
       RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx
       RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
       NULL,                        // client identity
       EOAC_NONE                    // proxy capabilities
    );

    if (FAILED(hres))
    {
        CoUninitialize();
        return 1;
    }


 //
 // 得到 Win32_Process::Create 方法
 //
 // 得到执行的方法名和类名
 //
  _bstr_t bstrMethodName("Create");
  _bstr_t bstrClassName ("Win32_Process"); 
 
 CComQIPtr< IWbemClassObject > pClass ;
 pSvc->GetObject( bstrClassName, 0, NULL, &pClass, NULL );


 CComQIPtr <IWbemClassObject> pMethod ;
 pClass->GetMethod( bstrMethodName, 0,  &pMethod, NULL );

 
 //
 // 创建 Win32_Process::Create 方法的参数实例
 //
 CComQIPtr <IWbemClassObject> pClassInstance ;
    hres = pMethod->SpawnInstance(0, &pClassInstance);
 // Create the values for the in parameters
 _variant_t varCommand("notepad.exe"); 
 // Store the value for the in parameters
 hres = pClassInstance->Put(L"CommandLine", 0, &varCommand, 0);
 

 //
 // 执行方法
 //
 CComQIPtr <IWbemClassObject> pOutParams ;
   
 hres = pSvc->ExecMethod( bstrClassName ,
      bstrMethodName ,
      0,
      NULL,
      pClassInstance,
      &pOutParams,
      NULL);

 _variant_t varReturnValue ;
 
hres = pOutParams->Get(_bstr_t(L"ReturnValue"), 0,  &varReturnValue, NULL, 0);
 

原创粉丝点击