关于管道的问题。。
程序代码:
#include <WINDOWS.H> #include <stdio.h> DWORD WINAPI ThreadInputProc(LPVOID lpParameter); HANDLE hRead1=NULL,hWrite1=NULL; HANDLE hRead2=NULL,hWrite2=NULL; CRITICAL_SECTION g_cs; BOOL threadFlag=FALSE; void main() { InitializeCriticalSection(&g_cs); SECURITY_ATTRIBUTES sa; sa.bInheritHandle=TRUE; sa.lpSecurityDescriptor=NULL; sa.nLength=sizeof(SECURITY_ATTRIBUTES); if(!CreatePipe(&hRead1,&hWrite1,&sa,0) || !CreatePipe(&hRead2,&hWrite2,&sa,0)) { printf("创建管道失败"); return; } char lzCmdline[200]; GetSystemDirectory(lzCmdline,200); strcat(lzCmdline,"\\cmd.exe"); STARTUPINFO si; GetStartupInfo(&si); si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; si.hStdInput = hRead1; si.hStdOutput = si.hStdError = hWrite2; si.wShowWindow = SW_HIDE; PROCESS_INFORMATION pi; if(!CreateProcess(lzCmdline,NULL,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi)) { printf("创建进程失败"); return ; } CreateThread(NULL,0,ThreadInputProc,NULL,0,0); char readbuff[1024]; memset(readbuff,0,1024); DWORD dwRead,dwTotalAvail = 0; BOOL bRet=FALSE; while (TRUE) { EnterCriticalSection(&g_cs); dwTotalAvail=0; if (!threadFlag) { bRet = PeekNamedPipe(hRead2, NULL, 0, NULL, &dwTotalAvail, NULL); if (bRet && dwTotalAvail > 0) { ReadFile(hRead2,readbuff,1024,&dwRead,NULL); printf("%s\n",readbuff); threadFlag=TRUE; } } LeaveCriticalSection(&g_cs); } } DWORD WINAPI ThreadInputProc(LPVOID lpParameter) { char writebuff[100]; DWORD dwWrite; while(TRUE) { EnterCriticalSection(&g_cs); if(threadFlag) { printf("Please Input cmd:\n"); gets(writebuff); if(!WriteFile(hWrite1, writebuff, sizeof(writebuff)+1, &dwWrite, NULL)) { printf("写入失败"); return FALSE; } threadFlag=FALSE; } LeaveCriticalSection(&g_cs); } return TRUE; }本来想建立个双管道,创建一个cmd进程,一个管道进行cmd命令的输入(另外创建一个线程),另一个管道进行cmd执行结果的输出(主线程),但是出现问题了,在那哪????求指出。。