调用Windows API创建创建子进程,CreateProcess命令行参数不能直接使用字符串(貌似更高版本的VS没这方面的烦恼)。
#include<stdio.h>
#include<string.h>
#include<windows.h>
int
main(void)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
TCHAR scommand[] = TEXT("C:\\WINDOWS\\system32\\mspaint.exe");
/*
** allocate memory
*/
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
/*
** create child process
*/
if(!CreateProcess(NULL,
scommand, // 直接使用参数"C:\\WINDOWS\\system32\\mspaint.exe"
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi))
{
fprintf(stderr, "Create Process Failed");
return -1;
}
/*
** parent will wait for the child to complete
*/
WaitForSingleObject(pi.hProcess, INFINITE);
printf("Child Complete\n");
/*
** close handles
*/
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
直接使用字符串作为参数,报出的错误。详细原因参考:http://blog.csdn.net/hgy413/article/details/6200983