100分求助一个关于在线程中read、write pipe的问题!

u010249954 2013-04-11 02:31:07
想要调用c:\\windows\\system32\\cmd.exe,让用户输入一个字符串,然后显示cmd执行的echo;

但是输入ping 127.0.0.1之后,并没有echo出来任何东西,一开始的cmd是可以显示的!


求助各位一下为何这么样?

下面是我的代码:


#include "stdafx.h"
#include <iostream>
using namespace std;
#include <stdio.h>
#include <Windows.h>
#include "process.h"

#define THREAD_NUM 2

HANDLE hReadPipe1, hWritePipe1, hReadPipe2, hWritePipe2;
HANDLE handle[THREAD_NUM];
DWORD byteRead, byteWrite;
string write_message;

unsigned int __stdcall ThreadWritePipe(PVOID pData)
{
//进程的输入重定向到hReadPipe2,所以从hWritePipe2写入
while (true)
{
if (!write_message.empty())
{
if (!WriteFile(hWritePipe2, (LPCVOID)write_message.c_str(), write_message.length() + 1, &byteWrite, NULL))
{
return 1;
}
else
{
write_message = "";
}
}
}
return 0;
}

unsigned int __stdcall ThreadReadPipe(PVOID pData)
{
//这个不知道还有没有更好的办法,用while (true)总觉得很土
BYTE buffer[1024];
while(true)
{
RtlZeroMemory(buffer,1024);
//进程的输出重定向到hWritePipe1,所以从hReadPipe1读取
if(ReadFile(hReadPipe1,buffer,1023,&byteRead,NULL) == NULL)
{
break;
}
cout<<buffer;
Sleep(20);
}
return 0;
}

int main(int argc,char* * argv)
{
//定义四个句炳保留两个管道的信息
SECURITY_ATTRIBUTES sat;
STARTUPINFO startupinfo;
PROCESS_INFORMATION pinfo;

string rString;
string m_Host="c:\\windows\\system32\\cmd.exe ";

sat.nLength=sizeof(SECURITY_ATTRIBUTES);
sat.bInheritHandle=true;
sat.lpSecurityDescriptor=NULL;
//创建管道,它用来做子进程的输出
if(!CreatePipe(&hReadPipe1, &hWritePipe1, &sat, NULL))
{
cout<<("Create Pipe Error!")<<endl;
return 1;
}

//创建管道,它用来做子进程的输入
if(!CreatePipe(&hReadPipe2, &hWritePipe2, &sat, NULL))
{
cout<<"Create Pipe Error!"<<endl;
return 1;
}

startupinfo.cb=sizeof(STARTUPINFO);
//用GetStartupInfo获得当前进程的参数,否则STARTUPINFO参数太多,会让人抓狂
GetStartupInfo(&startupinfo);
startupinfo.hStdInput = hReadPipe2;
startupinfo.hStdError = hWritePipe1;
startupinfo.hStdOutput= hWritePipe1;
//要有STARTF_USESTDHANDLES,否则hStdInput, hStdOutput, hStdError无效
startupinfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
startupinfo.wShowWindow=SW_HIDE;
if(!CreateProcess(NULL, (char*)m_Host.c_str(), NULL, NULL, TRUE, NULL, NULL, NULL, &startupinfo, &pinfo))
{
cout<<("create process error!")<<endl;
return 1;
}

handle[0] = (HANDLE)_beginthreadex(0, 0, ThreadWritePipe, 0, 0, 0);
handle[1] = (HANDLE)_beginthreadex(0, 0, ThreadReadPipe, 0, 0, 0);

char str[1024] = {'\0'};
bool flag = true;
while(flag)
{
gets(str);
if (str[0] == 'q')
{
flag = !flag;
}
else
{
write_message += str;
}
}
return 0;

WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE);

CloseHandle(hReadPipe1);
CloseHandle(hReadPipe2);
CloseHandle(hWritePipe1);
CloseHandle(hWritePipe2);

return 0;
}
...全文
254 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
u010249954 2013-04-11
  • 打赏
  • 举报
回复
引用 4 楼 zengraoli 的回复:
C/C++ code?12345678910111213141516171819202122unsigned int __stdcall ThreadWritePipe(PVOID pData){ //进程的输入重定向到hReadPipe2,所以从hWritePipe2写入 while (true) { if (!write_message……
Ok 结贴!
  • 打赏
  • 举报
回复

unsigned int __stdcall ThreadWritePipe(PVOID pData)
{
	//进程的输入重定向到hReadPipe2,所以从hWritePipe2写入
	while (true)
	{
		if (!write_message.empty())
		{
			write_message += "\r\n";
			if (!WriteFile(hWritePipe2, (LPCVOID)write_message.c_str(), write_message.length(), &byteWrite, NULL))
			{
				printf("execu ThreadWritePipe\n");
				return 1;
			}
			else
			{
				write_message = "";
			}
		}
		Sleep(100);
	}
	return 0;
}
u010249954 2013-04-11
  • 打赏
  • 举报
回复
引用 2 楼 kyotrue 的回复:
WriteFile(hWritePipe2, (LPCVOID)write_message.c_str(), write_message.length() + 1, &byteWrite... 这个,为啥要write_message.length() + 1?结束符不该写进去吧。。。
写不写其实没关系
kyotrue 2013-04-11
  • 打赏
  • 举报
回复
WriteFile(hWritePipe2, (LPCVOID)write_message.c_str(), write_message.length() + 1, &byteWrite... 这个,为啥要write_message.length() + 1?结束符不该写进去吧。。。
u010249954 2013-04-11
  • 打赏
  • 举报
回复
在线等!谢谢!

15,471

社区成员

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

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