C++如何读写注册表
2017-09-14 20:21
323 查看
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;
}
读注册表示例如下:
//读取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);
//写入注册表为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++代码读写数据库
- 在C++中如何实现文件的读写?
- 转载 在C++中如何实现文件的读写?
- 文件读取C++文件读写操作(三)如何统计文本的行数及如何读取文件某一行内容
- 在C++中如何实现文件的读写
- 在C++中如何实现文件的读写?
- 在C++中如何实现文件的读写?
- VB中如何实现注册表的读写
- 你好,C++(5)如何输出数据到屏幕、从屏幕输入数据与读写文件?
- c++添加开机启动项,win32 api操作注册表,读写注册表
- C++读写注册表的参考网址
- .Net中如何限制执行程序只能运行一次和如何读写注册表
- C/C++ 关于如何读写ini配置文件
- C++读写注册表
- C++读写注册表
- c++和c读写文件的悲剧如何发生的
- C语言如何读写注册表
- C++里如何读写xls文件
- C++ 读写注册表