Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: CreateProcess/ShellExecuteEx and wait for process on Windows 10



Permlink Replies: 3 - Last Post: Aug 14, 2017 11:38 AM Last Post By: El Moataz Billa... Threads: [ Previous | Next ]
Timo Lappalainen

Posts: 2
Registered: 5/25/02
CreateProcess/ShellExecuteEx and wait for process on Windows 10
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 19, 2016 1:05 AM
Hi all,

I am using Delphi XE2. I had nice RunProg to run applications with several switches like /WaitForProcess, but it does not work on Windows 10. I found that method:
if CreateProcess(PChar(nil), PChar(CmdLine), nil, nil, false, 0, nil, pStartDir, si,pi) then begin
...
WaitResult:=WaitForSingleObject(pi.hProcess,WaitProcessLoopTime);
...
Starts application, but soon after start even application is still running, WaitResult=0 meaning that it has been signaled like process would have been closed. The same behaviour is also with ShellExecuteEx and actually old command line command
start "My app" /WAIT MyApp.exe
start MyApp on Windows 10, but returns immediately. This worked right on Windows 7 and Windows 8.

I tried to find solution without success.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: CreateProcess/ShellExecuteEx and wait for process on Windows 10
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 19, 2016 9:49 AM   in response to: Timo Lappalainen in response to: Timo Lappalainen
Timo wrote:

I am using Delphi XE2. I had nice RunProg to run applications with
several switches like /WaitForProcess, but it does not work on Windows
10.

Yes, it does.

I found that method:

if CreateProcess(PChar(nil), PChar(CmdLine), nil, nil, false, 0, nil, pStartDir,
si,pi) then begin

...

WaitResult:=WaitForSingleObject(pi.hProcess,WaitProcessLoopTime);

...

Starts application, but soon after start even application is still running,
WaitResult=0 meaning that it has been signaled like process would have
been closed.

That is correct. Which means the most likely scenario is that the process
you are launching is itself launching another process and then exiting itself,
thus signaling your process handle. If that is indeed happening, there is
nothing you can do about that.

But, what you could try doing is use CreateJobObject() and AssignProcessToJobObject()
to assign your launched process to a new job, and then wait for the job handle
to be signaled. Any child process that gets launched will automatically
be assigned to the same job by default, and the job will be signaled when
all of its associated processes have been closed.

Refer to MSDN for more details:

Job Objects
https://msdn.microsoft.com/en-us/library/windows/desktop/ms684161.aspx

--
Remy Lebeau (TeamB)
Timo Lappalainen

Posts: 2
Registered: 5/25/02
Re: CreateProcess/ShellExecuteEx and wait for process on Windows 10
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 19, 2016 11:36 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Yes, it does.
You are right, thanks. I was testing this with new version of the sw, which does as you said that it creates other process. The I started to debug it on my own computer with calc.exe, which does same on W10, but not on W7 or W8.

Thanks for pointing JobObjects. That should solve the problem also for new version of sw I need to test.
El Moataz Billa...

Posts: 4
Registered: 5/4/12
Re: CreateProcess/ShellExecuteEx and wait for process on Windows 10
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 14, 2017 11:38 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks,

it look useful, i will try it my self because i have the same ptob with Timo

Thnaks agian
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02