C++如何读写注册表

1.如何向注册表中写值

//写入注册表为DWORD类型
bool SetRegValue(LPCTSTR lpSubKey, LPCTSTR lpValueName, DWORD dwValueKey)
{
	bool bRet = false;
	HKEY hKey;
	DWORD dwDispoistion = REG_OPENED_EXISTING_KEY;

	if (ERROR_SUCCESS == ::RegCreateKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &hKey, &dwDispoistion))
	{
		if (ERROR_SUCCESS == ::RegSetValueEx(hKey, lpValueName, 0, REG_DWORD, (BYTE*)&dwValueKey, sizeof(DWORD)))
		{
			bRet = true;
		}
	}
	::RegCloseKey(hKey);
	return bRet;
}


// 写入注册表为LPCTSTR 类型
bool SetRegValue(LPCTSTR lpSubKey, LPCTSTR lpValueName, LPCTSTR lpValueKey)
{
	bool bRet = false;
	HKEY hKey;
	DWORD dwDisposition = REG_OPENED_EXISTING_KEY;

	if (ERROR_SUCCESS == ::RegCreateKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &hKey, &dwDisposition))
	{
		if (ERROR_SUCCESS == ::RegSetValueEx(hKey, lpValueName, 0, REG_SZ, (BYTE*)lpValueKey,
			_tcslen(lpValueKey) * sizeof(TCHAR)))
		{
			bRet = true;
		}
	}
	::RegCloseKey(hKey);
	return bRet;
}
注:其中上面HKEY_LOCAL_MACHINE代表注册表中 众多主目录中的一个目录,也可以是其他主目录



写注册表示例如下:

//写入DWORD类型
SetRegValue(L"SYSTEM\\CurrentControlSet\\Services\\USBSTOR",	L"Start", 8);
//写入LPCTSTR类型
LPCTSTR lpSubKey = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\RoomManagement";
LPCTSTR lpValue = L"hello world";
bool bRet = SetRegValue(lpSubKey, L"YourKey", lpValue); //参数二三分别代表要写入的 键和值



2.如何读取注册表的值

// 读注册表的值(output --- wstring)
bool GetRegValue(HKEY hMainKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, std::wstring& wstrReturn/*out*/)
{
	bool bRet = false;
	HKEY hKey;
	if (ERROR_SUCCESS == ::RegOpenKeyEx(hMainKey, lpSubKey, 0, KEY_READ, &hKey))
	{
		DWORD dwType = REG_SZ;
		TCHAR szReturnValue[MAX_PATH + 1] = { 0 };
		DWORD dwSize;
		if (ERROR_SUCCESS == ::RegQueryValueEx(hKey, lpValueName, NULL, &dwType, (LPBYTE)szReturnValue, &dwSize))
		{
			wstrReturn = szReturnValue;
			bRet = true;
		}
	}
	::RegCloseKey(hKey);
	return bRet;
}

// 读出册表(output --- string)
bool GetRegValue(LPCTSTR lpSubKey, LPCTSTR lpValueName, std::string& strReturn/*out*/)
{
	bool bRet = false;
	HKEY hKey;
	if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_READ, &hKey))
	{
		DWORD dwType = REG_SZ;
		TCHAR szReturnValue[MAX_PATH + 1] = { 0 };
		DWORD dwSize;
		if (ERROR_SUCCESS == ::RegQueryValueEx(hKey, lpValueName, NULL, &dwType, (LPBYTE)szReturnValue, &dwSize))
		{
			// 将宽字节转换成多字节
			int dwBufSize = ::WideCharToMultiByte(CP_OEMCP, 0, szReturnValue, -1, NULL, 0, NULL, FALSE);
			char *buff = new char[dwBufSize];
			memset(buff, 0, dwBufSize);
			if (::WideCharToMultiByte(CP_OEMCP, 0, szReturnValue, -1, buff, dwBufSize, NULL, FALSE))
			{
				strReturn = buff;
				delete[] buff;
				buff = NULL;
			}
			bRet = true;
		}
	}
	::RegCloseKey(hKey);
	return bRet;
}


读注册表示例如下:

//读取wstring类型
LPCTSTR lpSubkey = _T("Software\\Microsoft\\Windows\\CurrentVersion\\YourFileName");
wstring	wsValue;
GetRegValue(HKEY_CURRENT_USER, lpSubkey, _T("YourKey"), wsValue);
//读取string类型
string strValue;
LPCTSTR lpSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\YourFileName");
GetRegValue(lpSubKey, _T("YourKey"), strValue);



你可能感兴趣的:(C/C++)