Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using CreateProcess() to start another app in background

1,424 views
Skip to first unread message

Peter Rees

unread,
Aug 31, 2004, 5:11:24 PM8/31/04
to
Hi all,

I ashed this question on the cpp.builder.nativeapi forum but go no reply so
am trying here.

I am using the following code to start another process ( exe file ). I want
the application that I am starting ( child ) to go into the background and
this application ( parent ) to remain unchanged ( it may itself also be in
the background )

Can someone advise what I need to do to accomplish this. I have searched
Google and the Win SDK but have not found the answer. I still want the new
app to appear on the task bar. I just do not want it to become the
foreground task.

TIA

Peter


// --- snip ----

memset(&sStartUpInfo,0, sizeof(sStartUpInfo)) ;
sStartUpInfo.cb = sizeof(sStartUpInfo) ;
sStartUpInfo.dwFlags = STARTF_USESHOWWINDOW ;
sStartUpInfo.wShowWindow = SW_SHOWDEFAULT ;
nExitCode =CreateProcess(NULL, cParam, NULL, NULL, FALSE,
CREATE_NEW_CONSOLE |
NORMAL_PRIORITY_CLASS,NULL,NULL, &sStartUpInfo, &sProcessInfo) ;
.......


Damien Honeyford

unread,
Aug 31, 2004, 5:46:17 PM8/31/04
to
Hi,

"Peter Rees" <pe...@NOOSPAMMrees.co.nz> wrote in message
news:4134e891$1...@newsgroups.borland.com...

> I am using the following code to start another process ( exe file ). I
want
> the application that I am starting ( child ) to go into the background and
> this application ( parent ) to remain unchanged ( it may itself also be in
> the background )

> // --- snip ----


>
> memset(&sStartUpInfo,0, sizeof(sStartUpInfo)) ;
> sStartUpInfo.cb = sizeof(sStartUpInfo) ;
> sStartUpInfo.dwFlags = STARTF_USESHOWWINDOW ;
> sStartUpInfo.wShowWindow = SW_SHOWDEFAULT ;
> nExitCode =CreateProcess(NULL, cParam, NULL, NULL, FALSE,
> CREATE_NEW_CONSOLE |
> NORMAL_PRIORITY_CLASS,NULL,NULL, &sStartUpInfo, &sProcessInfo) ;
> .......

You are nearly there -- instead of SW_SHOWDEFAULT though, try SW_SHOWNA or
SW_SHOWNOACTIVATE

Damien

Peter Rees

unread,
Aug 31, 2004, 6:45:00 PM8/31/04
to

Damien,

Thanks. I just tried that but the child still comes to the front. Drat :-)


Regards


Peter


David

unread,
Sep 1, 2004, 3:31:32 AM9/1/04
to
Not the best solution, but if all else fails and P.B. is on vacation.
I used notepad as an example

var
H: hWnd;

function GetWin(Handle: HWND; LParam: Longint): Bool; stdcall;
begin
if Getparent(Handle) = 0 then
begin
H := Handle;
SetWindowPos(H, HWND_BOTTOM, 0,0,0,0, SWP_NOMOVE or SWP_NOSIZE or SWP_NOACTIVATE or SWP_SHOWWINDOW);
end;
end;


var
Startupinfo: TStartupinfo;
Processinfo: TProcessInformation;
begin
FillChar(StartupInfo, SizeOf(StartupInfo), 0);
with StartupInfo do
begin
cb := SizeOf(TStartupInfo);
dwFlags := STARTF_USESHOWWINDOW;
wShowWindow := SW_HIDE;
end;

CreateProcess('c:\windows\notepad.exe', nil, nil, nil,
False, NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);

while (H = 0) {and limit to 10 secs} do
begin
Application.ProcessMessages;
EnumThreadWindows(Processinfo.dwThreadid, @Getwin, 0);
end;
end;

"Peter Rees" <pe...@NOOSPAMMrees.co.nz> schreef in bericht news:4134e891$1...@newsgroups.borland.com...

Damien Honeyford

unread,
Sep 1, 2004, 3:23:59 PM9/1/04
to
Hi,

"Peter Rees" <pe...@NOOSPAMMrees.co.nz> wrote in message

news:4134fe83$1...@newsgroups.borland.com...

> Thanks. I just tried that but the child still comes to the front. Drat :-)

Fudge; well reading the docs a little closer it would appear that the value
specified will only have an affect if the child uses ShowWindow(hWnd,
SW_DEFAULT) -- if it specifies SW_NORMAL or any other flag when displaying
it's window then the value from CreateProcess is ignored.

David seems to have hit upon a possible solution though; sorry I couldn't be
of more help.


Damien

Peter Rees

unread,
Sep 1, 2004, 6:00:12 PM9/1/04
to

David


Thanks for your help. It works. I modified you idea slightly to use

WaitForInputIdle( sProcessInfo.hProcess, 15000 ) ; // Wait up to 15
seconds for process to start

so no need for while() loop

Also used SetForegroundWindow( hWndThis ) to restore foreground window.

Regards

Peter


0 new messages