Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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…

749 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