我在我的程序中建立了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
我的代码:
#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
解决方案 »
- 推荐一个自己写的换肤Skin
- 如何在系统剪贴板没有内容时,将粘贴菜单变灰;有内容,则可用?
- 我对CWnd::Create的理解,不知道对不对
- 如何解析一个MAC包
- 执行CoGetClassObject()时,客户端程序报错是怎么回事,DllGetClassObject()函数隐含调用成功
- 毕业两年了,什么都没有,郁闷,散分
- 如何在程序中动态删除一个没有析构函数的对象?
- 请问,"DSP"是什么东西,这又是什么的简称?
- 欢迎讨论:关于“ISAPI”!
- 如何在unicode的编译环境下,调用ansi的函数.
- 小弟我写了一个邮件发送程序,要和smtp.163.com连接,,但是,,如何输入自己的邮箱的帐号和密码??用vc6.0,,代码如下。。。。。。 在
- 请问如何定义全局函数?
#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;
}