The last time we introduced how to save the data to the Registry, We need to read the data from the registry. In this example, we read the string data and mainly call the regqueryvalueex function. In the following example, we first query the length of the key value and then read the content.
The regqueryvalueex function declaration is as follows:
Winadvapi
Long
Apientry
Regqueryvalueexa (
_ In hkey,
_ In_opt lpcstr lpvaluename,
_ Reserved lpdword lpreserved,
_ Out_opt lpdword lptype,
_ Out_bcount_opt (* lpcbdata) lpbyte lpdata,
_ Inout_opt lpdword lpcbdata
);
Winadvapi
Long
Apientry
Regqueryvalueexw (
_ In hkey,
_ In_opt lpcwstr lpvaluename,
_ Reserved lpdword lpreserved,
_ Out_opt lpdword lptype,
_ Out_bcount_opt (* lpcbdata) lpbyte lpdata,
_ Inout_opt lpdword lpcbdata
);
# Ifdef Unicode
# Define regqueryvalueex regqueryvalueexw
# Else
# Define regqueryvalueex regqueryvalueexa
# Endif //! Unicode
Hkey is the primary key.
Lpvaluename is the key value name.
Lptype is a type.
Lpdata is the place where read data is stored.
The number of data read is the number of data read.
An example of calling a function is as follows:
#001 // enter the Registry return value. HKEY_CURRENT_USER/"software"/"wincpp"/"testreg"
#002 // "Windows" // "winsize" = "800*600"
#003 // Cai junsheng 2007/11/05 QQ: 9073204 Shenzhen
#004 STD: wstring getprofilestring (maid, maid,
#005 lpctstr lpszdefault)
#006 {
#007 // open the application key.
#008 hkey happkey = getappregistrykey ();
#009 if (happkey = NULL)
#010 {
#011 return lpszdefault;
#012}
#013
#014 hkey hseckey = NULL;
#015 DWORD dw;
#016
#017 // open the subkey.
#018 regcreatekeyex (happkey, lpszsection, 0, reg_none,
#019 reg_option_non_volatile, key_write | key_read, null,
#020 & hseckey, & DW );
#021 regclosekey (happkey );
#022
#023 if (hseckey = NULL)
#024 {
#025 return lpszdefault;
#026}
#027
#028 // query the key value.
#029 STD: wstring strvalue;
#030 DWORD dwtype = reg_none;
#031 DWORD dwcount = 0;
#032
#033 // query the length of the key value first.
#034 long lresult = regqueryvalueex (hseckey, (lptstr) lpszentry, null, & dwtype,
#035 null, & dwcount );
#036 if (lresult = error_success)
#037 {
#038 strvalue. Resize (dwcount );
#039
#040 // query the key value.
#041 lresult = regqueryvalueex (hseckey, (lptstr) lpszentry, null, & dwtype,
#042 (lpbyte) strvalue. Data (), & dwcount );
#043
#044}
#045
#046 regclosekey (hseckey );
#047 if (lresult = error_success)
#048 {
#049 return strvalue;
#050}
#051
#052 return lpszdefault;
#053}