Step1. we must know the path of the key in registry.
Step2. use function “RegOpenKeyEx”to open thespecified key.
Example: open “11Game”:
HKEY hKey;
LONG lRes =RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\11Game",0, KEY_READ, &hKey);
boolbExistsAndSuccess (lRes == ERROR_SUCCESS);
printf ("ExistsAndSuccess : %d\n",bExistsAndSuccess);
Step3: use function “RegQueryValueEx”to retrieves the type and data for the specified value name associated with anopen registry key.
Example:get the value of “version”in the key “11Game”:
/
std::wstring strValueOfVersion;
GetStringRegKey(hKey, L"Version",strValueOfVersion, L"bad");
LONG GetStringRegKey(HKEY hKey, conststd::wstring &strValueName, std::wstring &strValue, const std::wstring &strDefaultValue)
{
strValue = strDefaultValue;
WCHAR szBuffer[512];
DWORD dwBufferSize = sizeof(szBuffer);
ULONG nError;
nError =RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, (LPBYTE)szBuffer,&dwBufferSize);
if(ERROR_SUCCESS == nError)
{
strValue = szBuffer;
}
returnnError;
}
Step4.go to
http://msdn.microsoft.com/en-us/library/ms724911(v=vs.85).aspx
for get more functions.
code display:
#include <windows.h> #include <stdlib.h> #include <stdio.h> #include <string> #include <string.h> #include <iostream> LONG GetDWORDRegKey(HKEY hKey, const std::wstring &strValueName, DWORD &nValue, DWORD nDefaultValue) { nValue = nDefaultValue; DWORD dwBufferSize(sizeof(DWORD)); DWORD nResult(0); LONG nError = ::RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, reinterpret_cast<LPBYTE>(&nResult), &dwBufferSize); if (ERROR_SUCCESS == nError) { nValue = nResult; } return nError; } LONG GetBoolRegKey(HKEY hKey, const std::wstring &strValueName, bool &bValue, bool bDefaultValue) { DWORD nDefValue((bDefaultValue) ? 1 : 0); DWORD nResult(nDefValue); LONG nError = GetDWORDRegKey(hKey, strValueName.c_str(), nResult, nDefValue); if (ERROR_SUCCESS == nError) { bValue = (nResult != 0) ? true : false; } return nError; } LONG GetStringRegKey(HKEY hKey, const std::wstring &strValueName, std::wstring &strValue, const std::wstring &strDefaultValue) { strValue = strDefaultValue; WCHAR szBuffer[512]; DWORD dwBufferSize = sizeof(szBuffer); ULONG nError; nError = RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, (LPBYTE)szBuffer, &dwBufferSize); if (ERROR_SUCCESS == nError) { strValue = szBuffer; } return nError; } std::string ws2s(const std::wstring& ws) { std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C"; setlocale(LC_ALL, "chs"); const wchar_t* _Source = ws.c_str(); size_t _Dsize = 2 * ws.size() + 1; char *_Dest = new char[_Dsize]; memset(_Dest,0,_Dsize); wcstombs(_Dest,_Source,_Dsize); std::string result = _Dest; delete []_Dest; setlocale(LC_ALL, curLocale.c_str()); return result; } std::wstring s2ws(const std::string& s) { setlocale(LC_ALL, "chs"); const char* _Source = s.c_str(); size_t _Dsize = s.size() + 1; wchar_t *_Dest = new wchar_t[_Dsize]; wmemset(_Dest, 0, _Dsize); mbstowcs(_Dest,_Source,_Dsize); std::wstring result = _Dest; delete []_Dest; setlocale(LC_ALL, "C"); return result; } void main () { HKEY hKey; LONG lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\11Game", 0, KEY_READ, &hKey); bool bExistsAndSuccess (lRes == ERROR_SUCCESS); printf ("ExistsAndSuccess : %d\n",bExistsAndSuccess); bool bDoesNotExistsSpecifically (lRes == ERROR_FILE_NOT_FOUND); printf ("DoesNotExistsSpecifically : %d\n",bDoesNotExistsSpecifically); std::wstring strValueOfPathDir; std::wstring strValueOfVersion; std::wstring strKeyDefaultValue; GetStringRegKey(hKey, L"path", strValueOfPathDir, L"bad"); GetStringRegKey(hKey, L"Version", strValueOfVersion, L"bad"); GetStringRegKey(hKey, L"", strKeyDefaultValue, L"bad"); std::cout << "strValueOfBinDir:"<<ws2s(strValueOfPathDir) << std::endl; std::cout << "strValueOfVersion:"<<ws2s(strValueOfVersion) << std::endl; std::cout << "strKeyDefaultValue:"<<ws2s(strKeyDefaultValue) << std::endl; }