Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Converting DWORD Registry Value to String

Posted on 2000-02-26
6
Medium Priority
?
1,456 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

719 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