When we call the regqueryvalueex function to read string data in the registry, error 234 is often returned.
Msdn says if the buffer specified by lpdata parameter is not large enough to hold the data, the function returns the value error_more_data,
Window nt: If hkey specifies hkey_performance_data and the lpdata buffer is too small, regqueryvalueex returns error_more_data but lpcbdata does not return the required buffer size. this is because the size of the performance data can change from one call to the next. in this case, you must increase the buffer size and call regqueryvalueex again passing the updated buffer size in the lpcbdata parameter. repeat this until the function succeeds. you need to maintain a separate variable to keep track of the buffer size, because the value returned by lpcbdata is unpredictable.
The solution is as follows:
Byte bybuffer [1024];
DWORD dwlen = 1024;
// Read the Database Password
Regqueryvalueex (m_hkey, "dbpassword", null, & dwtype, bybuffer, & dwlen );
M_pt.strpwd = (lpctstr) bybuffer;
// Read the address of the Database Server
Dwlen = 1024;
Regqueryvalueex (m_hkey, "dbserveraddress", null, & dwtype, bybuffer, & dwlen );
M_pt.strhostname = (lpctstr) bybuffer;
// Read the database username
Dwlen = 1024;
Regqueryvalueex (m_hkey, "dbuser", null, & dwtype, bybuffer, & dwlen );
M_pt.strdbuser = (lpctstr) bybuffer;
In the above Code, when we read the password string, the variable dwlen will change to 8. If we do not perform any operation to read the following server address string, it is easy to get a 234 error. Because the length of the server address string is 10. Therefore, before calling this function, we must ensure that the value of the last parameter is greater than or equal to the length of the data to be read. After executing this function, the value of this parameter is the actual length.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/liuliu20036/archive/2008/11/22/3351541.aspx
About registry operations
Below is a basic dialog written based on MFC .. This example shows how to set the value of Type 3 in the Registry and obtain the value of Type 3 in the registry .. Use regsetvalueex () to set the parameters respectively, and obtain the regqueryvalueex () function.
Void cuserregdlg: onok ()
{
// Todo: add extra validation here
Cstring STR = "Hello Reg ";
DWORD regd= 100;
Byte bvalues [] = {0x11, 0x12, 0x55 };
Byte Vals [100];
DWORD lenit = 100;
Hkey HK;
// Create a registry if no registry is opened, and return the Registry handle at HK
If (: regcreatekey (HKEY_LOCAL_MACHINE, "software // test //", & HK) = error_success)
{
Afxmessagebox ("open registry ");
}
// Save the parameters to the Registry
Try
{
/*
Regsetvalueex (root key handle, value item name, retain parameter fill 0, data type, set data, data length)
Used to set the specific value of the registry key. If the data does not exist, create it.
*/
/* Save the character value to the Registry */
If (: regsetvalueex (HK, "server", 0, REG_SZ, (lpbyte) (lpcstr) STR, str. getlength () + 1) = error_success)
{
Afxmessagebox ("the character value parameter is saved successfully ");
}
/* Save the double byte to the Registry */
If (: regsetvalueex (HK, "part", 0, REG_DWORD, (byte *) & regd, sizeof (regd) = error_success)
{
Afxmessagebox ("two-byte parameter saved successfully ");
}
/* Save the binary data to the Registry */
If (: regsetvalueex (HK, "ID", 0, REG_BINARY, bvalues, 3) = error_success)
{
Afxmessagebox ("binary parameter saved successfully ");
}
/*
Regqueryvalueex
Read data, return the length of the read data)
Returns the value of a specific name of the registry key.
/*
/* Get the character value */
If (: regqueryvalueex (HK, "server", 0, null, (byte *) Vals, & lenit) = error_success)
{
Afxmessagebox (cstring) Vals );
}
/* Obtain the double byte value */
If (: regqueryvalueex (HK, "part", 0, null, (byte *) Vals, & lenit) = error_success)
{
Afxmessagebox (cstring) Vals );
}
/* Obtain the binary value */
If (: regqueryvalueex (HK, "ID", 0, null, (byte *) Vals, & lenit) = error_success)
{
Afxmessagebox (cstring) Vals );
}
// Close the registry
: Regclosekey (HK );
}
Catch (...)
{
Return;
}
// Cdialog: onok ();
}