6

I am trying something like this,

PROCESS_INFORMATION processInfo = .....
strcat( args, processInfo.dwProcessId);

where args is a char * which I need to pass as an argument to another executable.

Cody Gray
  • 230,875
  • 49
  • 477
  • 553
psp
  • 111
  • 1
  • 1
  • 10
  • 2
    A `DWORD` is an integer type. It doesn't make any sense to convert it to a `char*`. – Cody Gray Feb 14 '12 at 06:01
  • But I need this `DWORD` to be passed as an argument to another exe called using CreateProcess(). Any suggestions? – psp Feb 14 '12 at 06:04
  • possible duplicate of [C++ long to string](http://stackoverflow.com/questions/947621/c-long-to-string) or [How to convert an int to string in C](http://stackoverflow.com/questions/8257714/how-to-convert-an-int-to-string-in-c). – user7116 Feb 14 '12 at 06:04

4 Answers4

11

You can use sprintf

char procID[10];
sprintf(procID, "%d", processInfo.dwProcessId);

This will convert the processInfo.dwProcessId into a character which can then be used by you.

spex
  • 1,013
  • 8
  • 21
Ajit Vaze
  • 2,657
  • 2
  • 19
  • 23
3

To convert DWORD to char * you can use _ultoa / _ultoa_s

see here might help you.link1

Java
  • 2,401
  • 9
  • 46
  • 83
2

MSDN has pretty good documentation, check out the Data Conversion page.

There's sprintf() too.

Alexey Frunze
  • 59,618
  • 10
  • 77
  • 173
1

While not directly "converting to a char*", the following should do the trick:

std::ostringstream stream;
stream << processInfo.dwProcessId;
std::string args = stream.str();

// Then, if you need a 'const char*' to pass to another Win32
// API call, you can access the data using:
const char * foo = args.c_str();
André Caron
  • 43,255
  • 11
  • 62
  • 121