简体   繁体   English

在注册表中设置DWORD值

[英]Setting a DWORD value in the registry

I'm trying to set a DWORD value in the registry. 我正在尝试在注册表中设置DWORD值。 I made it work with a text value, but now I want to set another value with a numeric one(0). 我使用文本值,但现在我想用数字(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: 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覆盖它(我没有创建一个新的)。

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. 最大的错误是(const BYTE*)0x00 :你将0x00转换为BYTE * ,这意味着基本上你传递一个NULL指针。 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变量,将要存储的值放入其中的注册表中,并将指针传递给它而不是0x00

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. 此外,如果要存储DWORD值,则必须将REG_SZ更改为REG_DWORD ,否则DWORD将被解释为(有点奇怪)字符串。

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 RegSetValueEx function that fails, and the error code may have been something like "invalid parameter", that would have pointed you in the right direction. 如果您检查了错误代码,您会立即注意到它是RegSetValueEx函数失败,并且错误代码可能类似于“无效参数”,这会指向正确的方向。

For the dwType parameter to RegSetValueEx , you should be using REG_DWORD instead of REG_SZ . 对于dwType参数RegSetValueEx ,你应该使用REG_DWORD而不是REG_SZ

You should also be passing a valid pointer to a DWORD for the lpData parameter. 您还应该将有效指针传递给lpData参数的DWORD。

Change your REG_SZ parameter to REG_DWORD. 将REG_SZ参数更改为REG_DWORD。 That parameter specifies the type of the value that will be written to the registry. 该参数指定将写入注册表的值的类型。

See http://msdn.microsoft.com/en-us/library/ms724884(v=vs.85).aspx for a full list of types. 有关完整的类型列表,请参见http://msdn.microsoft.com/en-us/library/ms724884(v=vs.85).aspx

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

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