将CMD的输入输出重定向到自己的进程
2010-12-12 20:10
316 查看
void CreateMyPipe()
{
//创建管道
CreatePipe(&hReadPipe, &hWritePipe, NULL, NULL);
CreatePipe(&hChildReadPipe, &hChildWritePipe, NULL, NULL);
SetHandleInformation(hWritePipe, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
SetHandleInformation(hChildReadPipe, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
DWORD dwWord = PIPE_NOWAIT;
SetNamedPipeHandleState(hReadPipe, &dwWord, NULL, NULL);
//创建CMD进程
PROCESS_INFORMATION stProcessInfo;
STARTUPINFO stStartInfo;
::ZeroMemory(&stStartInfo,sizeof(stStartInfo));
stStartInfo.cb = sizeof(stStartInfo);
stStartInfo.dwFlags = STARTF_USESTDHANDLES;//STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
stStartInfo.hStdError = hWritePipe;
stStartInfo.hStdOutput = hWritePipe;
stStartInfo.hStdInput = hChildReadPipe;
if(!CreateProcessA(NULL, "cmd.exe/0", NULL, NULL, true, DETACHED_PROCESS, NULL, NULL, &stStartInfo, &stProcessInfo))
{
MessageBox("启动进程失败!");
return;
}else{
CloseHandle(stProcessInfo.hThread);
CloseHandle(stProcessInfo.hProcess);
}
//从管道中读出数据, 该数据通常为CMD的版本及版权信息
ReadFromChildPipe();
}
void ReadFromChildPipe()
{
//读取管道中所有可读取的数据并写入到txtMessage中
DWORD nRead;
char* strBuffer = new char[65536];
long nBufferLen;
nRead = -1;
while(nRead != 0)
{
nBufferLen = 65536;
memset(strBuffer, 0, 65536);
Sleep(30);
ReadFile(hReadPipe, strBuffer, nBufferLen, &nRead, NULL);
if(nRead != 0)
{
GetDlgItem(IDC_EDIT_MSG)->SetWindowTextA(strBuffer);
}
}
delete[] strBuffer;
}
void WriteMyPipe()
{
//将命令写入管道
DWORD nWrite;
char* strBuffer = new char[300];
memset(strBuffer, 0, 300);
memcpy(strBuffer, "dir/r/n", 5);
if (WriteFile(hChildWritePipe, strBuffer, 5, &nWrite, NULL))
{
ReadFromChildPipe();
}else{
MessageBox("写入失败");
}
delete[] strBuffer;
}
HANDLE hReadPipe;
HANDLE hWritePipe;
HANDLE hChildReadPipe;
HANDLE hChildWritePipe;
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
CloseHandle(hChildReadPipe);
CloseHandle(hChildWritePipe);
{
//创建管道
CreatePipe(&hReadPipe, &hWritePipe, NULL, NULL);
CreatePipe(&hChildReadPipe, &hChildWritePipe, NULL, NULL);
SetHandleInformation(hWritePipe, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
SetHandleInformation(hChildReadPipe, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
DWORD dwWord = PIPE_NOWAIT;
SetNamedPipeHandleState(hReadPipe, &dwWord, NULL, NULL);
//创建CMD进程
PROCESS_INFORMATION stProcessInfo;
STARTUPINFO stStartInfo;
::ZeroMemory(&stStartInfo,sizeof(stStartInfo));
stStartInfo.cb = sizeof(stStartInfo);
stStartInfo.dwFlags = STARTF_USESTDHANDLES;//STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
stStartInfo.hStdError = hWritePipe;
stStartInfo.hStdOutput = hWritePipe;
stStartInfo.hStdInput = hChildReadPipe;
if(!CreateProcessA(NULL, "cmd.exe/0", NULL, NULL, true, DETACHED_PROCESS, NULL, NULL, &stStartInfo, &stProcessInfo))
{
MessageBox("启动进程失败!");
return;
}else{
CloseHandle(stProcessInfo.hThread);
CloseHandle(stProcessInfo.hProcess);
}
//从管道中读出数据, 该数据通常为CMD的版本及版权信息
ReadFromChildPipe();
}
void ReadFromChildPipe()
{
//读取管道中所有可读取的数据并写入到txtMessage中
DWORD nRead;
char* strBuffer = new char[65536];
long nBufferLen;
nRead = -1;
while(nRead != 0)
{
nBufferLen = 65536;
memset(strBuffer, 0, 65536);
Sleep(30);
ReadFile(hReadPipe, strBuffer, nBufferLen, &nRead, NULL);
if(nRead != 0)
{
GetDlgItem(IDC_EDIT_MSG)->SetWindowTextA(strBuffer);
}
}
delete[] strBuffer;
}
void WriteMyPipe()
{
//将命令写入管道
DWORD nWrite;
char* strBuffer = new char[300];
memset(strBuffer, 0, 300);
memcpy(strBuffer, "dir/r/n", 5);
if (WriteFile(hChildWritePipe, strBuffer, 5, &nWrite, NULL))
{
ReadFromChildPipe();
}else{
MessageBox("写入失败");
}
delete[] strBuffer;
}
HANDLE hReadPipe;
HANDLE hWritePipe;
HANDLE hChildReadPipe;
HANDLE hChildWritePipe;
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
CloseHandle(hChildReadPipe);
CloseHandle(hChildWritePipe);
相关文章推荐
- 将CMD的输入输出重定向到自己的进程
- C++学习:通过管道进行cmd进程输入输出重定向
- Windows 批处理CMD脚本语言中的输入输出重定向详解
- python编写借助linux中的xinetd守护进程实现我们的网络程序(select、重定向标准输入输出)
- 查找+正则表达式+输入输出重定向+进程控制
- 进程通信之二 管道技术第一篇 输入输出的重定向
- 进程通信之二 管道技术第一篇 输入输出的重定向
- Windows下cmd标准输入输出重定向命令
- .net 下进程的输入输出重定向
- 重定向子进程控制台程序的输入输出
- 如何对CMD窗口进行输入输出重定向?
- Linux-打造自己的shell(支持输入输出重定向)
- 通过C#输入输出重定向与OutputDataReceived ,将cmd的运行程序,隐藏到后台执行
- VC使用管道重定向进程输入输出
- 如何对CMD窗口进行输入输出重定向
- 重定向子进程控制台程序的输入输出
- 将自己的域名重定向到某网站
- java调用CMD命令显示所有进程的信息
- 输入输出重定向
- linux windows关于端口和进程的cmd