打印

这人多,问个windows下向命令行程序发送字符的问题。

[复制链接]
1893|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
音乐乐乐|  楼主 | 2009-5-11 12:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
 用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;
}

相关帖子

沙发
mohanwei| | 2009-5-11 12:27 | 只看该作者

发到CSDN上吧……

使用特权

评论回复
板凳
McuPlayer| | 2009-5-11 17:29 | 只看该作者

这个做法基本是对的

创建PIPE
然后执行那个程序(命令行程序)
然后把输出分析后,再显示给用户

把你遇到的具体问题说出来,看能不能帮到你。

使用特权

评论回复
地板
李冬发| | 2009-5-11 18:50 | 只看该作者

你不用了管道吗,管道不仅可以输出,也还可以输入的。

使用特权

评论回复
5
音乐乐乐|  楼主 | 2009-5-12 09:59 | 只看该作者

问题出在这里,但不知道原因

if(!ReadFile(hReadPipe,    ph,    4096,&cchReadBuffer,//number of bytes actually read
                NULL))
在读取管道数据的时候发现,只有等到被调用的命令行程序B运行结束后才能返回数据,这是怎么回事呢?

使用特权

评论回复
6
computer00| | 2009-5-12 10:10 | 只看该作者

看看有没有不让确认的参数?

使用特权

评论回复
7
音乐乐乐|  楼主 | 2009-5-12 10:57 | 只看该作者

没有,呵呵,必须要确认,就是烧熔丝的时

 这个问题好像比较普遍,就是管道要等到被调用程序结束后才能收到数据,正在搜索方法...

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

350

主题

2293

帖子

3

粉丝