windows 浅析-读写注册表
简单叙述一下windows注册表的使用:
本文叙述问宽字版,一般逻辑为:
1、
RegOpenKeyExW---------->RegCreateKeyW---------------->RegSetValueExW
打开注册表 创建key 写入键值对
2、
RegOpenKeyExW---------------->RegSetValueExW
打开注册表 写入键值对
3、
RegOpenKeyExW---------->RegDeleteValueW
打开注册表 删除键值
4、
RegOpenKeyExW---------->RegQueryValueExW
打开注册表 查询键值对
api解析搬运msdn:
打开注册表
LSTATUS RegOpenKeyExW( HKEY hKey,
LPCWSTR lpSubKey,
DWORD ulOptions,
REGSAM samDesired,
PHKEY phkResult );
创建键值
LSTATUS RegCreateKeyExW( HKEY hKey,
LPCWSTR lpSubKey,
DWORD Reserved,
LPWSTR lpClass,
DWORD dwOptions,
REGSAM samDesired,
const LPSECURITY_ATTRIBUTES lpSecurityAttributes,
PHKEY phkResult,
LPDWORD lpdwDisposition );
查询键值
LSTATUS RegQueryValueExW( HKEY hKey,
LPCWSTR lpValueName,
LPDWORD lpReserved,
LPDWORD lpType,
LPBYTE lpData,
LPDWORD lpcbData );
删除键值
LSTATUS RegDeleteValueW( HKEY hKey,
LPCWSTR lpValueName );
此处举例子创建:【其他操作大同小异,注意异常以及对应字符串长度即可.】
HKEY hKey = 0;
DWORD nLength = 0;
WCHAR value[] = { _T("fuck233333\0") };
DWORD ret = ::RegOpenKeyExW(HKEY_CURRENT_USER, _T("Software\\Tencent\\QQ"), 0, KEY_SET_VALUE, &hKey);
if (ret != ERROR_SUCCESS)
{
cout << "open regedit error with: " << GetLastError() << endl;
return 0;
}
ret = ::RegCreateKeyW(hKey, _T("Alves"), &hKey);
if (ret)
{
std::cout << "add key error with:" << ret << std::endl;
}
nLength = (lstrlen(value) + 1) * sizeof(WCHAR);;
ret = RegSetValueExW(hKey,
_T("debugSwitch"),
0,
REG_SZ,
(BYTE*)(value),
nLength);
if (ret)
{
std::cout << "add key error with:" << ret << std::endl;
}
return 0;