> I have created a registry entry and want to read it through C program.
But the RegQueryValueEx() function is failing and I am not seeing any
error.
I directly get the command prompt.
Here is the code snippet:
#define REG_KEY TEXT(“Software\MyCompany\XYZ”) // XYZ is
also a key and
has a
default
value
REG_SZ in
it
HKEY hKey;
LONG lResult;
DWORD BufferSize = MAX_PATH;
DWORD dwValue, dwSize = sizeof(dwValue);
******
I consider it to be exceptionally bad style to use a comma in a
declaration, and it is very evil to mix initialized and uninitialized
declarations like this. I find this code nearly unreadablee
*****
_tprintf( _T(“\nInside delOEMInf.”) );
if((RegOpenKeyEx(HKEY_LOCAL_MACHINE,
REG_KEY,
0,
KEY_ALL_ACCESS,
&hKey)) == ERROR_SUCCESS)
{
_tprintf( _T(“\ RegOpenKeyEx OPENED”));
lResult = RegQueryValueEx(hKey, “”, NULL, NULL, (LPBYTE)&dwValue,
BufferSize);
*****
I’m surprised this even compiles! From the documentation
LONG WINAPI RegQueryValueEx(
__in HKEY hKey,
__in_opt LPCTSTR lpValueName,
__reserved lpReserved,
__ out_opt LPDWORD lpType,
__out_opt LPBYTE lpData,
__in_out_opt LPDWORD lpCbData)
There are several things wrong with your call. From a rbustness
perspective, you CANNOT set lpType to NULL, because this naively expects
that the type will be REG_DWORD. This is an unwarranted assumption, and
your world will disintegrate quickly if a fumble-fingered user with
regedit has made it something else. This actually happened to me sometime
in the late 1990s, and I NEVER assume the value type. If it is not
REG_DWORD, feel free to complain in some fashion, but do NOT assume that
it is!
Now look at the last parameter. It is SUPPOSED to be a pointer to a DWORD
that contains the maximum buffer size, and on completion, this variable
will reflect the ACTUAL buffer size used. You do not pass in an LPDWORD;
you pass in a DWORD. The compiler should complain. But let’s guess that
you had actually written &Buffersize. Then it is still wrong, because
lpData is the address of a DWORD and is certainly NOT going to be able to
accomodate MAX_PATH bytes. In fact you have just created a potential
security hole, since you ignore the type and give an absurd size; knowing
this, I could create a REG_BINARY value that could get me control in your
program. This Is Not A Pretty Sight.
In addition, I always check the size used after the call. You should set
it to sizeof(dwValue) before the call, and make sure that upon completion
it is ==sizeof(dwValue). I learned that Registry types and lengths are
never to be trusted.
And I have no idea why you used an 8-bit string literal for the name. Use
TEXT(“”) or _T(“”) or, since you want the unnamed default value, just
NULL.
As for debugging it, the very first thing you try is single-step
execution. Debug print statements are sometimes very convenient, but
there’s no substitute for seeing what’s really happening!
*****
_tprintf( _T(“\nRegQueryValueEx %d”), lResult);
RegCloseKey( hKey);
}
I get only the first printf “RegOpenKeyEx OPENED”.
The second printf does not gets printed and I see directly the command
prompt.
I am not abel to get the return value of regqueryvalueex().
What might be the issue and how to debug and resolve it?
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer