Problem with RegQueryValueEx

G

Guest

I have problem with the vc++ 6 compiler and its libraries on windows 200

Create the following new ke
HKEY_LOCAL_MACHINE\SOFTWARE\SURI\{6FE7A40D-F0EA-4008-A238-3C306E2B2EA9

inside the key create the strin

"Assembly"="P24R3O35T1E4C7T14U26S

============================
The file test.cpp looks lik
============================

------------------------------------------------------------------------------------------
/* The following is the header file included to accomadate vc++6*
#include<AFXWIN.H
#include <string.h
#include <windows.h
#include <tchar.h
#include <stdio.h
#include <malloc.h


/* To test This code create the following registry entrie

Create the following new ke
HKEY_LOCAL_MACHINE\SOFTWARE\SURI\{6FE7A40D-F0EA-4008-A238-3C306E2B2EA9

inside the key create the strin

"Assembly"="P24R3O35T1E4C7T14U26S

*

int function1(void)

int main (int argc, char* argv [])

char dir1 [1000]

int abc=0

abc=function1()

} /* main *

int function1(void

/* Local variable declaration*

HKEY hKey,hKey_1
DWORD dwType_1
DWORD dwSize
LONG queryResult
char itime[60]


if ((RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\SURI\\{6FE7A40D-F0EA-4008-A238-3C306E2B2EA9}"),0,KEY_QUERY_VALUE,&hKey_1))==ERROR_SUCCESS

queryResult = RegQueryValueEx(hKey_1, _T("Assembly"), NULL, &dwType_1,(LPBYTE)&itime, &dwSize)

if(ERROR_SUCCESS!=queryResult

printf("Error with RegQueryValueEx \n")



/* Check if the installation time is null
it should not happen once VSEP is installed *
if( strlen(itime) >= 1

MessageBox(NULL,"Success fully read the registry","Security Check",MB_OK)

/* Else means the user has deleted the installTime registry entry from the registry
We force to expire VSEP and delete the Package key *
else

MessageBox(NULL,"Error unable to read the registry","Security Check",MB_OK)
exit(1)



return 0


------------------------------------------------------------------------------------------

==========================================================
Compile and line the code using the following command line
==========================================================

"c:/Program Files/Microsoft Visual Studio/vc98/Bin/cl.exe" -c -nologo -W4 -GX -D "WIN32" -D "_WINDOWS" -FD -MT -Fotest.o test.cp

cl -otest.exe -nologo -W4 -GX -D "WIN32" -D "_WINDOWS" -FD -D_EVALUATION -MT test.o LIBCMT.lib /link /force:multipl

execute test .ex

The program would be able to successfully read the registry entry from windows registr
and we would get the message "Success fully read the registry

--------------------------------------------------------------------------------------------
=======
Proble
=======

change the lin

char dir1 [1000];
t
char dir1[10000]

compile and link using the same set of command lines abov

and execute test.ex

We would get the error message
"Error unable to read the registry

This is because the function cal

queryResult = RegQueryValueEx(hKey_1, _T("Assembly"), NULL, &dwType_1,(LPBYTE)&itime, &dwSize)

malfunction

Please help me in identifying the reason for malfunction of the function RegQueryValueE

-----------------------------------------------------------------------------------
 
E

EMonaco

You say it malfunctions? Does it return an error? If so what is the error?
If not- what does it return?

Given that increasing the size of the dir1 array would have the effect of
changing where on the stack your locals in function1() are referencing (in
terms of physical location)... I would suspect uhuh, arn't you supposed to
initialize dwSize with the size of itime before calling RegQueryValueEx()?
something like DWORD dwSize = 60, or better = sizeof(itime). Of course you'd
have to declare itime above dwSize in the latter case.



Regards,
Erin.
 
E

EMonaco

This is why you always want to report GetLastError() when APIs fail- thus
you'd see something like ERROR_MORE_DATA which lets you know your buffer is
to small- in this case your buffer is NOT to small but because your not
initializing dwSize before calling RegQueryValueEx() dwSize contains
whatever happens to be in its memory location at the time of the call- with
dir1[1000] it was a value big enough to succeed, with dir1[10000] it was
not- however its the luck of the draw, etc. and my guess is that with
modifications even (adding, removing local vars, etc) will cause even the
dir1[1000] to fail.

Regards,
Erin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top