Solved

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

Posted on 2014-01-08
3
1,306 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

863 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

24 Experts available now in Live!

Get 1:1 Help Now