//读注册表
std::wstring GetRegValue(HKEY hKeyType, DWORD dwType, LPCTSTR lpPath, LPCTSTR lpName)
{
HKEY hKEY;
DWORD dataSize = MAX_PATH;
char data[MAX_PATH];
std::string strValue("");
if (RegOpenKeyEx(hKeyType, lpPath, NULL, KEY_READ, &hKEY) == ERROR_SUCCESS) //如果无法打开hKEY,则中止程序的执行
{
long lRet = RegQueryValueEx(hKEY, lpName, NULL, &dwType, (LPBYTE)data, &dataSize);
if (lRet == ERROR_SUCCESS)
{
for (int i = 0; i < (int)dataSize; i++)
{
strValue = strValue + data[i];
}
}
RegCloseKey(hKEY); // 程序结束前要关闭已经打开的 hKEY。
}
else
{
RegCreateKeyEx(hKeyType, (LPCTSTR)lpPath, 0, NULL, NULL, KEY_WRITE, NULL, &hKEY, NULL);
RegCloseKey(hKEY); // 程序结束前要关闭已经打开的 hKEY。
}
std::wstring wstrValue((wchar_t*)strValue.data(), strValue.length() / 2);
return wstrValue;
}
eg:
std::wstring strValue;
strValue = GetRegValue(HKEY_LOCAL_MACHINE, REG_SZ, L"SoftWare\\Microsoft\\Windows\\CurrentVersion\\App Paths\\WebService", L"WebServiceIP");
//写注册表
void WriteRegedit(LPCTSTR m_path, LPCTSTR m_name, CString strParame)
{
HKEY hKEY;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, m_path, NULL, KEY_WRITE, &hKEY) == ERROR_SUCCESS) //如果无法打开hKEY,则中止程序的执行
{
RegSetValueEx(hKEY, m_name, NULL, REG_SZ, (BYTE*)strParame.GetBuffer(strParame.GetLength()), 2 * strParame.GetLength());
strParame.ReleaseBuffer();
RegCloseKey(hKEY);
}
else
{
RegCreateKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)m_path, 0, NULL, NULL, KEY_WRITE, NULL, &hKEY, NULL);
RegSetValueEx(hKEY, m_name, NULL, REG_SZ, (BYTE*)strParame.GetBuffer(strParame.GetLength()), 2 * strParame.GetLength());
strParame.ReleaseBuffer();
RegCloseKey(hKEY);
}
}
eg:
m_RegInfo.WriteRegedit(_T("SoftWare\\Microsoft\\Windows\\CurrentVersion\\App Paths\\WebService"), _T("WebServiceIP"), _T("192.168.0.0"));
//获取注册表子项的代码
以下部分内容转自:
http://blog.youkuaiyun.com/helonsy/article/details/7007873
//QueryKey - Enumerates the subkeys of key and its associated values.
//hKey - Key whose subkeys and values are to be enumerated.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
//枚举一个键的子键和子键的值
void QueryKey(HKEY hKey)
{
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
DWORD i, retCode;
TCHAR achValue[MAX_VALUE_NAME];
DWORD cchValue = MAX_VALUE_NAME;
// Get the class name and the value count.
retCode = RegQueryInfoKey(
hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
// Enumerate the subkeys, until RegEnumKeyEx fails.
//枚举子项
if (cSubKeys)
{
//printf("\nNumber of subkeys: %d\n", 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)
{
_tprintf(TEXT("(%d) %s\n"), i + 1, achKey);//achKey子项的名称
}
}
}
// Enumerate the key values.
// 枚举键值
if (cValues)
{
printf("\nNumber of values: %d\n", cValues);
for (i = 0, retCode = ERROR_SUCCESS; i < cValues; i++)
{
cchValue = MAX_VALUE_NAME;
achValue[0] = '\0';
retCode = RegEnumValue(hKey, i,
achValue,
&cchValue,
NULL,
NULL,
NULL,
NULL);
if (retCode == ERROR_SUCCESS)
{
_tprintf(TEXT("(%d) %s\n"), i + 1, achValue);
}
}
}
}
//调用部分
void main()
{
HKEY hTestKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths"),
0, KEY_READ, &hTestKey) == ERROR_SUCCESS)
{
QueryKey(hTestKey, vecSubkeyName);
}
RegCloseKey(hTestKey);
}
412

被折叠的 条评论
为什么被折叠?



