您的位置:首页 > 编程语言

双管道(CreatePipe)与cmd.exe进程间通信的有关问题 完美解决

2013-11-17 16:18 375 查看
最近研究怎么在别人电脑里开个CMD shell,就是直接用CMD执行我们的命令,遇到不少问题,但终于把问题完美解决了!!!!

源代码:http://download.csdn.net/detail/aq782645210/6566225

问题1:为什么启动cmd.exe时能读到(ReadFile)的初始化信息,但是再WriteFile(往管道里写数据后),cmd却没有任何信息,即PeekNamedPipe函数中的参数lBytesRead总是为零?难道是管道没写进去?

问题2:怎么有时输出 “MORE? ”的字符串?这是什么东西?

解决方案看代码:#include "stdafx.h"
#include <stdio.h>
#include <WINDOWS.H>
#define SEND_BUFF_SIZE 1024

//实现去除执行结果中的 命令字符串+"\n"
void print(char *cmdstr)
{
while(*((char*)cmdstr++)!='\n');
printf(cmdstr);
}

int main()
{
HANDLE hReadPipe1,hWritePipe1,hReadPipe2,hWritePipe2; //四个HANDLE 用来创建两个管道
CHAR Buff[SEND_BUFF_SIZE] = {0};
CHAR sendBuff[SEND_BUFF_SIZE] = ("dir \n");

//安全属性的东西
SECURITY_ATTRIBUTES sa;
sa.nLength=sizeof(sa);
sa.lpSecurityDescriptor=0;
sa.bInheritHandle=true;
int ret;

if(!CreatePipe(&hReadPipe1,&hWritePipe1,&sa,0))//创建两个匿名管道 导向DOS输入输出通道
{
return -1;
}
if(!CreatePipe(&hReadPipe2,&hWritePipe2,&sa,0))
{
return -1;
}

//启动信息
STARTUPINFO si;
ZeroMemory(&si,sizeof(si));
//GetStartupInfo(&si);
si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdInput = hReadPipe2;
si.hStdOutput = si.hStdError = hWritePipe1;

char cmdLine[256] = {0};
GetSystemDirectory(cmdLine,sizeof(cmdLine));
strcat(cmdLine, ("\\cmd.exe"));

PROCESS_INFORMATION ProcessInformation;
if(CreateProcess(cmdLine,NULL,NULL,NULL,TRUE,0,NULL,NULL,&si,&ProcessInformation) == 0)
{
return -1;
}

unsigned long lBytesRead,lBytesWrite;//读写数量存放变量

while(TRUE){
lBytesRead=0;
ZeroMemory(Buff,sizeof(Buff));
ret=PeekNamedPipe(hReadPipe1,Buff,SEND_BUFF_SIZE,&lBytesRead,0,0);//管道是否有数据可读
if(lBytesRead)
{
//第一次可以读到cmd的初始化信息
memset(Buff, 0, sizeof(Buff));
ret=ReadFile(hReadPipe1,Buff,SEND_BUFF_SIZE,&lBytesRead,0);//读取管道里的数据
//如果读到数据,则对数据进行下一步处理。。。。。。。
//printf(Buff);
print(Buff);
}
else{
//解决方案 估计windows是以"\r\n"结束的吧,所以要在命令执行后,显示完全后写入这个
/***********************************************************/
WriteFile(hWritePipe2, "\r\n",2,&lBytesWrite,0);
Sleep(100);
ret=ReadFile(hReadPipe1,Buff,SEND_BUFF_SIZE,&lBytesWrite,0);//读取管道里的数据
/***********************************************************/

//读入输入数据 以回车结束
char ch;
ZeroMemory(sendBuff,sizeof(sendBuff));
int count=0;
while((ch=getchar())!='\n')
{
sendBuff[count]=ch;
count++;
}
//加入回车
strcat(sendBuff,"\r\n");
//写入数据,执行命令
if(!WriteFile(hWritePipe2, sendBuff,sizeof(sendBuff),&lBytesWrite,0))
{
printf("WriteFile Error!!\r\n");
return -1;
}
//延时,等待程序执行
Sleep(100);
}
}

}

//解决方案  估计windows是以"\r\n"结束的吧,所以要在命令执行后,显示完全后写入这个
/***********************************************************/
WriteFile(hWritePipe2, "\r\n",2,&lBytesWrite,0);
Sleep(100);
ret=ReadFile(hReadPipe1,Buff,SEND_BUFF_SIZE,&lBytesWrite,0);//读取管道里的数据
/***********************************************************/

蓝色部分就是解决代码,原来程序执行完后,并没有像DOS那样直接加上"\r\n",就是结束符,所以我们只有自己动手了,经过测试完全OK

我相信大家都可以写远程SHELL了吧,哦哈哈哈~~~~

已经在看雪同步发布,呵呵 ~~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息