#include "stdio.h"
#include "process.h"
#include "windows.h"int main(int argc, char* argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = NULL;
si.hStdOutput = NULL;
si.wShowWindow = SW_SHOW;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
if (!CreateProcess(NULL , "c:\\windows\\system32\\cmd.exe"
,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi))
{
printf("Error on create cmd process");
}
return 0;
}运行以上代码,创建的cmd.exe进程马上就自动结束了。如果把cmd.exe改成notepad.exe,则notepad.exe不会自动结束。
请高人指点,这个是为什么?怎么让他不结束?
#include "process.h"
#include "windows.h"int main(int argc, char* argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = NULL;
si.hStdOutput = NULL;
si.wShowWindow = SW_SHOW;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
if (!CreateProcess(NULL , "c:\\windows\\system32\\cmd.exe"
,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi))
{
printf("Error on create cmd process");
}
return 0;
}运行以上代码,创建的cmd.exe进程马上就自动结束了。如果把cmd.exe改成notepad.exe,则notepad.exe不会自动结束。
请高人指点,这个是为什么?怎么让他不结束?
设置了这个
STARTF_USESTDHANDLES
可是能给我解释一下是为什么吗?
PROCESS_INFORMATION pi;si.dwFlags=STARTF_USESHOWWINDOW;
si.wShowWindow=TRUE;
BOOL bRet=::CreateProcess(
NULL,
szCommandLine,
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);if(bRet)
{
printf("新进程的进程ID号:%d\n",pi.dwProcessId);
printf("新进程的主线程ID号:%d\n",pi.hThread);
}::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
If this value is specified, sets the standard input of the process, standard output, and standard error handles to the handles specified in the hStdInput, hStdOutput, and hStdError members of the STARTUPINFO structure. The CreateProcess function's fInheritHandles parameter must be set to TRUE for this to work properly.
If this value is not specified, the hStdInput, hStdOutput, and hStdError members of the STARTUPINFO structure are ignored.
si.hStdOutput = NULL;
这个是输出定向到了NULL,就有问题吧....
STARTUPINFO si;
PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) ); char szCmdline[] ="c:\\windows\\system32\\cmd.exe /k"; // Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
szCmdline, // 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
)
{
//printf( "CreateProcess failed (%d)\n", GetLastError() );
return;
} // Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE ); // Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );