在注册表中设置 DWORD 值 [英] Setting a DWORD value in the registry

查看:90
本文介绍了在注册表中设置 DWORD 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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:键已经存在,值为 1,所以我试图用值 0 覆盖它(我没有创建一个新的).

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).

推荐答案

最大的错误在 (const BYTE*)0x00: you are cast 0x00 to a BYTE *>,这意味着基本上你正在传递一个 NULL 指针.相反,您应该创建一个 DWORD 变量,将要存储在注册表中的值放入其中并传递一个指向它的指针而不是 0x00.

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

此外,如果要存储 DWORD 值,则必须将 REG_SZ 更改为 REG_DWORD,否则将 DWORD> 将被解释为一个(有点奇怪的)字符串.

Also, you must change REG_SZ to REG_DWORD if you want to store a DWORD value, otherwise the DWORD will be interpreted as a (somewhat strange) string.

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.

如果您检查了错误代码,您会立即注意到失败的是 RegSetValueEx 函数,并且错误代码可能类似于无效参数"之类的东西,这会指向您正确的方向.

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

这篇关于在注册表中设置 DWORD 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆