?
Solved

problem with RegQueryValueEx

Posted on 2005-03-08
7
Medium Priority
?
506 Views
Last Modified: 2013-11-20
I have the following code (including some AfxMessageBox statements for test:

----------------
long lReturn = RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T("Software\\Vendor\\App"), 0, KEY_QUERY_VALUE , &hKey );

if ( ERROR_SUCCESS == lReturn)
{
     DWORD dwType = 0;
     TCHAR lpszData[1024];
     DWORD dwDataSize = (lstrlen(lpszData) + 1) * sizeof(TCHAR);
     if (ERROR_SUCCESS == (lReturn = RegQueryValueEx( hKey, _T("Location"), 0, &dwType, (BYTE *)lpszData, &dwDataSize)) )
     {
          m_strIniFile = lpszData;
          m_strIniFile += _T("\\camera.ini");
          AfxMessageBox("test - here");
     }
     else
       {
          m_strIniFile = _T(".\\camera.ini");
          CString strError( _T("An error occured during initialization.\n The error was: ") );
          _TCHAR lpszFormattedError[_MAX_PATH];
          FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, lReturn, lpszFormattedError, _MAX_PATH, 0 );
          strError += lpszFormattedError;
          AfxMessageBox(strError);
            }
}
else
{
     m_strIniFile = _T(".\\camera.ini");
     AfxMessageBox("test - the other");
}

----------------


When I run this code in debug mode, within the VC++ 6.0 environment, I get the "test - here" message - which is what I want.
When I make a release build and run it, I get the "An error occured during initialization.." message with "more data is available"

what does that mean? This code used to work fine, and I haven't changed it (other than those AfxMessage tests.) so is there a project setting or something that would affect this?

thanks!
-Paul
0
Comment
Question by:PMH4514
[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
  • 4
  • 3
7 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 200 total points
ID: 13486558
Hi PMH4514,
> >DWORD dwDataSize = (lstrlen(lpszData) + 1) * sizeof(TCHAR);

The above line looks wrong, and can preduce unpredictiable results (undefined behavior)

Instead of lstrlen, you should use sizeof.
DWORD dwDataSize = sizeof(lpszData);

David Maisonave :-)
Cheers!
0
 
LVL 30

Expert Comment

by:Axter
ID: 13486605
The lpszData variable is not getting initialized to anything.
In the debug version, this variable will usually get initialize to zero's.
However, in the release version, anything can be in this variable, so you have no idea what the value of dwDataSize will end up being.

From looking at the code logic, it should be using sizeof instead of strlen.
sizeof doesn't care if there's data in the string or not, and it's value is determine at compile time, and not at runtime.
This makes sizeof more efficient, and in this case more reliable.

David Maisonave  :-}
0
 

Author Comment

by:PMH4514
ID: 13486636
You're giving us your full name now David? ;-)

Let me go try that.

Strange ideed though, because release versions of this app have been working for a couple years now, this code has not changed, nor has the path to, the name of, or the variable contained within, the INI file.



0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Expert Comment

by:Axter
ID: 13486708
PMH4514,
> >You're giving us your full name now David? ;-)

I just change my auto reply signature settings for QuickPost.  I thought I give this a try for now.

David Maisonave  :-}
0
 

Author Comment

by:PMH4514
ID: 13486796
Well, your solution worked..

I'm still puzzled why it stopped working? Is it possible that OS service pack updates could have caused the old (albeit "bad") code to stop working as it was?
0
 
LVL 30

Expert Comment

by:Axter
ID: 13486833
>>I'm still puzzled why it stopped working? Is it possible that OS service pack updates could have caused the old (albeit "bad") code to
>>stop working as it was?

Any time you have code that produces undefined behavior, anything can happen at anytime.

Your code can work for years, and any minor change can cause it to stop working.
0
 

Author Comment

by:PMH4514
ID: 13486914
THANKS!
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

764 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