Reputation: 799
I was surprised about the behavior of the following code:
if(RegQueryValueEx(....)!=ERROR_SUCCESS){
...
}
when it was run from visual studio it didn't enter this if block, because the key did exist. when ran outside of the visual studio environment it evaluated true and hence entered the block, even though the queried key existed. After some testing i found out that when i first save it to a variable it runs always fine. With the following code:
HKEY hSoftwareKey,hAppKey;
DWORD dwLength;
int iStatus=1;
char szBuffer[MAX_PATH];
if(iStatus&&RegOpenKeyA(HKEY_CURRENT_USER,"Software",&hSoftwareKey)!=ERROR_SUCCESS)
iStatus = 0;
if(iStatus&&RegCreateKeyA(hSoftwareKey,"Amine",&hAppKey)!=ERROR_SUCCESS){
iStatus = 0;
}
ZeroMemory(szBuffer,MAX_PATH);
LONG lRet;
lRet = RegQueryValueExA(hAppKey,"One",0,0,reinterpret_cast<LPBYTE>(szBuffer),&dwLength);
Does this bevavior have anything to do with the __stdcall/WINAPI calling convention? If so could somebody please explain why
Upvotes: 0
Views: 81
Reputation: 5142
You need to initialize dwLength
to MAX_PATH
before calling RegQueryValueEx(), otherwise it's value is undefined.
from MSDN RegQueryValueEx:
lpcbData is a pointer to a variable that specifies the size of the buffer pointed to by the lpData parameter, in bytes. When the function returns, this variable contains the size of the data copied to lpData
Upvotes: 3