Solved

How to change DWORD to a string?

Posted on 2001-07-13
16
350 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:chenwei
  • 6
  • 4
  • 3
  • +3
16 Comments
 

Author Comment

by:chenwei
ID: 6279801
And the value of this variable is not a number but something like this: SERVICE_RUNNING or SERVICE_STOPPED.
0
 

Expert Comment

by:pigsmayfly
ID: 6279862
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
 
LVL 6

Expert Comment

by:MichaelS
ID: 6279912
_ltoa() function could help
0
 

Expert Comment

by:barzangy
ID: 6280093
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
 

Author Comment

by:chenwei
ID: 6280106
And the value of this variable is not a number but something like this: SERVICE_RUNNING or SERVICE_STOPPED.
0
 

Author Comment

by:chenwei
ID: 6280133
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
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6280143
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
 

Expert Comment

by:barzangy
ID: 6280201
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:barzangy
ID: 6280205
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
 

Expert Comment

by:barzangy
ID: 6280216
I also forgot a "}" in the end of the function out_error!!!
0
 
LVL 22

Expert Comment

by:nietod
ID: 6280287
>> 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
 

Author Comment

by:chenwei
ID: 6280510
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
 
LVL 22

Accepted Solution

by:
nietod earned 20 total points
ID: 6280541
>> 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
 

Author Comment

by:chenwei
ID: 6280602
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
 

Author Comment

by:chenwei
ID: 6280627
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
 
LVL 22

Expert Comment

by:nietod
ID: 6280820
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 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…
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.

760 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

20 Experts available now in Live!

Get 1:1 Help Now