Solved

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

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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.
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

615 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