设置注册表项问题 [英] Setting A Reg Key Problem

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

问题描述

我正在尝试将注册表键1803设置为3,但遇到一些问题....这些是值.
值名称:1803
数据类型:REG_DWORD(DWORD值)
值数据:(0 =允许下载,3 =禁止下载)

这是我的代码...

I''m trying set a regkey 1803 to 3 and am having some problems....these are the values.
Value Name: 1803
Data Type: REG_DWORD (DWORD Value)
Value Data: (0 = downloads enabled, 3 = downloads disabled)

Here is my code...

#include "stdafx.h"
#include <windows.h>
#include <winreg.h>

#pragma comment(lib,"Advapi32.lib")
//#pragma (dll,Advapi32.dll) What To Do ??????????? 
int main () {
	HKEY hKey;			
	DWORD buffersize = 1024;
	char* lpData = new char[buffersize];

	RegOpenKeyEx (HKEY_CURRENT_USER,L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3",NULL,KEY_SET_VALUE,&hKey);
	RegQueryValueEx(hKey,L"1803",NULL,REG_DWORD,(LPBYTE) lpData,&buffersize);
	RegSetValueExW(hKey,L"1803",REG_DWORD,(LPBYTE) lpData,,3);
	RegCloseKey (hKey);
	delete lpData;
}



这是我的错误...
1 IntelliSense:"int"类型的参数与"LPDWORD"类型的参数不兼容
2 IntelliSense:"LPBYTE"类型的参数与"DWORD"类型的参数不兼容
3 IntelliSense:需要一个表达式



Here are my errors...
1 IntelliSense: argument of type "int" is incompatible with parameter of type "LPDWORD"
2 IntelliSense: argument of type "LPBYTE" is incompatible with parameter of type "DWORD"
3 IntelliSense: expected an expression

推荐答案



您发布的原始代码有很多错误.

1.)您应检查RegOpenKeyEx的返回值是否有ERROR_SUCCESS.
2.)您正在尝试读取注册表值...,但是仅使用KEY_SET_VALUE打开密钥.
3.)当您明显使用恒定的缓冲区大小时,您正在分配动态数组.
4.)一些缺少的功能参数.
5.)您应该使用正确的delete []运算符.

这样的事情应该起作用:

Hi,

Many things are wrong with the original code you posted.

1.) You should check the return value of RegOpenKeyEx for ERROR_SUCCESS.
2.) You are trying to read the registry value... but you open the key with only KEY_SET_VALUE.
3.) You are allocating a dynamic array when your obviously using a constant buffer size.
4.) Some missing function parameters.
5.) You should use the correct delete[] operator.

Something like this should work:

#define BUFFER_SIZE 1024 //add this to your header		
HKEY hKey;			
DWORD dwErr = NO_ERROR;

dwErr = RegOpenKeyEx (HKEY_CURRENT_USER,L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3",NULL,KEY_SET_VALUE|KEY_QUERY_VALUE,&hKey);
if(ERROR_SUCCESS == dwErr)
{
	BYTE data[BUFFER_SIZE] = {0};
	DWORD dwType = REG_NONE;

	dwErr = RegQueryValueEx(hKey,L"1803",NULL,&dwType,data,&buffersize);
	if(ERROR_SUCCESS == dwErr && REG_DWORD == dwType)
	{
		*((LPDWORD)data) = 0;
		dwErr = RegSetValueExW(hKey,L"1803",0,REG_DWORD,data,sizeof(DWORD));
	}
	RegCloseKey (hKey);
}



另外...如果您使用的是64位操作系统并编写32位应用程序,则需要将KEY_WOW64_64KEY添加到RegOpenKeyEx标志中.

最好的祝福,
-大卫·德劳恩(David Delaune)



Also... if you are on a 64 bit operating system and writing a 32 bit application you will need to add KEY_WOW64_64KEY to your RegOpenKeyEx flags.

Best Wishes,
-David Delaune


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

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