RegQueryValueEx behaviour with C++

Posted on 2003-03-14
Medium Priority
Last Modified: 2010-04-01
under W2000 or NT this function works perfectly, but I cannot have it working under W98. The dwPath comes back with "" and lResult = 87.

Any idea?. The code is below.


// dwPath size is large enough
BOOL getKeyValue(char *dwPath)
HKEY hKey;
DWORD dwType, dwSize;

long lResult = RegOpenKeyEx(HKEY_CURRENT_USER ,
                 0, KEY_QUERY_VALUE, &hKey) ;

if (lResult != ERROR_SUCCESS)
     return FALSE ;

lResult = RegQueryValueEx(hKey, TEXT("InstallDir"), NULL, &dwType, (LPBYTE)dwPath, &dwSize);

if (lResult != ERROR_SUCCESS)
     return FALSE ;

lResult = RegCloseKey(hKey) ;
if (lResult != ERROR_SUCCESS)
     return FALSE ;

return TRUE;
Question by:alex_paris
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 32

Accepted Solution

jhance earned 80 total points
ID: 8135572
I see that you're NOT initializing dwSize with the size of your dwPath buffer.  This is a FUNDAMENTAL ERROR and will eventually cause problem on ALL versions of Windows.

You MUST init this value before ANY call to RegQueryValueEx().
LVL 32

Expert Comment

ID: 8136455
By the way.  I know this from painful experience....  I can't tell you how many times this has bitten me.

Glad to help.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question