c++程序使注册表通过WMI录入不工作
C++ program to make registry through WMI entry is not working.
我已经编辑了下面的代码,使用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,代码开始工作。这给了我一个起点,我最终发现我们需要设置提供者和所需架构的上下文。下面是一些我觉得很有用的链接:
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- WMI检测进程创建事件-c++
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- c++程序使注册表通过WMI录入不工作
- 有什么c++库可以和WMI一起工作吗?