Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

problem with RegQueryValueEx

Posted on 2005-03-08
7
Medium Priority
?
513 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Integration Management Part 2
Suggested Courses
Course of the Month12 days, 10 hours left to enroll

578 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