背景:用CreateProcess调用win32控制台程序,参数传递win32程序接受不到。
代码:
CString strFullPath = _T(“d:\\test.exe”);
CString strCmdLine = _T(“param”);
STARTUPINFO si = {sizeof(si)};
PROCESS_INFORMATION pi;
BOOL bRunProgram = CreateProcess(strFullPath.GetBuffer(), strCmdLine.GetBuffer(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
strFullPath.ReleaseBuffer();
strCmdLine.ReleaseBuffer();
if (bRunProgram)
{
// 等待程序结束
WaitForSingleObject(pi.hProcess,INFINITE);
CloseHandle(pi.hProcess);
}
错误原因:参数中少空格。
修改为
CString strCmdLine = _T("");
strCmdLine += _T(" ");//此处要添加空格
strCmdLine += _T(“ param”);
参数传递成功。