c++程序使注册表通过WMI录入不工作

C++ program to make registry through WMI entry is not working.

本文关键字:工作 WMI 程序 注册表 c++      更新时间:2023-10-16

我已经编辑了下面的代码,使用WMI调用通过c++使注册表项,但虽然代码返回零,它不使注册表项。我以管理员权限运行它。但读取注册表项的代码是完美的工作。希望有人能帮我解决这个问题。

    void cppMain()
    {
    // Step 1: --------------------------------------------------
    // Initialize COM. ------------------------------------------
    ComLibUsage     comLibUsage( ComLibUsage::Threading::multiThreaded );
    // Step 2: --------------------------------------------------
    // Set general COM security levels --------------------------
    // Note: If you are using Windows 2000, you must specify -
    // the default authentication credentials for a user by using
    // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
    // parameter of CoInitializeSecurity ------------------------
    CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL) || Fail( "Failed to initialize security" );
    // Step 3: ---------------------------------------------------
    // Obtain the initial locator to WMI -------------------------
    IWbemLocatorPtr     pLoc;
    CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc) || Fail( "Failed to create IWbemLocator object." );
    // Step 4: ---------------------------------------------------
    // Connect to WMI through the IWbemLocator::ConnectServer method
    // Connect to the local rootcimv2 namespace
    // and obtain pointer pSvc to make IWbemServices calls.
    IWbemServicesPtr    pSvc;
    pLoc->ConnectServer(_bstr_t(L"ROOT\default"), NULL, NULL, 0, NULL, 0, 0, &pSvc ) || Fail( "Could not connect." );
    cout << "Connected to ROOT\default WMI namespace" << endl;

    // Step 5: --------------------------------------------------
    // Set security levels for the proxy ------------------------
    CoSetProxyBlanket( pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE) || Fail( "Could not set proxy blanket" );
    ////////////////////////////////////////////////////////////////
    // set up to call the WmiSetBrightness Method
    _bstr_t     methodName  = L"CreateKey";
    _bstr_t     className   = L"StdRegProv";
    IWbemClassObjectPtr pClass;
    pSvc->GetObject(className, 0, NULL, &pClass, NULL)
        || Fail( "GetObject(className, ...) failed" );
    IWbemClassObjectPtr pInParamsDefinition;
    pClass->GetMethod(methodName, 0, &pInParamsDefinition, NULL)
        || Fail( "GetMethod(methodName, ...) failed" );
    IWbemClassObjectPtr pClassInstance;
    pInParamsDefinition->SpawnInstance(0, &pClassInstance)
        || Fail( "SpawnInstance failed" );
    _variant_t  var1( L"2147483650" );
    pClassInstance->Put(L"hDefKey", 0, &var1, CIM_UINT32)|| Fail( "Put failed for 'hDefKey'" );

    _variant_t  var2( L"SOFTWARE\Newkey" );
    pClassInstance->Put(L"sSubKeyName", 0, &var2, CIM_STRING)|| Fail( "Put failed for 'sSubKeyName'" );

    /*_variant_t  var3( L"CachePrefix" );
    pClassInstance->Put(L"sValueName", 0, &var3, CIM_STRING)|| Fail( "Put failed for 'sValueName'" );
    _variant_t  var4( L"CachePrefix" );
    pClassInstance->Put(L"sValue", 0, &var4, CIM_STRING)|| Fail( "Put failed for 'sValue'" );*/
    // Execute Method
    IWbemClassObject* pOutParams = NULL;
     pSvc->ExecMethod(className, methodName, 0,
    NULL, pClassInstance, &pOutParams, NULL)|| Fail( "Could not execute method" );
    // To see what the method returned,
    // use the following code.  The return value will
    // be in &varReturnValue
    _variant_t varReturnValue;
    pOutParams->Get(_bstr_t(L"ReturnValue"), 0, &varReturnValue, NULL, 0)|| Fail( "Get failed" );
    wcout << varReturnValue.intVal << endl;
    //std::wstring result = varReturnValue.bstrVal;
    //wcout << result;
    system("pause");
    }

我不确定这个问题是否和我的一样,但我之前也遇到过类似的问题,所以我决定把我的发现告诉大家。

我正在编写非常相似的代码。在我的例子中,我使用的是x86。不知何故,在测试期间,我切换到x64,代码开始工作。这给了我一个起点,我最终发现我们需要设置提供者和所需架构的上下文。下面是一些我觉得很有用的链接: