1)初始化COM库
        由于用C++编写WMI应用是基于COM技术的,所以必须初始化COM库,这时调用函数CoInitializeEx用于初始化COM库。
    HRESULT hres;
    hres =  CoInitializeEx(0, COINIT_MULTITHREADED); 
    if (FAILED(hres))
    ...{
        cout << "Failed to initialize COM library. Error code = 0x" 
            << hex << hres << endl;
        return 1;                  // Program has failed.
    }2)初始化COM库安全级别
        调用函数CoInitializeSecurity用于初始化COM安全级别。
    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.
    }3)连接到WMI命名空间
        通过调用CoCreateInstance初始化WMI的定位器(IWbemLocator类型的实例)。
    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.
    }
为什么在2000下 CoCreateInstance这个函数调用会失败,请各位高手指点一二。      

解决方案 »

  1.   

    Return Values
    S_OK 
    An instance of the specified object class was successfully created. 
    REGDB_E_CLASSNOTREG 
    A specified class is not registered in the registration database. Also can indicate that the type of server you requested in the CLSCTX enumeration is not registered or the values for the server types in the registry are corrupt. 
    CLASS_E_NOAGGREGATION 
    This class cannot be created as part of an aggregate. 
    E_NOINTERFACE 
    The specified class does not implement the requested interface, or the controlling IUnknown does not expose the requested interface. 判断返回值