Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1674
  • Last Modified:

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

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
BrotherEarth
Asked:
BrotherEarth
  • 2
1 Solution
 
jkrCommented:
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
 
BrotherEarthAuthor Commented:
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
 
jkrCommented:
You're most welcome ;o)
0

Featured Post

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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now