我按照MSDN上给出的例子,在主函数中调用CreateProcess去创建一个新的进程,尝试了种种方法,但总是以失败而告终。在98 和 XP 下,结果是一样的。源程序如下: (在VC6.0 下运行)
#include "windows.h"
#include "iostream.h"void main(void)
{
STARTUPINFO si;
PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) ); // Start the child process.
//这里的返回值总是为 0 !
if( !CreateProcess( NULL, // No module name (use command line).
"MyChildProcess", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
cout<<"CreateProcess failed."<<endl;
} // Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE ); // Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
#include "windows.h"
#include "iostream.h"void main(void)
{
STARTUPINFO si;
PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) ); // Start the child process.
//这里的返回值总是为 0 !
if( !CreateProcess( NULL, // No module name (use command line).
"MyChildProcess", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
cout<<"CreateProcess failed."<<endl;
} // Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE ); // Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
memset(&startupInfo,0,sizeof(STARTUPINFO));
startupInfo.wShowWindow = SW_MINIMIZE;
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
PROCESS_INFORMATION processInformation;
BOOL bRetCode = CreateProcess(szFileName,NULL,NULL, NULL, false,NORMAL_PRIORITY_CLASS,NULL,NULL,&startupInfo,&processInformation);
if(!bRetCode)
{
CString strError = "无法启动....";
return false;
}
而且你也应该自己跟踪一下GetLastError()的值分析原因,然后根据错误原因去找解决方法。
如果不存在,CreateProcess的返回值就会是0!