access registry

本文详细介绍了如何使用Windows API函数操作注册表,包括打开关键、读取值及类型等步骤,并通过示例代码展示了具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Step1. we must know the path of the key in registry.


Step2. use function “RegOpenKeyEx”to open thespecified key.

Example: open “11Game”:

      

       HKEY hKey;
       LONG lRes =RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\11Game",0, KEY_READ, &hKey);
       boolbExistsAndSuccess (lRes == ERROR_SUCCESS);
       printf ("ExistsAndSuccess : %d\n",bExistsAndSuccess);

Step3: use function “RegQueryValueEx”to retrieves the type and data for the specified value name associated with anopen registry key.

Example:get the value of “version”in the key “11Game”:


/
std::wstring strValueOfVersion;
GetStringRegKey(hKey, L"Version",strValueOfVersion, L"bad");
LONG GetStringRegKey(HKEY hKey, conststd::wstring &strValueName, std::wstring &strValue, const std::wstring &strDefaultValue)
{
       strValue = strDefaultValue;
       WCHAR szBuffer[512];
       DWORD dwBufferSize = sizeof(szBuffer);
       ULONG nError;
       nError =RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, (LPBYTE)szBuffer,&dwBufferSize);
 
       if(ERROR_SUCCESS == nError)
       {
              strValue = szBuffer;
       }
       returnnError;
}


Step4.go to

http://msdn.microsoft.com/en-us/library/ms724911(v=vs.85).aspx

for get more functions.

code display:


#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
LONG GetDWORDRegKey(HKEY hKey, const std::wstring &strValueName, DWORD &nValue, DWORD nDefaultValue)
{
	nValue = nDefaultValue;
	DWORD dwBufferSize(sizeof(DWORD));
	DWORD nResult(0);
	LONG nError = ::RegQueryValueExW(hKey,
		strValueName.c_str(),
		0,
		NULL,
		reinterpret_cast<LPBYTE>(&nResult),
		&dwBufferSize);
	if (ERROR_SUCCESS == nError)
	{
		nValue = nResult;
	}
	return nError;
}


LONG GetBoolRegKey(HKEY hKey, const std::wstring &strValueName, bool &bValue, bool bDefaultValue)
{
	DWORD nDefValue((bDefaultValue) ? 1 : 0);
	DWORD nResult(nDefValue);
	LONG nError = GetDWORDRegKey(hKey, strValueName.c_str(), nResult, nDefValue);
	if (ERROR_SUCCESS == nError)
	{
		bValue = (nResult != 0) ? true : false;
	}
	return nError;
}


LONG GetStringRegKey(HKEY hKey, const std::wstring &strValueName, std::wstring &strValue, const std::wstring &strDefaultValue)
{
	strValue = strDefaultValue;
	WCHAR szBuffer[512];
	DWORD dwBufferSize = sizeof(szBuffer);
	ULONG nError;
	nError = RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, (LPBYTE)szBuffer, &dwBufferSize);

	if (ERROR_SUCCESS == nError)
	{
		strValue = szBuffer;
	}
	return nError;
}

std::string ws2s(const std::wstring& ws)
{
	std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
	setlocale(LC_ALL, "chs");
	const wchar_t* _Source = ws.c_str();
	size_t _Dsize = 2 * ws.size() + 1;
	char *_Dest = new char[_Dsize];
	memset(_Dest,0,_Dsize);
	wcstombs(_Dest,_Source,_Dsize);
	std::string result = _Dest;
	delete []_Dest;
	setlocale(LC_ALL, curLocale.c_str());
	return result;
}
std::wstring s2ws(const std::string& s)
{
	setlocale(LC_ALL, "chs");
	const char* _Source = s.c_str();
	size_t _Dsize = s.size() + 1;
	wchar_t *_Dest = new wchar_t[_Dsize];
	wmemset(_Dest, 0, _Dsize);
	mbstowcs(_Dest,_Source,_Dsize);
	std::wstring result = _Dest;
	delete []_Dest;
	setlocale(LC_ALL, "C");
	return result;
}
void main () {
	HKEY hKey;
	LONG lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\11Game", 0, KEY_READ, &hKey);
	bool bExistsAndSuccess (lRes == ERROR_SUCCESS);
	printf ("ExistsAndSuccess : %d\n",bExistsAndSuccess);
	bool bDoesNotExistsSpecifically (lRes == ERROR_FILE_NOT_FOUND);
	printf ("DoesNotExistsSpecifically : %d\n",bDoesNotExistsSpecifically);
	std::wstring strValueOfPathDir;
	std::wstring strValueOfVersion;
	std::wstring strKeyDefaultValue;
	GetStringRegKey(hKey, L"path", strValueOfPathDir, L"bad");
	GetStringRegKey(hKey, L"Version", strValueOfVersion, L"bad");
	GetStringRegKey(hKey, L"", strKeyDefaultValue, L"bad");
	std::cout << "strValueOfBinDir:"<<ws2s(strValueOfPathDir) << std::endl;
	std::cout << "strValueOfVersion:"<<ws2s(strValueOfVersion) << std::endl;
	std::cout << "strKeyDefaultValue:"<<ws2s(strKeyDefaultValue) << std::endl;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值