C++ 在注册表中设置 DWORD 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5437800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:10:13  来源:igfitidea点击:

Setting a DWORD value in the registry

c++windowswinapiregistry

提问by Adrian

I'm trying to set a DWORD value in the registry. I made it work with a text value, but now I want to set another value with a numeric one(0). But it doesnt write it.
This is my code:

我正在尝试在注册表中设置一个 DWORD 值。我使它与文本值一起工作,但现在我想用数字 one(0) 设置另一个值。但它不写它。
这是我的代码:

RegOpenKeyEx(HKEY_CURRENT_USER, "Software\pager", 0, KEY_ALL_ACCESS, &hKey);
RegSetValueEx(hKey, TEXT("Save"), 0, REG_SZ, (const BYTE*)0x00, sizeof(DWORD));
RegCloseKey(hKey);

PS: the key already exist with the value 1 so I'm trying to overide it with the value 0(I'm not creating a new one).

PS:密钥已经存在,值为 1,所以我试图用值 0 覆盖它(我没有创建一个新的)。

回答by Matteo Italia

The biggest error is in (const BYTE*)0x00: you are casting 0x00 to a BYTE *, which means that basically you are passing a NULLpointer. Instead, you should create a DWORDvariable, put the value you want to store in the registry in it and pass a pointer to it instead of that 0x00.

最大的错误在于(const BYTE*)0x00:您将 0x00 强制转换为 a BYTE *,这意味着基本上您正在传递一个NULL指针。相反,您应该创建一个DWORD变量,将要存储在注册表中的值放入其中并传递一个指向它的指针而不是那个0x00

Also, you must change REG_SZto REG_DWORDif you want to store a DWORDvalue, otherwise the DWORDwill be interpreted as a (somewhat strange) string.

此外,如果要存储值,则必须更改REG_SZ为,否则将被解释为(有些奇怪的)字符串。REG_DWORDDWORDDWORD

RegOpenKeyEx(HKEY_CURRENT_USER, "Software\pager", 0, KEY_ALL_ACCESS, &hKey);
DWORD value=0;
RegSetValueEx(hKey, TEXT("Save"), 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
RegCloseKey(hKey);

But, most importantly, you should really check the return values of these functions: now you're just "hoping" they work, ignoring any failure and continuing with the instruction flow, which can lead to unexpected situations.

但是,最重要的是,您应该真正检查这些函数的返回值:现在您只是“希望”它们工作,忽略任何失败并继续执行可能导致意外情况的指令流。

If you checked the error codes you would have noticed immediately that it is the RegSetValueExfunction that fails, and the error code may have been something like "invalid parameter", that would have pointed you in the right direction.

如果您检查了错误代码,您会立即注意到它是RegSetValueEx失败的函数,并且错误代码可能类似于“无效参数”,这将为您指明正确的方向。

回答by Peter Huene

For the dwTypeparameter to RegSetValueEx, you should be using REG_DWORDinstead of REG_SZ.

对于dwType参数 to RegSetValueEx,您应该使用REG_DWORD代替REG_SZ

You should also be passing a valid pointer to a DWORD for the lpDataparameter.

您还应该为lpData参数传递一个指向 DWORD 的有效指针。

回答by shf301

Change your REG_SZ parameter to REG_DWORD. That parameter specifies the type of the value that will be written to the registry.

将 REG_SZ 参数更改为 REG_DWORD。该参数指定将写入注册表的值的类型。

See http://msdn.microsoft.com/en-us/library/ms724884(v=vs.85).aspxfor a full list of types.

有关类型的完整列表,请参阅http://msdn.microsoft.com/en-us/library/ms724884(v=vs.85).aspx