Muokkaa

Jaa


Initializing COM for a WMI Application

The first step in connecting to WMI is setting up the COM calls to CoInitializeEx and CoInitializeSecurity.

The code examples in this topic require the following references and #include statements to compile correctly.

#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")

The following procedure describes how to initialize COM from a client application.

To initialize COM from a client application

  1. Initialize COM with a call to CoInitializeEx.

    Calling CoInitializeEx is a standard procedure for setting up a COM interface. Therefore, WMI does not require that you observe any additional procedures when calling CoInitializeEx. For more information, see COM.

    The following code example describes how to call CoInitializeEx.

    HRESULT hr;
    hr = CoInitializeEx(0, COINIT_MULTITHREADED); 
    if (FAILED(hr)) 
    { cout << "Failed to initialize COM library. Error code = 0x"
           << hex << hr << endl; 
      return hr;
    }
    
  2. Set the general COM security levels with a call to the CoInitializeSecurity interface.

    CoInitializeSecurity is a standard function you must call when setting up a COM interface for a process. Call CoInitializeSecurity if you want to set the default security settings for authentication, impersonation, or authentication service for an entire process. For more information, see Setting Client Application Process Security. If you want to set or change the security for a specific proxy, you must call CoSetProxyBlanket. Use CoSetProxyBlanket whenever you must set or change COM security when running inside another process where you cannot control the default security settings for authentication, impersonation, or authentication service. For more information, see Setting the Security Levels on a WMI Connection and Setting the Security on IWbemServices and Other Proxies.

    WMI has several security issues you should be aware of when programming a WMI client application. For more information, see Setting Client Application Process Security.

    The following code example describes how to call CoInitializeSecurity to set COM security on the process.

    hr =  CoInitializeSecurity(
        NULL,                        // Security descriptor    
        -1,                          // COM negotiates authentication service
        NULL,                        // Authentication services
        NULL,                        // Reserved
        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication level for proxies
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation level for proxies
        NULL,                        // Authentication info
        EOAC_NONE,                   // Additional capabilities of the client or server
        NULL);                       // Reserved
    
    if (FAILED(hr))
    {
       cout << "Failed to initialize security. Error code = 0x" 
            << hex << hr << endl;
       CoUninitialize();
       return hr;                  // Program has failed.
    }
    

After you initialize COM, your next step is to create a connection to a WMI namespace. For more information, see Creating a Connection to a WMI Namespace.

Creating a WMI Application Using C++

Access to WMI Namespaces