我在我的程序中建立了cmd.exe进程,并通过命名通道向其发送命令和接收信息。第一次的发送和执行返回信息是正常的,但第二次就会返回more?究竟是什么意思。有人说是一个屏幕不够显示就会这样,但我的信息无论多还是少都会这样阿,就连我执行两次ver命令都会这样.
我的代码:
#include<windows.h>
#include<winbase.h>
#include<iostream.h>
#include<stdio.h>SECURITY_ATTRIBUTES stPipeA,stPipeB;
HANDLE hPipeARead,hPipeAWrite;//建立通道
HANDLE hPipeBRead,hPipeBWrite;//建立通道
STARTUPINFO stInfo;
PROCESS_INFORMATION stProInfo;
char Cmd[MAX_PATH]="C:\\WINDOWS\\system32\\cmd.exe";
 
int main()
{
cout<<"Pipe Program!"<<endl; memset(&stPipeA,0,sizeof(stPipeA));
stPipeA.bInheritHandle=TRUE;
    stPipeA.lpSecurityDescriptor=NULL;
stPipeA.nLength=sizeof(SECURITY_ATTRIBUTES);

if(!CreatePipe(&hPipeAWrite,&hPipeARead,&stPipeA,0))
{
cout<<"CreatePipe Error!"<<endl;
} memset(&stPipeB,0,sizeof(stPipeB));
stPipeB.bInheritHandle=TRUE;
stPipeB.lpSecurityDescriptor=NULL;
stPipeB.nLength=sizeof(SECURITY_ATTRIBUTES); if(!CreatePipe(&hPipeBWrite,&hPipeBRead,&stPipeB,0))
{
        cout<<"CreatePipe Error!"<<endl;
}    GetStartupInfo(&stInfo);
stInfo.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
stInfo.hStdInput=hPipeAWrite;
stInfo.hStdError=stInfo.hStdOutput=hPipeBRead;
stInfo.wShowWindow=SW_SHOW;
    
    //创建cmd.exe进程
if(!CreateProcess(Cmd,NULL,NULL,NULL,TRUE,0,NULL,NULL,&stInfo,&stProInfo))
{
       cout<<"CreateProcess Error!"<<endl;
} char bb[1024]={'\n'};//用来保存cmd显示的内容
unsigned long size=0; //显示在当前目录下的文件
if(!WriteFile(hPipeARead,"ver\r\n",7,&size,NULL))
{
       cout<<"WriteFile Error!"<<endl;
}
    
WaitForSingleObject(stProInfo.hProcess,WAIT_TIMEOUT); //读去返回信息
if(!ReadFile(hPipeBWrite,bb,1024,&size,NULL))
{
       cout<<"ReadFile Error"<<endl;
   return 0;
}
//输出返回信息
cout<<bb<<endl;//第二次输入dir命令
///////////////////////////////////////////////////////////////////////////////////
memset(bb,0,1024);
if(!WriteFile(hPipeARead,"ver\r\n",7,&size,NULL))
{
       cout<<"WriteFile Error!"<<endl;
}
    
WaitForSingleObject(stProInfo.hProcess,WAIT_TIMEOUT); //读去返回信息
if(!ReadFile(hPipeBWrite,bb,1024,&size,NULL))
{
       cout<<"ReadFile Error"<<endl;
   return 0;
}
//输出返回信息
cout<<bb<<endl;////////////////////////////////////////////////////////////////////////////////// CloseHandle(hPipeARead);
CloseHandle(hPipeAWrite);
 
CloseHandle(hPipeBRead);
CloseHandle(hPipeBWrite); CloseHandle(stProInfo.hProcess );
CloseHandle(stProInfo.hThread );return 1;
  
}
返回信息:
Pipe Program!
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.D:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\Pipe>ver<---------第一次verMicrosoft Windows XP [版本 5.1.2600]D:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\Pipe><------------第二次ver
More?
Press any key to continue

解决方案 »

  1.   

    #include<windows.h>
    #include<winbase.h>
    #include<iostream.h>
    #include<stdio.h>SECURITY_ATTRIBUTES stPipeA,stPipeB;
    HANDLE hPipeARead,hPipeAWrite;//建立通道
    HANDLE hPipeBRead,hPipeBWrite;//建立通道
    STARTUPINFO stInfo;
    PROCESS_INFORMATION stProInfo;
    char Cmd[MAX_PATH]="C:\\WINDOWS\\system32\\cmd.exe";
     
    int main()
    {
    cout<<"Pipe Program!"<<endl; memset(&stPipeA,0,sizeof(stPipeA));
    stPipeA.bInheritHandle=TRUE;
        stPipeA.lpSecurityDescriptor=NULL;
    stPipeA.nLength=sizeof(SECURITY_ATTRIBUTES);

    int count=0;
    there:
    count++;
    if(!CreatePipe(&hPipeAWrite,&hPipeARead,&stPipeA,0))
    {
    cout<<"CreatePipe Error!"<<endl;
    } memset(&stPipeB,0,sizeof(stPipeB));
    stPipeB.bInheritHandle=TRUE;
    stPipeB.lpSecurityDescriptor=NULL;
    stPipeB.nLength=sizeof(SECURITY_ATTRIBUTES); if(!CreatePipe(&hPipeBWrite,&hPipeBRead,&stPipeB,0))
    {
            cout<<"CreatePipe Error!"<<endl;
    }    GetStartupInfo(&stInfo);
    stInfo.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
    stInfo.hStdInput=hPipeAWrite;
    stInfo.hStdError=stInfo.hStdOutput=hPipeBRead;
    stInfo.wShowWindow=SW_SHOW;
        
        //创建cmd.exe进程
    if(!CreateProcess(Cmd,NULL,NULL,NULL,TRUE,0,NULL,NULL,&stInfo,&stProInfo))
    {
           cout<<"CreateProcess Error!"<<endl;
    } char bb[1024]={'\n'};//用来保存cmd显示的内容
    unsigned long size=0;
    //显示在当前目录下的文件
    if(!WriteFile(hPipeARead,"ver\r\n",7,&size,NULL))
    {
           cout<<"WriteFile Error!"<<endl;
    }
        
    WaitForSingleObject(stProInfo.hProcess,WAIT_TIMEOUT); //读去返回信息
    if(!ReadFile(hPipeBWrite,bb,1024,&size,NULL))
    {
           cout<<"ReadFile Error"<<endl;
       return 0;
    }
    //输出返回信息
    cout<<bb<<endl; if(count<2)
    {
    goto there;
    }
    //第二次输入dir命令
    ///////////////////////////////////////////////////////////////////////////////////
    /* memset(bb,0,1024);
    if(!WriteFile(hPipeARead,"ver\r\n",7,&size,NULL))
    {
           cout<<"WriteFile Error!"<<endl;
    }
        
    WaitForSingleObject(stProInfo.hProcess,WAIT_TIMEOUT); //读去返回信息
    if(!ReadFile(hPipeBWrite,bb,1024,&size,NULL))
    {
           cout<<"ReadFile Error"<<endl;
       return 0;
    }
    //输出返回信息
    cout<<bb<<endl;
    */
    ////////////////////////////////////////////////////////////////////////////////// CloseHandle(hPipeARead);
    CloseHandle(hPipeAWrite);
     
    CloseHandle(hPipeBRead);
    CloseHandle(hPipeBWrite); CloseHandle(stProInfo.hProcess );
    CloseHandle(stProInfo.hThread );return 1;
      
    }