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

x
?
Solved

Converting DWORD Registry Value to String

Posted on 2000-02-26
6
Medium Priority
?
1,517 Views
Last Modified: 2008-03-04
I have a registry DWORD value = 0x00000002 (2).  I need to check if the value is >= 2.  I am using RegOpenKeyEx to open the registry key and RegQueryValueEx to find the value of the DWORD.  This all works fine.  The problem is converting from DWORD to string or int.  I log the result in a file and it gives me a black square.  Any ideas?

      reg_error = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                                           "SYSTEM\\CurrentControlSet\\Services\\W3SVC\\Parameters",
                                           0,
                                           KEY_ALL_ACCESS,
                                           &hKey);

      sprintf(xbuf, "Reg Error: %d", reg_error);
      fprintf(dbglog, "%s\n", xbuf);
      err_log (xbuf);
      
      if (reg_error == ERROR_SUCCESS) {

            RegQueryValueEx(hKey,
                                    "MajorVersion",
                                    NULL,
                                    &val_type,
                                    (LPBYTE)data_buffer,
                                    &buf_backup);
            sprintf(xbuf, "Data Buffer: %s", data_buffer);
            fprintf(dbglog, "%s\n", xbuf);
            err_log (xbuf);
0
Comment
Question by:paulca
  • 4
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 400 total points
ID: 2561252
A DWORD is an unsigned integral type (unsigned int in win32), so it can be converted to an int as long as the value inside the DWORD does not exceed the range of int just by using =, like

DWORD Dw = 5;
int i;

i = Dw;
0
 
LVL 2

Expert Comment

by:bbousquet
ID: 2561259
Well, it did convert the DWORD to a string, but a string containing the character 2, which is a non-printable character. If you want to convert a DWORD variable to a string representing the "2" value (and not the character 0x02) you could use:

CString strTmp;
strTmp.Format("%d", data_buffer);

....or for hex:
strTmp.Format("%x", data_buffer);

[if you'd rather use sprintf, just substitute it to the "strTmp.Format" call, with the same params]
0
 
LVL 22

Expert Comment

by:nietod
ID: 2561269
The problem with

sprintf(xbuf, "Data Buffer: %s", data_buffer);

is that data_buffer stores a DWORD value (that is the exact binary nature of the value, you might have delcared data_buffer as if it is a char array, but the OS stored a binary number there in DWORD format.)  So you need to tell the sprintf() function that you are printing an unsinged integer value which would be the %u format specifieer, not the %s string format specifier.  However,

sprintf(xbuf, "Data Buffer: %u", data_buffer);

would not be correct.  The reason is that when you pass data_biffer you are will pass a pointer to some sort of array (I assume that data_buffer is an array, right?)  And that is fine when you print a string, sprintf() expects a pointer to an array when it prints a string with %s.   But when you print a number with %u (or %i etc) doesn't expect a pointer to the number, it expects the actuall number, so you need to do a little playing to make this work

continues.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:bbousquet
ID: 2561278
Wasn't that what I just said?

;-)
0
 
LVL 22

Expert Comment

by:nietod
ID: 2561292
If you declare an array, the name of the array acts like a pointer.  So, for example, if you did

char data_buffer[100];

databuffer acts like a pointer to 100 characters.  You can convert this to a pointer to a DWORD by doing

(DWORD *)data_buffer;

Now this gives you a pointer to a DWORD, but sprintf() wants the actual DWORD, so you just need dereference this pointer with *, so you'll get

sprintf(xbuf, "Data Buffer: %u",*(DWORD *)data_buffer);

Let me know if you have any questions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2561298
Sorry, bbousquet, but this is not a real-time system.  So I could't see your comment until I posted my comment that came AFTER that.  And actually, I didn't see it then either.  I was busy typing, not reading so I didn't see it until now.

Anyways, if you read what I said, you will see that your code woin't actually work because you are passing a pointer to a value when the function expects the actual value.  (try it.)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

972 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