wxProcess::Exists(PID)

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
asadilan
Earned some good credits
Earned some good credits
Posts: 147
Joined: Tue Jul 27, 2010 10:42 pm

wxProcess::Exists(PID)

Post by asadilan » Mon Feb 10, 2014 7:59 pm

Hi All,

I need help with wxprocess::exists();

Code: Select all

wxProcess process ;
bool test = false;
pid = wxExecute(command, wxEXEC_ASYNC , &process);

if(wxProcess::Exists(pid)// process.Exists(pid)
{
 test = true;
}
else
{
test =false;
}


why is that test is always true?

Thanks
Asadillan

User avatar
doublemax
Moderator
Moderator
Posts: 14770
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxProcess::Exists(PID)

Post by doublemax » Mon Feb 10, 2014 9:15 pm

First you create the process and then you check for its pid. Isn't it normal that it's always true? It could only be false if the process is already terminated by the time the next code line is reached. Very unlikely.
Use the source, Luke!

asadilan
Earned some good credits
Earned some good credits
Posts: 147
Joined: Tue Jul 27, 2010 10:42 pm

Re: wxProcess::Exists(PID)

Post by asadilan » Mon Feb 10, 2014 9:18 pm

Hi DoubleMax,

Thanks for replying.
yes that is true.
I forgot to mention that i put a breakpoint before checking and manually kill the process through process explorer.
I waited a while and it still tells me that the process exists.

Thanks for pointing that out :)

Asadilan

User avatar
doublemax
Moderator
Moderator
Posts: 14770
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxProcess::Exists(PID)

Post by doublemax » Mon Feb 10, 2014 9:56 pm

Which platform?

Under Windows wxProcess::Exists(pid) is a relatively small wrapper around the Win32 API functions and i don't really see where it could fail. Try tracing through that method.
Use the source, Luke!

asadilan
Earned some good credits
Earned some good credits
Posts: 147
Joined: Tue Jul 27, 2010 10:42 pm

Re: wxProcess::Exists(PID)

Post by asadilan » Mon Feb 10, 2014 10:08 pm

Thanks
i think there is a bug there.
it should be openprocess and getexitcodeprocess instead of just openprocess.

tigerbeard
Experienced Solver
Experienced Solver
Posts: 62
Joined: Sat Oct 07, 2006 1:56 pm

Re: wxProcess::Exists(PID)

Post by tigerbeard » Fri Apr 01, 2016 6:28 pm

asadilan wrote:Hi All,
I need help with wxprocess::exists();
why is that test is always true?
I found this post in search for a quite interesting other issue I have with wxExecute. The same test unfortunately is *not* always true.
Here is the code adapted from the example of asadilan

Code: Select all

bool test = false;
int	pid = 0;		// ist a bit inconsisten wxExecute returns long, Exists() takes int ...

pid = wxExecute("MyTestTool.exe");
if(wxProcess::Exists(pid)// process.Exists(pid)
	{test = true;}			// works fine
else
	{test =false;}

pid = wxExecute("explorer.exe");
if(wxProcess::Exists(pid)// process.Exists(pid)
	{test = true;}
else
	{test =false;}			// does not work. Strange!
The result for Explorer.exe is wrong compared to other applications. MyTestTool.exe is just an example. Both commands open the applications. But in TaskManager only the MyTestTool.exe has the same PID than returned by exExecute. When the explorer window openes, there is a new "explorer.exe" entry in the task manage, but it has a different PID. Obviously wxExists() would return false.

In http://stackoverflow.com/questions/1208 ... powershell I found the explanation that windows starts a new the explorer process with the initial number, but modifies it almost instantly to merge/align/whatever it with the existing processes.
If that is correct, the process I see in task manager has a different ID from the one returned by wxExecute(). Currently I have no idea how to find out this new PID using wxWidget framework commands only (want to avoid native windows libs).

Can someone confirm or better explain that behaviour? What is the smartest way how to find out the new Explorer PID?


I am running V2.9.3 on Windows7.
System config: Xubuntu16.04/Win7/XP/2K, wxWidgets 2.9.3, C::B, GCC, VC

tigerbeard
Experienced Solver
Experienced Solver
Posts: 62
Joined: Sat Oct 07, 2006 1:56 pm

Re: wxProcess::Exists(PID)

Post by tigerbeard » Sat Apr 02, 2016 9:43 pm

found it out.

wxExecute spaws the process normally when starting explorer.exe. The parent PID is the onw application process.
At least this is the case if in Explorer options 'start in a separate process' is selected.
After less than half a second later svchost creates a new explorer process using a not documented /factory option, this time its parent is the explorer process that is running the desktop, taskbar etc. It takes over the data from the app created process, which then is closed.
Unfortunately its hard to identify the different explorer tasks by queries. Depending on configuration and OS Version they might have a path in the title bar, they might be separate processes or not etc. So the only rather
safe way seems to be getting a process list filteres on explorer.exe before starting the new explorer window, then wait a second, redo that and check on a new PID. That new PID is very likely the successor of the newly created explorer window.

So wxExecute works fine, just the windows explorer is a pretty weird kind of a process.

Maybe it helps someone else to safe a few hours exploring :)
System config: Xubuntu16.04/Win7/XP/2K, wxWidgets 2.9.3, C::B, GCC, VC

Post Reply