views:

438

answers:

1

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.

+4  A: 

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.

mghie
Great, I'm going to try that out.If it helps anybody I actually found a similar article.http://stackoverflow.com/questions/340356/making-createprocess-inherit-the-console-of-the-calling-process
Phil