CreateProcess 进程自动结束了?

xmokay 2009-09-15 10:55:02
#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不会自动结束。
请高人指点,这个是为什么?怎么让他不结束?
...全文
170 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
哈利路亚1874 2009-09-16
  • 打赏
  • 举报
回复
CreateProcess,ShellExcute,WinExec均是不错的悬着,每种方法都可以实现
  • 打赏
  • 举报
回复

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 );
MoXiaoRab 2009-09-16
  • 打赏
  • 举报
回复
一创建进程就消失,因为那个标志没设,STARTUPINFO被忽略了
  • 打赏
  • 举报
回复
貌似要给CMD传个参数 "/c" 还是"/k" 。记得不太清楚了,这样他就不会闪下就结束了。
曾经的董胖 2009-09-15
  • 打赏
  • 举报
回复
在不是必要的场合,还是计较推荐ShellExecute/ShellExecuteEx ,这玩意来的比较方便。
cnzdgs 2009-09-15
  • 打赏
  • 举报
回复
STARTF_USESTDHANDLES是重定向子进程的输入/输出设备时使用的,如果不指定该标志,则忽略hStdInput、hStdOutput等成员,使用标准输入/输出设备。
dirdirdir3 2009-09-15
  • 打赏
  • 举报
回复
si.hStdError = NULL;
si.hStdOutput = NULL;


这个是输出定向到了NULL,就有问题吧....
gaoxiaowei 2009-09-15
  • 打赏
  • 举报
回复
STARTF_USESTDHANDLES:msdn解释
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.
Ghost90 2009-09-15
  • 打赏
  • 举报
回复
ShellExecute(NULL, "open", "cmd.exe", NULL, NULL, SW_SHOW);
gaoxiaowei 2009-09-15
  • 打赏
  • 举报
回复
char szCommandLine[]="cmd";

STARTUPINFO si={sizeof(si)};
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);
xmokay 2009-09-15
  • 打赏
  • 举报
回复
知道了,是因为
设置了这个
STARTF_USESTDHANDLES
可是能给我解释一下是为什么吗?
hhwei1985 2009-09-15
  • 打赏
  • 举报
回复
sf

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧