Solved

C++ Problem with getting DWORD (from Registry) to INTEGER

Posted on 2014-01-08
3
1,239 Views
Last Modified: 2014-01-08
It's been years since I have had to call on EE, but I am working on C++ code (I am a C# person).  So I am struggling.

I am storing a DWORD in the registry (successfully), the integer is the number 98, the data in the registry is stored as hex 0x00000062, but when I retrieve the value from the registry, I need to convert it back to an INTEGER (because it stores Serial Port current selection value).  The last line of code is where I need to set the selection box to the integer value, I know it is getting something out of the registry, but I cannot seem to figure out how to get it from the DWORD to an integer (it always converts to 0).  Seems I have exhausted many hours on Google without success:

HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Motorola\\RS507Config", 0, KEY_READ, &hKey) != ERROR_SUCCESS)
    AfxMessageBox(_T("Could not open registry key"));
DWORD type;
DWORD cbData;
    if (RegQueryValueEx(hKey, "BtSerialPort", NULL, &type, NULL, &cbData) != ERROR_SUCCESS)
    {
        RegCloseKey(hKey);
        AfxMessageBox(_T("Could not read registry value"));
    }
    if (type != REG_DWORD)
    {
        RegCloseKey(hKey);
        AfxMessageBox(_T("Incorrect registry value type"));
    }
DWORD *pdwData;
DWORD dwSize = sizeof(DWORD);

    if (RegQueryValueEx(hKey, "BtSerialPort", NULL, NULL, (LPBYTE)pdwData, (LPDWORD)&dwSize) != ERROR_SUCCESS)
        {
               RegCloseKey(hKey);
               m_ComboDeviceSelect.SetCurSel(13);
               AfxMessageBox(_T("Could NOT read registry value"));
        }
        RegCloseKey(hKey);
        m_ComboDeviceSelect.SetCurSel((INT) pdwData);
0
Comment
Question by:BrotherEarth
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39766133
The problem is that you are trying to convert a pointer to a DWORD to an integer, which cannot work that way. That should be

        RegCloseKey(hKey);
        m_ComboDeviceSelect.SetCurSel((INT) *pdwData); // note the '*'

Open in new window


Also, he declaration of 'pdwData' in the aboveis uninitialized pointer. So you better make that

HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Motorola\\RS507Config", 0, KEY_READ, &hKey) != ERROR_SUCCESS)
    AfxMessageBox(_T("Could not open registry key"));
DWORD type;
DWORD cbData;
    if (RegQueryValueEx(hKey, "BtSerialPort", NULL, &type, NULL, &cbData) != ERROR_SUCCESS)
    {
        RegCloseKey(hKey);
        AfxMessageBox(_T("Could not read registry value"));
    }
    if (type != REG_DWORD)
    {
        RegCloseKey(hKey);
        AfxMessageBox(_T("Incorrect registry value type"));
    }
DWORD dwData; // better us an instance, not a pointer
DWORD dwSize = sizeof(DWORD);

    if (RegQueryValueEx(hKey, "BtSerialPort", NULL, NULL, (LPBYTE)&dwData,  // address instead of a pointer
(LPDWORD)&dwSize) != ERROR_SUCCESS)
        {
               RegCloseKey(hKey);
               m_ComboDeviceSelect.SetCurSel(13);
               AfxMessageBox(_T("Could NOT read registry value"));
        }

        RegCloseKey(hKey);
        m_ComboDeviceSelect.SetCurSel((INT) dwData); 

Open in new window

0
 

Author Closing Comment

by:BrotherEarth
ID: 39766181
I did not notice the pointer character all this time and so I spent hours, I wished I had noticed.  Thank you very, very much for the quick response (I used the second set of code to eliminate the pointer code).  Mission accomplished!
0
 
LVL 86

Expert Comment

by:jkr
ID: 39766185
You're most welcome ;o)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

705 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

13 Experts available now in Live!

Get 1:1 Help Now