1
votes

In C I've used the system() function before in a console application and if I start another process using system() it inherits the console window of the process that called it.

In Delphi system() doesn't exist so I'm using ShellExecute() to create a new process, but the new process comes up in a new console window. Is there some way that I can make it inherit the handle of the window that's calling it?

I've used

function GetConsoleWindow(): HWND; stdcall; external 'kernel32.dll';

to get the console window and passed it in the HWND part of ShellExecute(), but that didn't work.

1
I for to say I've used: function GetConsoleWindow():HWND; stdcall; external 'kernel32.dll'; This function to get the console window in an attempt to pass that in the HWID part of ShellExecute, but it didn't work.Phil
I've added that to your question, hopefully in the way you meant it.mghie
Phil, when you say "it didn't work," what do you mean by that? What did it do instead?Mason Wheeler
It opens another console window.Phil

1 Answers

5
votes

Using ShellExecute() you won't be able to make the spawned application use the same console. The HWND element in the ShellExecute() call is documented:

Specifies a parent window. This window receives any message boxes that an application produces. For example, an application may report an error by producing a message box.

so it won't have any effect for console applications.

If you use CreateProcess() instead you have much more control over the spawned process. By using the dwCreationFlags parameter you can force the new process to get its own console (use the CREATE_NEW_CONSOLE flag), but per default it will inherit the console of the parent process as well.