双管道(CreatePipe)与cmd.exe进程间通信的问题

nono507 2012-10-19 11:05:47
先贴代码:

HANDLE hReadPipe1,hWritePipe1,hReadPipe2,hWritePipe2; //四个HANDLE 用来创建两个管道

UINT __stdcall sendDataThread(LPVOID lpParam)//启动线程创建管道与cmd关联
{


char Buff[SEND_BUFF_SIZE];
TCHAR sendBuff[SEND_BUFF_SIZE];
SECURITY_ATTRIBUTES sa;
sa.nLength=12;
sa.lpSecurityDescriptor=0;
sa.bInheritHandle=true;
int ret;

if(!CreatePipe(&hReadPipe1,&hWritePipe1,&sa,0))//创建两个匿名管道
{
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;

TCHAR cmdLine[256] = {0};
GetSystemDirectory(cmdLine,sizeof(cmdLine));
wcscat(cmdLine,L"\\cmd.exe");

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

unsigned long lBytesRead;
Sleep(100);

while(TRUE) //开始循环读取或写入信息
{
ret=PeekNamedPipe(hReadPipe1,Buff,SEND_BUFF_SIZE,&lBytesRead,0,0);//管道是否有数据可读
if(lBytesRead)
{
//第一次可以读到cmd的初始化信息
ret=ReadFile(hReadPipe1,Buff,SEND_BUFF_SIZE,&lBytesRead,0);//读取管道里的数据
if(!ret)
break;
//如果读到数据,则对数据进行下一步处理。。。。。。。
}
else
{

if(!WriteFile(hWritePipe2,L"dir",sizeof(L"dir"),&lBytesRead,0))
{
return -1;
}
//这时写数据成功,但是管道没反应,PeekNamedPipe的lBytesRead为0
}

}

return 0;
}


问题是:
为什么启动cmd.exe时能读到(ReadFile)的初始化信息,
但是再WriteFile(往管道里写数据后),cmd却没有任何信息,即PeekNamedPipe函数中的参数lBytesRead总是为零?
难道是管道没写进去?
...全文
1894 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq782645210 2013-11-17
  • 打赏
  • 举报
回复
转载:http://blog.csdn.net/aq782645210/article/details/16370409 最近研究怎么在别人电脑里开个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了吧,哦哈哈哈~~~~ 已经在看雪同步发布,呵呵 ~~~
huangwenshen 2012-10-19
  • 打赏
  • 举报
回复
windows的结束符是\r\n吧 linux的只是一个\n
nono507 2012-10-19
  • 打赏
  • 举报
回复
解决了 是命令字符串后面要加个 ("dir \n")
不过应该是加\r\n吧?
nono507 2012-10-19
  • 打赏
  • 举报
回复
感谢楼上两位,我用 AfxMessageBox(CString(Buff)); 弹框
怎么有时会弹 “MORE? ”的字符串?这是什么东西?
huangwenshen 2012-10-19
  • 打赏
  • 举报
回复
我将取出来的buff再写入管道都可以,应该是你WriteFile的第二个和第三个参数的问题。


if(!WriteFile(hWritePipe2,Buff,sizeof(Buff),&lBytesRead,0))
{
return -1;
}
int errcode = GetLastError();
Eleven 2012-10-19
  • 打赏
  • 举报
回复
HANDLE hReadPipe1,hWritePipe1,hReadPipe2,hWritePipe2; //四个HANDLE 用来创建两个管道


UINT __cdecl sendDataThread(LPVOID lpParam)//启动线程创建管道与cmd关联
{
TCHAR Buff[SEND_BUFF_SIZE] = {0};
TCHAR sendBuff[SEND_BUFF_SIZE] = _T("dir \n");
SECURITY_ATTRIBUTES sa;
sa.nLength=sizeof(sa);
sa.lpSecurityDescriptor=0;
sa.bInheritHandle=true;
int ret;

if(!CreatePipe(&hReadPipe1,&hWritePipe1,&sa,0))//创建两个匿名管道
{
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;

TCHAR cmdLine[256] = {0};
GetSystemDirectory(cmdLine,sizeof(cmdLine));
_tcscat(cmdLine, _T("\\cmd.exe"));

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

unsigned long lBytesRead;
Sleep(100);

while(TRUE) //开始循环读取或写入信息
{
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);//读取管道里的数据
if(!ret)
break;
//如果读到数据,则对数据进行下一步处理。。。。。。。
}
else
{
if(!WriteFile(hWritePipe2, sendBuff,sizeof(sendBuff),&lBytesRead,0))
{
return -1;
}
//这时写数据成功,但是管道没反应,PeekNamedPipe的lBytesRead为0
}
AfxMessageBox(CString(Buff));
}

return 0;

}

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧