Solved

Problem with RegQueryValueEx

Posted on 1997-06-08
2
734 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Running shell command from Access 3 63
Folder Comparison 12 52
Perl Awk Need Help 3 94
Thin secure Windows 10 5 49
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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now