2012-02-14 345 views
1

我想这样的事情,如何将DWORD转换为char *?

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

其中argschar *,我需要作为参数传递到其他可执行文件。

+2

DWORD是一个整数类型。将它转换为char *'没有任何意义。 – 2012-02-14 06:01:17

+0

但我需要这个'DWORD'作为参数传递给使用CreateProcess()调用的另一个exe文件。有什么建议么? – psp 2012-02-14 06:04:15

+0

[C++ long to string](http://stackoverflow.com/questions/947621/c-long-to-string)或[如何将int转换为C中的字符串](http:// stackoverflow。 COM /问题/ 8257714 /如何对转换-AN-INT-到字符串中-C)。 – user7116 2012-02-14 06:04:22

回答

7

您可以用sprintf

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

这将processInfo.dwProcessId转换成然后可以通过你所使用的字符。

+0

在所有可能的选项中,我认为这是最优化的选项。 – psp 2012-02-14 06:22:17

+0

由于DWORD是一个无符号整数,因此应该使用'%u'。 – user7116 2012-02-14 15:11:13

+8

实际上,'DWORD'是一个'unsigned long',你应该使用'%lu'作为格式说明符。 – tomlogic 2012-03-22 19:56:41

1

虽然没有直接 “转换为char*”,下面应该做的伎俩:

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(); 
1

要转换DWORD为char *您可以使用_ultoa/_ultoa_s

看到这里可能会帮助你。 link1