//读注册表
std::wstring CRegInfo::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,&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,KEY_WRITE,&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"SoftWareMicrosoftwindowsCurrentVersionApp PathsWebService",L"WebServiceIP");
//写注册表
voID WriteRegedit(LPCTSTR m_path,LPCTSTR m_name,CString strParame)
{
HKEY hKEY;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,m_path,则中止程序的执行
{
RegSetValueEx(hKEY,m_name,(BYTE*)strParame.GetBuffer(strParame.GetLength()),2 * strParame.GetLength());
strParame.ReleaseBuffer();
RegCloseKey(hKEY);
}
else
{
RegCreateKeyEx(HKEY_LOCAL_MACHINE,(LPCTSTR)m_path,NulL);
RegSetValueEx(hKEY,2 * strParame.GetLength());
strParame.ReleaseBuffer();
RegCloseKey(hKEY);
}
}
eg:
m_RegInfo.WriteRegedit(_T(“SoftWareMicrosoftwindowsCurrentVersionApp PathsWebService”),_T(“WebServiceIP”),_T(“192.168.0.0”));
总结以上是内存溢出为你收集整理的C++读取注册表信息与写入信息到注册表全部内容,希望文章能够帮你解决C++读取注册表信息与写入信息到注册表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)