Solved

Problem with RegQueryValueEx

Posted on 1997-06-08
2
759 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
[X]
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 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Title # Comments Views Activity
not able to insert into temp table 68 174
Tab names are off by one 5 54
twoTwo  challenge 35 112
scoreUp challenge 14 69
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

740 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