HANDLE  hMyReadPipe,
hMyWritePipe,
hProcessReadPipe, 
hProcessWritePipe; SECURITY_ATTRIBUTES sa;
DWORD BytesWrite;
TCHAR Buffer[10000];
int dwByteScan; sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL; 
sa.bInheritHandle = TRUE;  CreatePipe(&hMyReadPipe,&hProcessWritePipe,&sa,10000);
CreatePipe(&hProcessReadPipe,&hMyWritePipe,&sa,10000); STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = hProcessWritePipe;
si.hStdOutput = hProcessWritePipe;
si.wShowWindow = SW_SHOW;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.hStdInput = hMyWritePipe; CreateProcess(NULL,
"C:\\windows\\system32\\cmd.exe",NULL,NULL,
TRUE,
NORMAL_PRIORITY_CLASS|CREATE_NEW_CONSOLE|CREATE_DEFAULT_ERROR_MODE,NULL,NULL,
&si,&pi
); CWinThread * hReadThread = AfxBeginThread((AFX_THREADPROC)ReadThread ,hMyReadPipe );
while(1)
{
scanf("%s",&Buffer);
dwByteScan = strlen(Buffer);
if(!strcmp(Buffer,"exit"))
{
TerminateProcess(pi.hProcess,0);
break;
}
Buffer[dwByteScan] = '\r'; 
Buffer[dwByteScan+1] = '\0'; 
WriteFile(hMyWritePipe,Buffer,dwByteScan+2,&BytesWrite,NULL);
} TerminateThread(hReadThread->m_hThread,0);为什么CMD.exe启动后立刻结束了?各位麻烦帮帮我啊

解决方案 »

  1.   

    你CMD后面跟参数了吗?
    启动CMD用不着CreateProcess把,用ShellExecute或者WinExec就可以了吧。
      

  2.   


    iRetVal = CreateProcess("C:\\windows\\system32\\cmd.exe", strCmdLine, NULL, NULL, FALSE, 0, NULL, 
    m_strWorkPath, &si, &pi);//strCmdLine为要传入的参数
                                    //m_strWorkPath为当前工作目录
      

  3.   

    感觉你的2个管道的4个句柄使用的不对。
    si.hStdInput = hMyWritePipe; 
    WriteFile(hMyWritePipe,Buffer,dwByteScan+2,&BytesWrite,NULL); 
    看到了吧,你向hMyWritePipe写内容,那么si.hStdInput的输入就应该是hProcessReadPipe。你再整理一下思路,搞清楚到底哪个句柄读,哪个句柄写。