How to send command to console application from GUI application

后端 未结 3 620
半阙折子戏
半阙折子戏 2020-12-05 21:13

I have a console application that I launch from a GUI applicaiton. The console application takes parameters for filenames to parse and process. Currently I am able to captur

3条回答
  •  北海茫月
    2020-12-05 21:26

    You need two pipes, one for the process to send output to you (stdout), and one for you to send input to the process (stdin).

    From your code, it looks like you are putting both ends of the same pipe into the TStartupInfo record. So you are effectively making the process talk to itself. :-)

    So, you need to call CreatePipe() twice, to create two pipes, one for stdin, one for stdout (and stderr).

    Then, put the reading handle of stdin in suiStartup.hStdInput and the writing handle of stdout in suiStartup.hStdOutput

    To send data to the process, write to the write handle of the stdin pipe. To read the output of the process, read the read handle of the stdout pipe.

    Edit: (again)

    As for all the duplicating handles and inheritable and non-inheritable stuff described on this page (specifically in the code example), you need to make sure the handles you send to the process are inheritable (as you have done).

    You should also make sure the handles of the pipes that the parent process use are not inheritable. But you don't have to do this... I've gotten away with not doing it before.

    You can do this by either calling DuplicateHandle() on the handles, specifying they are not inheritable and closing the old handles, or calling SetHandleInformation() with 0 specified for the flags (as discussed here).

    It's been a while since I have done this myself, but I'm pretty sure this is so that the reference count for the handles is associated with the calling process, rather than the child process. This prevents a handle being closed whilst you're still using it (the calling process might close 'stdin' for example). Make sure you close the handles though, otherwise you will end up leaking handles.

    HTH.

    N@

提交回复
热议问题