简体   繁体   English

在C ++中将DWORD值写入注册表

[英]Writing a DWORD value to registry in C++

I am trying to write a DWORD value to the registry programmatically in C++. 我试图在C ++中以编程方式将DWORD值写入注册表。

I've done a bit of searching and I have found that this question has been asked before. 我做了一些搜索,我发现之前已经问过这个问题了。 I've tried to follow their solution but have come up with a really frustrating issue which, as far as I know, have not been addressed by their solution. 我试图按照他们的解决方案,但提出了一个非常令人沮丧的问题,据我所知,他们的解决方案没有解决这个问题。

This is my code: 这是我的代码:

HKEY hKey;
LPCWSTR sKeyPath;
int iResult;

sKeyPath = L"Software\\ABI\\";
iResult = RegOpenKeyEx(HKEY_CURRENT_USER, sKeyPath, NULL, KEY_ALL_ACCESS, &hKey);
DWORD value = 0x00000003;
iResult = RegSetValueEx(hKey, L"Test", NULL, REG_DWORD, (const BYTE*)value, sizeof(value));
RegCloseKey(hKey);

I've done some basic debugging and found that the value of iResult is 998 after I call RegSetValueEx . 我做了一些基本调试,发现调用RegSetValueExiResult的值是998。 I am sure that this key is present in the windows registry because I created it manually with regedit.exe for testing purposes. 我确信这个密钥存在于Windows注册表中,因为我使用regedit.exe手动创建它以进行测试。 The value of the DWORD "Test" is initially 0x00000009 and is unchanged after I run my program. DWORD“Test”的值最初是0x00000009,在运行程序后没有变化。

I am not sure where I am wrong. 我不确定我错在哪里。

Any help would be appreciated. 任何帮助,将不胜感激。

PS I've not managed to find any helpful site on the net for error 998. The only reference I found mentions that that's the worst error you can get when handling registry. PS我没有设法在网上找到任何有用的网站错误998.我发现的唯一参考提到这是处理注册表时可以得到的最严重的错误。

PPS By the way, I'm running this program on Windows 8. I don't think that changes anything but I've had experiences with Windows 8 having some weird security issues before. PPS顺便说一句,我在Windows 8上运行这个程序。我不认为这会改变任何东西,但我之前遇到过一些奇怪的安全问题。

You need to pass the address of value : 您需要传递value地址

iResult = RegSetValueEx(hKey,
                        L"Test",
                        NULL,
                        REG_DWORD,
                        (const BYTE*)&value, // Change made here.
                        sizeof(value));

The error code 998 means: 错误代码998表示:

Invalid access to memory location. 对内存位置的访问无效。

When the address of value is not passed its actual value ( 3 ) is being used as a memory address, causing the failure. 当未传递value的地址时,其实际值( 3 )被用作存储器地址,从而导致失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM