设置两个注册表项 [英] Setting Two Registry Keys

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

问题描述

我正在尝试一次设置两个注册表项,而无需两次运行此代码.我怎样才能做到这一点?谢谢!

I''m trying to set two registry keys at once without running this code twice. How can I do this? Thanks!

HKEY hKey; DWORD buffersize = 1024; DWORD dwErr = NO_ERROR; 
#define BUFFER_SIZE 1024
dwErr = RegOpenKeyEx (HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3",NULL,KEY_SET_VALUE|KEY_QUERY_VALUE,&hKey);
fwErr = RegOpenKeyEx (HKEY_LOCAL_MACHINE,"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 = RegQueryValueExW(hKey,L"1803",NULL,&dwType,data,&buffersize);
	if(ERROR_SUCCESS == dwErr && REG_DWORD == dwType)
		{
		*((LPDWORD)data) = 3; //0 is Enable 3 is Disable
		dwErr = RegSetValueExW(hKey,L"1803",0,REG_DWORD,data,sizeof(DWORD));
		}
		RegCloseKey (hKey);
		SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)_T("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3"));
	}

推荐答案

您需要掌握诸如带参数的函数"这样的基本抽象方法.注册表项和要设置的值应作为参数传递给代码.

您可以在此片段中使用OS API和硬编码的立即常量来显示此代码(通常这确实很糟糕),但未显示单个函数,这看起来很令人不安.看来您以非常不成比例的方式发展了编程技能.您已经对Windows API有了足够的了解,而没有对如何编写代码的基本了解.你真的很着急吗?那么,您的活动是否有用值得怀疑.

您声称您使用的是C ++,但实际上,不仅您没有使用OOP,还需要熟悉编程的基础知识,这些基础知识要比OOP和其他高级主题要好得多.我建议您在使用OS API,UI等启动任何程序之前,全面学习C ++并在实践中使用基本语言功能,而无需跳过任何主题.

—SA
You need to master such a basic method of abstraction as "function with parameters". The registry keys and a values to set should be passed as parameters to the code.

The fact you show this code with access to OS API and immediate constants hard-coded in this fragment (which is really bad in general) but without showing a single function looks disturbing. It looks like you develop your programming skills in a very disproportional manner. You''re getting deep enough into Windows API without basic understanding of how to write code. Are you really in such rush? Then the usefulness of your activity is questionable.

You claim you''re using C++, but in fact not only you''re not using OOP, you need to get familiar with the basics of programming which go well before OOP and other advanced topics. I would advise to go along the full course of C++ and using of basic language features in practice, without skipping of any topics, before you start any program using OS APIs, UI and the like.

—SA


这行吗?
HKEY hKey; DWORD buffersize = 1024;
DWORD dwErr = NO_ERROR;
#define BUFFER_SIZE 1024
dwErr = RegOpenKeyEx HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3",NULL,KEY_SET_VALUE|KEY_QUERY_VALUE,&hKey)&& RegOpenKeyEx (HKEY_LOCAL_MACHINE,"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 = RegQueryValueExW(hKey,L"1803",NULL,&dwType,data,&buffersize);
	if(ERROR_SUCCESS == dwErr && REG_DWORD == dwType && *((LPDWORD)data) != 0)	
	{
	*((LPDWORD)data) = 0; //0 is Enable 3 is Disable
	dwErr = RegSetValueExW(hKey,L"1803",0,REG_DWORD,data,sizeof(DWORD));
	}
	RegCloseKey (hKey);
SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)_T    ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3"));
SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)_T("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3"));
}


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

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