Solved

Converting DWORD Registry Value to String

Posted on 2000-02-26
6
1,407 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 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
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

617 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