Solved

Converting DWORD Registry Value to String

Posted on 2000-02-26
6
1,238 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

932 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

18 Experts available now in Live!

Get 1:1 Help Now