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 - on strike
  • 239,200
  • 50
  • 490
  • 574
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 - on strike 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,110
  • 10
  • 21
Ajit Vaze
  • 2,686
  • 2
  • 20
  • 24
3

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

see here might help you.link1

Java
  • 2,451
  • 10
  • 48
  • 85
2

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

There's sprintf() too.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
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
  • 44,541
  • 12
  • 67
  • 125