How to change DWORD to a string?

In my program (using MS VC++) I want to print avariable of type DWORD. But I have to change it to a type of char [] or a string. How?
chenweiAsked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
>> I know now it's impossible to do what I ask.
We conviced you of that using several examples that did it?

>> I should get out how the SERVICE_STOPPED
>> and SERVICE_RUNNING are defined in MS VC++.

They are pre-processor definitions (#defines).  they are defined in winsvc.h
0
 
chenweiAuthor Commented:
And the value of this variable is not a number but something like this: SERVICE_RUNNING or SERVICE_STOPPED.
0
 
pigsmayflyCommented:
DWORD means "double word" which is type of "int".  In Visual C++, this is really an "unsigned long".

If, of course the DWORD is really a number then there is no sense changing it to a string of any kind.  So

DWORD i = 10;
std::cout << i << std::endl;

print number 10

If DWORD is a pointer to a char array, then you should "change" it by typecasting, depending on what it originally is, ie:

char s[100] = "Hello";
DWORD addr = (DWORD)&s;
std::cout << (char *)addr << std::endl;

If you know that DWORD is a pointer but do not know what it originally was, then you are in trouble.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
MichaelSCommented:
_ltoa() function could help
0
 
barzangyCommented:
Use string streams like this:

#include <windows.h>
#include <iostream>
#include <string>
#include <strstream>

using namespace std;

int main() {

     DWORD a = 1030;
     ostrstream s1 = ostrstream();    
     s1 << " " << a << " ";
     string DWORDSTR = s1.str();
     DWORDSTR.erase(DWORDSTR.begin() + 6, DWORDSTR.end());
     cout << DWORDSTR << endl;
     cin.ignore();
     return 0;
}
0
 
chenweiAuthor Commented:
And the value of this variable is not a number but something like this: SERVICE_RUNNING or SERVICE_STOPPED.
0
 
chenweiAuthor Commented:
Thanks for the help. But maybe there is misunderstanding. Surely I know how to change an integer to a string and I know DWORD is an integer as well. But what I want is how to change the SERVICE_RUNNING to a string.

In my program there is a function like:
...
...
DWORD m_dwState;
...
BOOL b = QueryServiceStatus(hService,&ss);
m_dwState = ss.dwCurrentState;
...
if (m_dwState == SERVICE_RUNNING)
...
...

SERVICE_RUNNING is an integer but I don't know huw much it is, so I want to change it to a string like "SERVICE_RUNNING".

Maybe it's impossible?
0
 
jasonclarkeCommented:
I think maybe you are talking about some kind of enumeration, the simplest thing is to use a switch statement:

DWORD value;
std::string message;

switch (value)
{
case 1:
   message = "SERVICE_RUNNING";
   break;
case 2:
   message = "SERVICE_STOPPED";
   break;
default:
   message = "Unknown Code!";
}

a better, but possibly more complicated solution might be to use a map to store the strings.

0
 
barzangyCommented:
You can use some macro's with #:

#include <iostream>
using namespace std;

#define ERR_OK
#define ERR_1
#define ERR_2
#define ERR_3
#define ERR_4

#define CASE(anerror) case(anerror) : cout << "The error " << #anerror << " occured \n"; break;

void out_error (int err) {
 switch(err) {
  CASE(ERR_OK)
  CASE(ERR_1)
  CASE(ERR_2)
  CASE(ERR_3)
  CASE(ERR_4)
 }

int main () {
  out_error(3);
  return 0;
}

0
 
barzangyCommented:
Oops, The definitions above should be:

#define ERR_OK 0
#define ERR_1  1
#define ERR_2  2
#define ERR_3  3
#define ERR_4  4
0
 
barzangyCommented:
I also forgot a "}" in the end of the function out_error!!!
0
 
nietodCommented:
>> You can use some macro's with #:
You can also smoke cigarettes while taking a bath in gasoline.   Personally I wouldn't

In C++ you should never use #define to create constants.  C++ supports true constants that are far far safer and more logical that #defines

As a variation in JasonClarke's idea, if the constants are numerically sequential (or reasonably so), you can skip the case statement and use an array.   I beleive you have that case here.  L:ike

const char *ServiceList[] = {"SERVICE_STOPPED",
                                             "SERVICE_START_PENDING",
                                             "SERVICE_STOP_PENDING",
                                             "SERVICE_RUNNING",
                                             "SERVICE_CONTINUE_PENDING"};

Then you can use somethine like

string S = ServiceList[TheServiceDWORDValue];
0
 
chenweiAuthor Commented:
Thankd for the information from all sites. I know the methods you told me. But the SERVICE_STOPPED and SERVICE_RUNNING are predefined in VC++.

I know now it's impossible to do what I ask. I should get out how the SERVICE_STOPPED and SERVICE_RUNNING are defined in MS VC++.
0
 
chenweiAuthor Commented:
Hi nietod,

I got it. Thanks. I want to give you the points but I am not sure if you've locked the question or not. If no, please lock this question so I can give the points.
0
 
chenweiAuthor Commented:
Hi nietod,

I got it. Thanks. I want to give you the points but I am not sure if you've locked the question or not. If no, please lock this question so I can give the points.
0
 
nietodCommented:
The question is currently locked by barzangy.  Notice how his post has the "propsed answer" title.   Only one expert can lock a question at a time so I cannot lock.

Your choices (always)are to either accept that as the answer by grading it, or reject it so other experts can lock the question.  Also, if there is no proposed answer (if one was never proposed, or if you rejected all the proposed answers) you also have the choice of accepting comments as answers.  Each comment will have a link for doing so on the right side of their title.  (Make sure you choose a comment for the right expert, lots of people seem to choose the wrong one by mistake.  Remember the title is above the comment, not below.)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.