cmd.exe可以透过匿名管道实现输入输出重定向到MFC组件,python.exe为什么不行呢
cmd.exe可以通过匿名管道实现输入输出重定向到MFC组件,python.exe为什么不行呢
如题,我想通过管道将python.exe执行器的输入输出重定向MFC组件中,为什么不行呢。。但是换成cmd.exe就明显可以呀,不知是何原因,同时在cmd.exe成功重定向后,执行python时,也无法进入python的交互模式,输入import 时只会退出python回到cmd中,那位大神遇到这问题,求助啊。。
代码如下:
#include <stdio.h>
#include "stdafx.h"
DWORD WINAPI threadWork(LPVOID threadNo) ;
int main()
{
HANDLE hReadPipe,hWritePipe;
HANDLE hInput,hOutput;
STARTUPINFO startInfo;
PROCESS_INFORMATION procInfo;
SECURITY_ATTRIBUTES pipeSA=
{
sizeof(SECURITY_ATTRIBUTES),NULL,TRUE
};
if(!CreatePipe(&hReadPipe,&hInput,&pipeSA,0)) // read ,write
{
printf("error \n");
}
if(!CreatePipe(&hOutput,&hWritePipe,&pipeSA,0))
{
printf("error \n");
}
ZeroMemory(&startInfo,sizeof(startInfo));
GetStartupInfo(&startInfo);
startInfo.cb = sizeof(startInfo);
startInfo.wShowWindow = SW_HIDE;
startInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
startInfo.hStdInput = hOutput;
startInfo.hStdOutput = hInput;
startInfo.hStdError = hInput;
if(CreateProcess(NULL,"cmd.exe",NULL,NULL,TRUE,CREATE_NO_WINDOW,NULL,NULL,&startInfo,&procInfo))
{
CloseHandle(hInput);
CloseHandle(hOutput);
CloseHandle(procInfo.hThread);
CreateThread(NULL,0,threadWork,hReadPipe,0,NULL);
}
while(1)
{
DWORD w;
int len = 0;
char buf[64];
// scanf("%s",buf);
gets(buf);
len = strlen(buf);
buf[len++]='\r';
buf[len++]='\n';
WriteFile(hWritePipe,buf,len,&w,0);
Sleep(100);
}
int a=GetLastError();
return 1;
}
DWORD WINAPI threadWork(LPVOID threadNo)
{
char buf[64];
DWORD count;
HANDLE hReadPipe = threadNo;
while(1)
{
ReadFile(hReadPipe,buf,63,&count,0);
if(count > 0)
{
buf[count]='\0';
printf("%s",buf);
Sleep(50);
}
else
Sleep(100);
}
return 1;
}
这是运行结果:
输入python时,就卡住了。。。但是别的命令,ls就可以的

------解决思路----------------------
仅供参考:
如题,我想通过管道将python.exe执行器的输入输出重定向MFC组件中,为什么不行呢。。但是换成cmd.exe就明显可以呀,不知是何原因,同时在cmd.exe成功重定向后,执行python时,也无法进入python的交互模式,输入import 时只会退出python回到cmd中,那位大神遇到这问题,求助啊。。
代码如下:
#include <stdio.h>
#include "stdafx.h"
DWORD WINAPI threadWork(LPVOID threadNo) ;
int main()
{
HANDLE hReadPipe,hWritePipe;
HANDLE hInput,hOutput;
STARTUPINFO startInfo;
PROCESS_INFORMATION procInfo;
SECURITY_ATTRIBUTES pipeSA=
{
sizeof(SECURITY_ATTRIBUTES),NULL,TRUE
};
if(!CreatePipe(&hReadPipe,&hInput,&pipeSA,0)) // read ,write
{
printf("error \n");
}
if(!CreatePipe(&hOutput,&hWritePipe,&pipeSA,0))
{
printf("error \n");
}
ZeroMemory(&startInfo,sizeof(startInfo));
GetStartupInfo(&startInfo);
startInfo.cb = sizeof(startInfo);
startInfo.wShowWindow = SW_HIDE;
startInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
startInfo.hStdInput = hOutput;
startInfo.hStdOutput = hInput;
startInfo.hStdError = hInput;
if(CreateProcess(NULL,"cmd.exe",NULL,NULL,TRUE,CREATE_NO_WINDOW,NULL,NULL,&startInfo,&procInfo))
{
CloseHandle(hInput);
CloseHandle(hOutput);
CloseHandle(procInfo.hThread);
CreateThread(NULL,0,threadWork,hReadPipe,0,NULL);
}
while(1)
{
DWORD w;
int len = 0;
char buf[64];
// scanf("%s",buf);
gets(buf);
len = strlen(buf);
buf[len++]='\r';
buf[len++]='\n';
WriteFile(hWritePipe,buf,len,&w,0);
Sleep(100);
}
int a=GetLastError();
return 1;
}
DWORD WINAPI threadWork(LPVOID threadNo)
{
char buf[64];
DWORD count;
HANDLE hReadPipe = threadNo;
while(1)
{
ReadFile(hReadPipe,buf,63,&count,0);
if(count > 0)
{
buf[count]='\0';
printf("%s",buf);
Sleep(50);
}
else
Sleep(100);
}
return 1;
}
这是运行结果:
输入python时,就卡住了。。。但是别的命令,ls就可以的
------解决思路----------------------
仅供参考:
#pragma comment(lib,"user32")
#include <stdio.h>
#include <windows.h>
int main() {
SECURITY_ATTRIBUTES sa = {0};
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};
HANDLE hPipeOutputRead = NULL;
HANDLE hPipeOutputWrite = NULL;
HANDLE hPipeInputRead = NULL;
HANDLE hPipeInputWrite = NULL;
BOOL bTest = 0;
DWORD dwNumberOfBytesRead = 0;
DWORD dwNumberOfBytesWrite = 0;
CHAR szMsg[100];
CHAR szBuffer[256];
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
// Create pipe for standard output redirection.
CreatePipe(&hPipeOutputRead, // read handle
&hPipeOutputWrite, // write handle
&sa, // security attributes
0 // number of bytes reserved for pipe - 0 default
);
// Create pipe for standard input redirection.
CreatePipe(&hPipeInputRead, // read handle
&hPipeInputWrite, // write handle
&sa, // security attributes
0 // number of bytes reserved for pipe - 0 default
);
// Make child process use hPipeOutputWrite as standard out,
// and make sure it does not show on screen.
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW
------解决思路----------------------
STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdInput = hPipeInputRead;
si.hStdOutput = hPipeOutputWrite;
si.hStdError = hPipeOutputWrite;
CreateProcess (
NULL, "c:\\python24\\python.exe",
NULL, NULL,
TRUE, 0,
NULL, NULL,
&si, &pi);
// Now that handles have been inherited, close it to be safe.
// You don't want to read or write to them accidentally.
CloseHandle(hPipeOutputWrite);
CloseHandle(hPipeInputRead);
// Now test to capture DOS application output by reading
// hPipeOutputRead. Could also write to DOS application
// standard input by writing to hPipeInputWrite.
sprintf(szMsg, "print 1+2\n\x1a\n");
WriteFile(
hPipeInputWrite, // handle of the write end of our pipe
&szMsg, // address of buffer that send data
18, // number of bytes to write
&dwNumberOfBytesWrite,// address of number of bytes read
NULL // non-overlapped.
);
while(TRUE)
{
bTest=ReadFile(
hPipeOutputRead, // handle of the read end of our pipe
&szBuffer, // address of buffer that receives data
256, // number of bytes to read
&dwNumberOfBytesRead, // address of number of bytes read
NULL // non-overlapped.
);
if (!bTest){
sprintf(szMsg, "Error #%d reading pipe.",GetLastError());
MessageBox(NULL, szMsg, "WinPipe", MB_OK);
break;
}
// do something with data.
szBuffer[dwNumberOfBytesRead] = 0; // null terminate
MessageBox(NULL, szBuffer, "WinPipe", MB_OK);
}
// Wait for CONSPAWN to finish.
WaitForSingleObject (pi.hProcess, INFINITE);
// Close all remaining handles
CloseHandle (pi.hProcess);
CloseHandle (hPipeOutputRead);
CloseHandle (hPipeInputWrite);
return 0;
}