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
Solved

Problem with RegQueryValueEx

Posted on 1997-06-08
2
754 Views
Last Modified: 2013-11-20
I'm having a problem with the RegQueryValueEx function - it's returning error code 87 (Invalid Parameter), and it's due to the 5th parameter.  I know this, because it returns ERROR_SUCCESS if I make this parameter NULL.

      CString sPwd;
      HKEY      phk;
      char      sPwdReg[10];
      DWORD      dwType;
      DWORD      dwcbData;

//#ifdef WIN32_EXTRA_LEAN
//      this_statement_will_generate_compile_error;
//#endif
      if (::RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\a\\b",
                  0, KEY_ALL_ACCESS, &phk) != ERROR_SUCCESS)
            return;
      AfxMessageBox("OpenKey worked");

      long lRet = ::RegQueryValueEx(phk, (LPTSTR) "Password", NULL, NULL,
                  (LPBYTE) sPwdReg, &dwcbData);

      AfxMessageBox("QueryValue worked");

      CString msg;
      msg.Format("lRet = %ld", lRet);
      AfxMessageBox(msg);

The "Password" registry value is a string, by the way.  Does anybody know what I'm doing wrong here?  I've looked through all the examples in the Visual C++ documentation, and found no clues.  I've seen this argument typecast to LPCTSTR or LPTSTR, but this won't compile.  I've also tried using a char* argument, and allocating memory to it with malloc, but this shouldn't make a difference, and it didn't.

A possibly related problem is that in my Project Settings/C/C++ Tab/Preprocessor Category, I have undefined WIN32_EXTRA_LEAN in order for the compiler to include "winreg.h", which is needed for the registry API.  However, if I uncomment the #ifdef WIN32_EXTRA_LEAN directive, I find that WIN32_EXTRA_LEAN *is* defined.  I cannot figure this out - I'm working around it by just explicitly including "winreg.h".
0
Comment
Question by:nbranitsky
2 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1302032
You should initialize dwcbData to the size of the buffer in bytes before passing it to ::RegQueryValueEx().

DWORD dwcbData = sizeof(sPwdReg);

0
 

Author Comment

by:nbranitsky
ID: 1302033
Thanks.  That should teach me to read the documentation more closely!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

860 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