Windows平台查询、修改注册表的方式如下:已测
#pragma once #include "stdafx.h" #include <iostream> //#include <Windows.h> #include <afx.h> # include <stdio.h> # include <tchar.h> # include <locale.h> #include <time.h> #include <fstream> #include <queue> #include <string> #define MAX_KEY_LENGTH 255 #define MAX_VALUE_NAME 16383 using namespace std; class SetReg { public: SetReg(); ~SetReg(); public: //test读取某个键的值 char* read_subitem(CString keyStr); //打开注册表 LONG OpenReg(const wchar_t* path, HKEY& retKey, HKEY rootKey = HKEY_CURRENT_USER); //查询注册表中的键和值 string QueryRegKeyValu(const wchar_t* path, HKEY rootKey = HKEY_CURRENT_USER); //查询注册表中的键和值 bool QueryRegKey(const wchar_t* path, HKEY rootKey = HKEY_CURRENT_USER); //设置某个键的值 int SetRegKeyValue(const char* subKey, const char* Value, const wchar_t* path, HKEY rootKey = HKEY_CURRENT_USER); private: std::string getKeyValue(std::string sKey, std::string sValue, int indexSrc, int indexDst); CString pChar2CStr(const char* pChar); private: const HKEY m_RootKey = HKEY_CURRENT_USER; public: LPCTSTR data_Set = L"Software\\SinoWave\\TermInfo"; private: Json::FastWriter writer; Json::Value Info; Json::Value array; Json::Value data; Json::Value root; std::queue<std::wstring> keystack; //定义获取的类型 DWORD dwType = REG_BINARY | REG_DWORD | REG_EXPAND_SZ | REG_MULTI_SZ | REG_NONE | REG_SZ; TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name DWORD cbName; // size of name string TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name DWORD cchClassName = MAX_PATH; // size of class string DWORD cSubKeys = 0; // number of subkeys DWORD cbMaxSubKey; // longest subkey size DWORD cchMaxClass; // longest class string DWORD cValues; // number of values for key DWORD cchMaxValue; // longest value name DWORD cbMaxValueData; // longest value data DWORD cbSecurityDescriptor; // size of security descriptor FILETIME ftLastWriteTime; // last write time //LPCSTR achValue; TCHAR achValue[MAX_VALUE_NAME]; DWORD cchValue = MAX_VALUE_NAME; std::string strResult; //需要查询的 string regList[22] = { "AudioCaptureType","AudioQulityEx","UseLocalSoftAec", "Nslevel","TransmitProtocol","VideoCodecType","ForceUseFEC", "HWVATypeDecode","UseHevcdecoder","VideoDisplayMode", "DrawSelectRectAlways","CapHeight","CapWidth","LocalVideoScale", "BandWidth","FrameRate","TitleColor","writelogfile","DrawVideoStyle", "AecMode","UseInterlaceFlag","DisplayFrameRate" }; };
#include "SetReg.h"
SetReg::SetReg() :strResult(""), data_Set ( L"Software\\SinoWave\\TermInfo")
{
}
SetReg::~SetReg()
{
data_Set = NULL;
}
//子健获取
char* SetReg::read_subitem(CString keyStr)
{
std::cout << "---读取子键---" << std::endl;
HKEY cpp_key = NULL; //保存注册表的句柄
DWORD dwIndexs = 0; //需要返回子项的索引
TCHAR keyName[MAX_PATH] = { 0 }; //保存子键的名称
DWORD charLength = 256; //想要读取多少字节并返回实际读取到的字符长度
char dwValue[256];
DWORD dwSzType = REG_SZ;
DWORD dwSize = sizeof(dwValue);
LPWSTR lpName;
long ret1 = OpenReg(data_Set, cpp_key);
if (ERROR_SUCCESS == ret1)
{
//以下两种都是获取键值的方式
ret1 = ::RegQueryValueEx(cpp_key, keyStr, 0, &dwSzType, (LPBYTE)&dwValue, &dwSize);
//ret1 = RegGetValue(HKEY_CURRENT_USER, data_Set, _T("AecMode"), RRF_RT_REG_SZ, &dwSzType, dwValue, &dwSize);
if (ret1 == ERROR_SUCCESS)
{
std::cout << "注册表读取成功" << std::endl;
std::cout << "读取到的值得地址" << &dwValue << std::endl;
std::cout << "读取到的值:";
for (int i = 0; i < sizeof(dwValue); i++)
{
std::cout << dwValue[i];
if (dwValue[i] == NULL)
break;
}
std::cout << std::endl;
}
else {
std::cout << "注册表读取失败" << std::endl;
std::cout << "读取到的值得地址" << &dwValue << std::endl;
if (cpp_key != NULL)
{
RegCloseKey(cpp_key);//关闭句柄
}
const char* retStr = "not found key";
return (char*)retStr;
}
}
else {
std::cout << "打开注册表失败!" << std::endl;
}
if (cpp_key != NULL)
{
RegCloseKey(cpp_key);//关闭句柄
}
std::cout << "---读取子键结束---" << std::endl;
return dwValue;
}
//无用
char* TCHARToChar(const TCHAR* pTchar)
{
char* pChar = NULL;
#ifdef _UNICODE
int nLen = wcslen(pTchar) + 1;
pChar = new char[nLen * 2];
WideCharToMultiByte(CP_ACP, 0, pTchar, nLen, pChar, 2 * nLen, NULL, NULL);
#else
int nLen = strlen(pTchar) + 1;
pChar = new char[nLen];
memcpy(pChar, pTchar, nLen * sizeof(char));
#endif
return pChar;
}
LONG SetReg::OpenReg(const wchar_t* path, HKEY& retKey, HKEY rootKey)
{
//尝试打开注册表,‘HKEY_LOCAL_MACHINE’要打开的根表;‘cpp_data’要打开的子表项;‘0’固定值;‘KEY_ALL_ACCESS’申请的权限;‘&cpp_key’返回句柄;
long ret1 = RegOpenKeyEx(rootKey, path, 0, KEY_READ | KEY_ALL_ACCESS, &retKey);
if (ret1 != ERROR_SUCCESS)
{
RegCloseKey(retKey);
retKey = NULL;
return ret1;
}
//RegCloseKey(retKey);
return ret1;
}
#include <atlconv.h>
bool SetReg::QueryRegKey(const wchar_t* path, HKEY rootKey = HKEY_CURRENT_USER)
{
HKEY hKey;
DWORD i, retCode;
BOOL ret = FALSE;
std::string key_ValStr;
string strRet = "";
long ret1 = OpenReg(path, hKey, rootKey);
if (ret1 != ERROR_SUCCESS)
{
if (hKey != NULL)
RegCloseKey(hKey);
ret = FALSE;
return ret;
}
retCode = RegQueryInfoKey(hKey, achClass, &cchClassName, NULL,
&cSubKeys, &cbMaxSubKey, &cchMaxClass, &cValues, &cchMaxValue,
&cbMaxValueData, &cbSecurityDescriptor, &ftLastWriteTime
);
// Enumerate the subkeys, until RegEnumKeyEx fails.
if (cSubKeys)
{
for (i = 0; i < cSubKeys; i++)
{
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey, i, achKey, &cbName, NULL, NULL, NULL, &ftLastWriteTime);
if (retCode == ERROR_SUCCESS)
{
//use achKey to build new path and input it into stack.
std::wstring newPath = L"";
newPath.append(path);
newPath.append(L"\\");
newPath.append(achKey);
keystack.push(newPath);
}
}
}
// Enumerate the key values.
if (cValues)
{
for (i = 0, retCode = ERROR_SUCCESS; i < cValues; i++)
{
cchValue = MAX_VALUE_NAME;
achValue[0] = '\0';
unsigned char vari[70];
retCode = RegEnumValue(hKey, i, achValue, &cchValue, NULL, NULL, NULL, NULL);
if (retCode == ERROR_SUCCESS)
{
TCHAR szBuffer[255] = { 0 };
DWORD dwNameLen = 255;
retCode = RegQueryValueEx(hKey, achValue, 0, &dwType, (LPBYTE)szBuffer, &dwNameLen);
if (retCode == ERROR_SUCCESS)
{
ret = TRUE;
}
else
{
ret = FALSE;
}
}
else
{
ret = FALSE;
}
}
}
return ret;
}
string SetReg::QueryRegKeyValu(const wchar_t* path, HKEY rootKey)
{
HKEY hKey;
DWORD i, retCode;
std::string key_ValStr;
string strRet = "";
long ret1 = OpenReg(path, hKey, rootKey);
if (ret1 != ERROR_SUCCESS)
{
if(hKey!=NULL)
RegCloseKey(hKey);
return "Open Reg Failed";
}
/**
key handle
buffer for class name
size of class string
reserved
number of subkeys
longest subkey size
longest class string
number of values for this key
longest value name
longest value data
security descriptor
last write time
**/
retCode = RegQueryInfoKey(hKey, achClass, &cchClassName, NULL,
&cSubKeys, &cbMaxSubKey, &cchMaxClass, &cValues, &cchMaxValue,
&cbMaxValueData, &cbSecurityDescriptor, &ftLastWriteTime
);
// Enumerate the subkeys, until RegEnumKeyEx fails.
if (cSubKeys)
{
for (i = 0; i < cSubKeys; i++)
{
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey, i, achKey, &cbName, NULL, NULL, NULL, &ftLastWriteTime);
if (retCode == ERROR_SUCCESS)
{
//use achKey to build new path and input it into stack.
std::wstring newPath = L"";
newPath.append(path);
newPath.append(L"\\");
newPath.append(achKey);
keystack.push(newPath);
}
}
}
// Enumerate the key values.
if (cValues)
{
for (i = 0, retCode = ERROR_SUCCESS; i < cValues; i++)
{
cchValue = MAX_VALUE_NAME;
achValue[0] = '\0';
unsigned char vari[70];
retCode = RegEnumValue(hKey, i, achValue, &cchValue, NULL, NULL, NULL, NULL);
if (retCode == ERROR_SUCCESS)
{
TCHAR szBuffer[255] = { 0 };
DWORD dwNameLen = 255;
retCode = RegQueryValueEx(hKey, achValue, 0, &dwType, (LPBYTE)szBuffer, &dwNameLen);
if (retCode == ERROR_SUCCESS)
{
if (dwType == REG_DWORD) //REG_DWORD
{
DWORD dwRet = 0;
retCode = RegQueryValueEx(hKey, achValue, 0, &dwType, (LPBYTE)dwRet, &dwNameLen);
if (retCode == ERROR_SUCCESS)
{
USES_CONVERSION; //这两句也要加入2
char *szBuffer1 = W2A(achValue);//这两句也要加入3
}
}
else if (dwType == REG_SZ)//REG_SZ
{
//查询子表中有多少键值对
USES_CONVERSION; //这两句也要加入2
char *szBuffer1 = W2A(achValue);//这两句也要加入3
//轮询数组
int length = sizeof(regList) / sizeof(regList[0]);
for (int i=0;i< length;i++)
{
if (szBuffer1 == regList[i])
{
char *szBuffer2 = W2A(szBuffer);
//getKeyValue(szBuffer1, szBuffer2, i, cValues);
Info["regKey"] = szBuffer1;
Info["regVal"] = szBuffer2;
array.append(Info);
break;
}
}
}
}
else
{
strResult = "{ \"command\":1202,\"data\" : {\"sType\":3,\"retVlue\":-1,\"Msg\" : \"查询注册表参数失败\"} }";
}
}
}//for
if (retCode == ERROR_SUCCESS)
{
data["config"] = Json::Value(array);
data["sType"] = 0;
root["data"] = data;
root["command"] = "1202";
strResult = writer.write(root);
}
}
#if 0
ofstream out("regJson.txt");
if (out.is_open())
{
out << strResult.c_str();
out << "";
out.flush();
out.close();
}
#endif
if (hKey != NULL)
RegCloseKey(hKey);
return strResult;
}
int SetReg::SetRegKeyValue(const char* subKey, const char* Value, const wchar_t* path, HKEY rootKey)
{
int ret = -1;
HKEY hKey = NULL;
LPCWSTR dValue = (LPCWSTR)Value;
BYTE* lpb = (BYTE*)dValue;
DWORD length = sizeof(lpb);
CString subKey1 = pChar2CStr(subKey);
path = data_Set;
long ret1 = OpenReg(path, hKey, rootKey);
if (ret1 != ERROR_SUCCESS)
{
if(hKey!=NULL)
RegCloseKey(hKey);
ret = -1;
}
else
{
ret1 = ::RegSetValueEx(hKey, subKey1, NULL, REG_SZ, lpb, length);
if (ret1 != ERROR_SUCCESS)
{
::RegCloseKey(hKey);
ret = -1;
}
else
{
RegCloseKey(hKey);
hKey = NULL;
ret = 0;
}
}
return ret;
}
/**
* 拼接string
*/
std::string SetReg::getKeyValue(std::string sKey, std::string sValue, int indexSrc, int indexDst)
{
char szDoubleQutoes[] = "\"";
char szDoubleQutoes1[] = "\"";
char szColon[] = ":";
if (strResult.find("{") == 0)
{
}
else
{
strResult.append("{");
}
if (!sKey.empty() && !sValue.empty())
{
strResult.append(szDoubleQutoes);
strResult.append(sKey);
strResult.append(szDoubleQutoes1);
strResult.append(szColon);
strResult.append(szDoubleQutoes);
strResult.append(sValue);
strResult.append(szDoubleQutoes1);
if ((indexDst - indexSrc) != 1)
strResult.append(",");
if ((indexDst - indexSrc) == 1)
{
strResult.append("}");
}
}
return strResult;
}
CString SetReg::pChar2CStr(const char* pChar)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const char* _Source = pChar;//s.c_str();
size_t converted = 0;
size_t _Dsize = strlen(pChar) + 1; //s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs_s(&converted, _Dest, _Dsize, _Source, _TRUNCATE);
std::wstring result = _Dest;
delete[]_Dest;
setlocale(LC_ALL, curLocale.c_str());
return CString(result.c_str());
}