Home > Software engineering >  MFC how to input command to the external exe, and returns the result
MFC how to input command to the external exe, and returns the result

Time:09-22

Question is roughly like this, I have a MFC exe files, for example, A.e xe I need by now A.e xe to perform an outer B.e xe file,
As shown in figure B.e xe:


B.e xe is a third party of an application, similar to the CMD interface, you can enter the command line to execute instructions,
Now I want to by A.e xe to B.e xe input command line instruction, how to do this? Anonymous pipe is? Or what method, I ask you a great god!

CodePudding user response:

ShellExecute WinExec CreateProcess

CodePudding user response:

https://blog.csdn.net/u012442719/article/details/55261051

CodePudding user response:



refer to the second floor chengbar response:
https://blog.csdn.net/u012442719/article/details/55261051


Now I want to through the MFC to the child to enter commands, need to get the child as a result, to carry out the instructions how to implement this?

CodePudding user response:

Fyi:
 # pragma comment (lib, "user32") 
#include
#include
#include
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. The lpSecurityDescriptor=NULL;

//Create pipe for standard output redirection.
The CreatePipe (& amp; HPipeOutputRead,//read handle
& HPipeOutputWrite,//write handle
& Sa,//security attributes
0//number of bytes reserved for pipe - 0, the default
);

//Create pipe for standard input redirection.
The CreatePipe (& amp; HPipeInputRead,//read handle
& HPipeInputWrite,//write handle
& Sa,//security attributes
0//number of bytes reserved for pipe - 0, the default
);

//Make the 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, "CMD. Exe,"
NULL, NULL,
TRUE, 0,
NULL, NULL,
& Si, & amp; PI);

//Now that handles have had inherited, close it to be safe.
//You don 't want to read or write to them accidentally.
The CloseHandle (hPipeOutputWrite);
The CloseHandle (hPipeInputRead);

//Now test to capture the DOS application output by reading
//hPipeOutputRead. Could also write the to DOS application
//standard input by writing to hPipeInputWrite.
Sprintf (szMsg, "ver \ n");
WriteFile (
HPipeInputWrite,//handle of the write end of our pipe
& SzMsg,//address of buffer that send data
Strlen (szMsg),//the number of bytes to write
& DwNumberOfBytesWrite,//the address of the number of bytes read
NULL//non - overlapped.
);

While (TRUE)
{
BTest=ReadFile (
HPipeOutputRead,//handle of the read end of our pipe
& SzBuffer,//the address of the buffer that receives the data
256,//the number of bytes to read
& DwNumberOfBytesRead,//the address of the number of bytes read
NULL//non - overlapped.
);

if (! BTest) {
Sprintf (szMsg, "pipe. Error # % d reading", GetLastError ());
Printf (" % s ", szMsg);
break;
}

//do something with the data.
SzBuffer [dwNumberOfBytesRead]=0;//null terminate
Printf (" % s ", szBuffer);
If (' & gt; '==szBuffer] [dwNumberOfBytesRead - 1) break;
}

Sprintf (szMsg, "CHCP \ nexit \ n");
WriteFile (
HPipeInputWrite,//handle of the write end of our pipe
& SzMsg,//address of buffer that send data
Strlen (szMsg),//the number of bytes to write
& DwNumberOfBytesWrite,//the address of the number of bytes read
NULL//non - overlapped.
);

While (TRUE)
{
BTest=ReadFile (
HPipeOutputRead,//handle of the read end of our pipe
& SzBuffer,//the address of the buffer that receives the data
256,//the number of bytes to read
& DwNumberOfBytesRead,//the address of the number of bytes read
NULL//non - overlapped.
);

if (! BTest) {
Sprintf (szMsg, "pipe. Error # % d reading", GetLastError ());
Printf (" % s ", szMsg);
break;
}

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related