C++读取注册表信息与写入信息到注册表
//读注册表 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”)); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |