用VC 做了个外壳程序A,调用一个JTAG编程的命令行程序B,并且不显示B的界面。
B的显示信息通过管道已经可以在A上显示,但讨厌的是B运行过程中居然要求用户确认(Y或者N回车),如何实现? 我又建了一个管道想让A写,B作为input,但是好像不行,运行后连B的信息都不能显示到A上了,晕!
CString CAVRJtag::InvokeJTAG(CString sExeFile,CString sPara,DWORD * dwError) { CString sResult=""; CString sPath;
sPath=sExeFile;//执行文件名称 sPath+=" "; sPath+=sPara;//参数 //执行文件
*dwError=0;
LPPROCESS_INFORMATION lppi; PROCESS_INFORMATION pi; HANDLE hReadPipe,hWritePipe; HANDLE hReadPipe1,hWritePipe1; SECURITY_ATTRIBUTES lsa,lsa1; STARTUPINFO si;
lsa.nLength=sizeof(SECURITY_ATTRIBUTES); lsa.lpSecurityDescriptor=NULL; lsa.bInheritHandle=TRUE; lsa1.nLength=sizeof(SECURITY_ATTRIBUTES); lsa1.lpSecurityDescriptor=NULL; lsa1.bInheritHandle=TRUE;
lppi=π if (!CreatePipe(&hReadPipe,&hWritePipe,&lsa,0))//dos程序写,本程序读取 // create the pipe for output { //AfxMessageBox("Could't create Pipe\n"); *dwError=1; return sResult; } if (!CreatePipe(&hReadPipe1,&hWritePipe1,&lsa1,0))//本程序写,DOS程序读取 // create the pipe for input { //AfxMessageBox("Could't create Pipe\n"); *dwError=1; return sResult; }
memset(&si,0,sizeof(STARTUPINFO)); si.cb=sizeof(STARTUPINFO); //important part-making SW_HIDE we prevent console to open up si.dwFlags=STARTF_USESTDHANDLES| STARTF_USESHOWWINDOW; si.wShowWindow=SW_HIDE; si.hStdOutput=hWritePipe; //si.hStdInput=hReadPipe1; //这句一加上连第一个管道都通讯不上了 BOOL rst; rst=CreateProcess(NULL,sPath.GetBuffer(0), NULL, NULL, TRUE, 0, NULL,NULL, &si, &pi); DWORD dg=GetLastError(); if (!rst) { *dwError=1; //AfxMessageBox("Could't Create process\r\n"); return sResult; } DWORD cchReadBuffer;//number of bytes read or to be writen CString sText; TCHAR ph[5000]; for(;;) { cchReadBuffer=0; if(PeekNamedPipe(hReadPipe,ph,1,&cchReadBuffer,NULL,NULL)) //ReadFile is blocking call so we should first //check if we have something to read break; if(cchReadBuffer) {//yes we do ,so read it and print out to the edit ctrl if(!ReadFile(hReadPipe, ph, 4096,&cchReadBuffer,//number of bytes actually read NULL)) { break; } CString strTmp=""; ph[cchReadBuffer]=0; strTmp=ph; sResult+=strTmp; if(sResult.Find("Are you still want to continue")!=-1)//&&sResult.Find("Yes\\No?")!=-1) { Sleep(100); char buf[10]; buf[0]='N';buf[1]=13;buf[2]=10;buf[3]=0; DWORD bw; WriteFile(hWritePipe1,buf,4,&bw,NULL); } } else { //no we don't have anything in the buffer //maybe the program exited if(WaitForSingleObject(pi.hProcess,0)==WAIT_OBJECT_0) break; //so we should exit either } Sleep(50); //continue otherwise } ph[cchReadBuffer]=0; sResult+=ph; CloseHandle(hReadPipe); CloseHandle(pi.hThread); CloseHandle(pi.hProcess); CloseHandle(hWritePipe); return sResult; } |