#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不会自动结束。
请高人指点,这个是为什么?怎么让他不结束?

解决方案 »

  1.   

    知道了,是因为
    设置了这个
    STARTF_USESTDHANDLES
    可是能给我解释一下是为什么吗?
      

  2.   

    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);
      

  3.   

    ShellExecute(NULL,   "open",   "cmd.exe",   NULL,   NULL,   SW_SHOW);
      

  4.   

     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. 
      

  5.   

    si.hStdError = NULL; 
    si.hStdOutput = NULL; 
    这个是输出定向到了NULL,就有问题吧....
      

  6.   

    在不是必要的场合,还是计较推荐ShellExecute/ShellExecuteEx ,这玩意来的比较方便。
      

  7.   

    STARTF_USESTDHANDLES是重定向子进程的输入/输出设备时使用的,如果不指定该标志,则忽略hStdInput、hStdOutput等成员,使用标准输入/输出设备。
      

  8.   

    CreateProcess,ShellExcute,WinExec均是不错的悬着,每种方法都可以实现
      

  9.   

    貌似要给CMD传个参数 "/c" 还是"/k" 。记得不太清楚了,这样他就不会闪下就结束了。
      

  10.   

    一创建进程就消失,因为那个标志没设,STARTUPINFO被忽略了
      

  11.   


     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 );