功能很简单,创建管道执行dir c:,然后messagebox出来。 代码能编译了,但命令写入管道时不成功,不知为什么,高手指点一下啊!!
void CCmdpipeDlg::OnCmd()
{
int ret;
SECURITY_ATTRIBUTES sa1,sa2;
HANDLE hReadPipe2,hWritePipe2,hReadPipe1,hWritePipe1; CreatePipe( &hReadPipe1, &hWritePipe1, &sa1, 0 );
CreatePipe( &hReadPipe2, &hWritePipe2, &sa2, 0 ); STARTUPINFO si;
::ZeroMemory(&si,sizeof(si));
si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.hStdInput=hReadPipe2;
si.hStdOutput=si.hStdError=hWritePipe1;
PROCESS_INFORMATION processInfo;
char cmdLine[] = "cmd.exe";
ZeroMemory( &processInfo , sizeof(PROCESS_INFORMATION) );
ret=CreateProcess(NULL, cmdLine, NULL,NULL,1,0,NULL,NULL,&si,&processInfo);
if( !ret ) return;
unsigned long bytesRead=0;
char *buff="dir c:";
bytesRead=strlen(buff);
ret = WriteFile( hWritePipe2, buff, bytesRead, &bytesRead, 0 ); //把命令写入管道
if( !ret ) return; ret = ReadFile( hReadPipe1, buff, bytesRead, &bytesRead, 0 ); //把执行的结果读出来
if( !ret ) return; AfxMessageBox(buff);
TerminateProcess( processInfo.hProcess, 0 );
CloseHandle( hReadPipe1 );
CloseHandle( hReadPipe2 );
CloseHandle( hWritePipe1 );
CloseHandle( hWritePipe2 ); }
void CCmdpipeDlg::OnCmd()
{
int ret;
SECURITY_ATTRIBUTES sa1,sa2;
HANDLE hReadPipe2,hWritePipe2,hReadPipe1,hWritePipe1; CreatePipe( &hReadPipe1, &hWritePipe1, &sa1, 0 );
CreatePipe( &hReadPipe2, &hWritePipe2, &sa2, 0 ); STARTUPINFO si;
::ZeroMemory(&si,sizeof(si));
si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.hStdInput=hReadPipe2;
si.hStdOutput=si.hStdError=hWritePipe1;
PROCESS_INFORMATION processInfo;
char cmdLine[] = "cmd.exe";
ZeroMemory( &processInfo , sizeof(PROCESS_INFORMATION) );
ret=CreateProcess(NULL, cmdLine, NULL,NULL,1,0,NULL,NULL,&si,&processInfo);
if( !ret ) return;
unsigned long bytesRead=0;
char *buff="dir c:";
bytesRead=strlen(buff);
ret = WriteFile( hWritePipe2, buff, bytesRead, &bytesRead, 0 ); //把命令写入管道
if( !ret ) return; ret = ReadFile( hReadPipe1, buff, bytesRead, &bytesRead, 0 ); //把执行的结果读出来
if( !ret ) return; AfxMessageBox(buff);
TerminateProcess( processInfo.hProcess, 0 );
CloseHandle( hReadPipe1 );
CloseHandle( hReadPipe2 );
CloseHandle( hWritePipe1 );
CloseHandle( hWritePipe2 ); }
SECURITY_ATTRIBUTES sa1,sa2;
sa1.nLength = sizeof(SECURITY_ATTRIBUTES);
sa1.lpSecurityDescriptor = NULL;
sa1.bInheritHandle = TRUE;
sa2.nLength = sizeof(SECURITY_ATTRIBUTES);
sa2.lpSecurityDescriptor = NULL;
sa2.bInheritHandle = TRUE;
HANDLE hReadPipe2,hWritePipe2,hReadPipe1,hWritePipe1; CreatePipe( &hReadPipe1, &hWritePipe1, &sa1, 0 );
CreatePipe( &hReadPipe2, &hWritePipe2, &sa2, 0 ); STARTUPINFO si;
::ZeroMemory(&si,sizeof(si));
si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.hStdInput=hReadPipe2;
si.hStdOutput=si.hStdError=hWritePipe1;
si.cb=sizeof(si);
PROCESS_INFORMATION processInfo;
char cmdLine[] = "cmd.exe /k dir c:";
ZeroMemory( &processInfo , sizeof(PROCESS_INFORMATION) );
ret=CreateProcess(NULL, cmdLine, NULL,NULL,1,0,NULL,NULL,&si,&processInfo);
if( !ret ) return;
char buff[1024]={'\0'};
strcpy(buff,"dir c:");
DWORD bytesRead=1024; Sleep(1000);
ret = WriteFile( hWritePipe2, buff, bytesRead, &bytesRead, 0 );
if( !ret ) return;
ret = ReadFile( hReadPipe1, buff, 1024, &bytesRead, 0 );
if( !ret ) return; AfxMessageBox(buff);
TerminateProcess( processInfo.hProcess, 0 );
CloseHandle( hReadPipe1 );
CloseHandle( hReadPipe2 );
CloseHandle( hWritePipe1 );
CloseHandle( hWritePipe2 );