Solved

Problem with RegQueryValueEx

Posted on 1997-06-08
2
763 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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
WinWaitActive parameters 12 31
Change owner from userA to server Local Administrators 7 73
sameEnds challenge 3 194
java ^ examples 8 73
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ā€¦
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

739 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