Subj: RegQueryValueEx fails Section: Windows API To: all Thursday, November 12, 1998 11:26:06 AM From: Daniel Finnegan, 110735,3536 #723351 what circumstances could cause the Win32 API call RegQueryValueEx() to cause an access violation (dr watson screen) which crashes vb6.exe ? this access violation occurs for all values to be retrieved EXCEPT if the value is a zero-length string ! zero-length strings are retrieved with no problem --> RegQueryValueEx() returns 0 = success. so , apparently the actual loading of the buffer is what is causing the problem i am using Visual Basic 6.0 on Windows NT 4.0 with Service Pack 3 my declaration is : Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long my calling code is Dim KeyValueBuffer As String * 100 KeyValueBuffer = String(100, " ") KeyValueBufferLength = 101 'include Null terminator (which is automatically added) ReturnValue = RegQueryValueEx(hODBCDSKey, "ServerName", 0, REG_SZ, KeyValueBuffer, KeyValueBufferLength) where hODBCDSKey is a handle to one of the sub-keys of the key HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI in the registry. hODBCDSKey was obtained using RegEnumKeyEx(). the values which i am attempting to retrieve are strings of lengths far less than 100 characters. ----------------------------------------------------------------------------------------------- Subj: RegQueryValueEx fails Section: Windows API To: Daniel Finnegan, 110735,3536 Thursday, November 12, 1998 10:48:07 PM From: Calvin G. Smith, 102226,2127 #723395 Hi Daniel! >>>>>> what circumstances could cause the Win32 API call RegQueryValueEx() to cause an access violation (dr watson screen) which crashes vb6.exe ? this access violation occurs for all values to be retrieved EXCEPT if the value is a zero-length string ! zero-length strings are retrieved with no problem --> RegQueryValueEx() returns 0 = success. so , apparently the actual loading of the buffer is what is causing the problem <<<<<< I agree! From looking at your code, KeyValueBuffer should be Null terminated ( e.g. KeyValueBuffer = String$(100, 0) ). That's assuming that hODBCDSKey was opened successfully. >>>>>> hODBCDSKey was obtained using RegEnumKeyEx() <<<<<< Personally, I use RegOpenKeyEx to open my Key Root and Key of choice. >>>>>> include Null terminator (which is automatically added) <<<<<< Unlike Win95, NT does not automatically Null terminate strings. So, if your app is running on Win95 and NT, you'll need to run KeyValueBuffer through the Mid$( ) function, starting at position KeyValueBufferLength, for a length of 1 to see if a Null is present. e.g. If (Asc(Mid(KeyValueBuffer, KeyValueBufferLength, 1)) = 0) Then ... If so, strip the Null before returning the returned Key value out of your function. Hope that helps! :-) Calvin Smith - Sr. Visual Basic/MS Access Consultant ******************************************************** http://www.CalvinSmithSoftware.com/ ********************************************************