Solved

Converting DWORD Registry Value to String

Posted on 2000-02-26
6
1,208 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 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Expert Comment

by:bbousquet
Comment Utility
Wasn't that what I just said?

;-)
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 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

9 Experts available now in Live!

Get 1:1 Help Now