News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

ShellExecute

Started by Mr Earl, March 16, 2005, 10:49:05 AM

Previous topic - Next topic

Mr Earl

If I start an application with ShellExecute, how do I determine that the application has terminated?

Tedd

GetExitCodeProcess gives STILL_ACTIVE until the process has terminated - where it gives the return code.
(But it requires a process handle - ShellExecute return an instance handle; not sure :bdg)
No snowflake in an avalanche feels responsible.

pbrennick

Mr Earl,

Tedd has the right idea here, this is the info he was not sure about:

Quote
Return Values

If the function succeeds, the return value is the instance handle of the application that was run, or the handle of a dynamic data exchange (DDE) server application.
If the function fails, the return value is an error value that is less than or equal to 32. The following table lists these error values:

Value   Meaning
0   The operating system is out of memory or resources.
ERROR_FILE_NOT_FOUND   The specified file was not found.
ERROR_PATH_NOT_FOUND   The specified path was not found.
ERROR_BAD_FORMAT   The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).
SE_ERR_ACCESSDENIED   The operating system denied access to the specified file.
SE_ERR_ASSOCINCOMPLETE   The filename association is incomplete or invalid.
SE_ERR_DDEBUSY   The DDE transaction could not be completed because other DDE transactions were being processed.
SE_ERR_DDEFAIL   The DDE transaction failed.
SE_ERR_DDETIMEOUT   The DDE transaction could not be completed because the request timed out.
SE_ERR_DLLNOTFOUND   The specified dynamic-link library was not found.
SE_ERR_FNF   The specified file was not found.
SE_ERR_NOASSOC   There is no application associated with the given filename extension.
SE_ERR_OOM   There was not enough memory to complete the operation.
SE_ERR_PNF   The specified path was not found.
SE_ERR_SHARE   A sharing violation occurred.

Paul

Tedd

I actually meant I wasn't sure if you can just use an instance handle as a process handle.
- ShellExecute returns an instance handle (hInstance)
- GetExitCodeProcess requires a process handle as its first parameter (hProcess)

The question is whether you can either: use hInstance as hProcess, or simply obtain the hProcess from the hInstance.
No snowflake in an avalanche feels responsible.

pbrennick

Tedd,
Sorry about the confusion.  Put your mind at ease, they are the same in principle and can be used in this manner.

Paul

pbrennick

Mr Earl,
I do not know the full extent of your intensions here.  Remember, if you detect that the process is still active and you intend to force it down that if you use TerminateProcess, for example, it will force down the process and all its threads in a very radical manner (which is why most use ExitProcess, instead).  The problem with this method is any DLLs loaded by the process will be left in memory causing memory pool problems.  Be warned.

Paul

Mr Earl

Thank you all for the advice and warnings.  I was hoping there was a notification that would tell me WHEN the process terminated without my having to do the checking.

00100b

Another option is to use CreateProcess and WaitForSingleObject (GetExitCodeProcess can still be used since the STILL_ACTIVE remains until the last thread of the process terminates).  When the process terminates, the handle pointing to the process object returned by CreateProcess will be set to the signalled state.

pbrennick

Mr Earl,

Quote
Thank you all for the advice and warnings.  I was hoping there was a notification that would tell me WHEN the process terminated without my having to do the checking.

The point here is that in assembly, both the plus and the negative, is that you must do a lot of the work yourself.  If you do not want to do that, then this language is not the right choice for you.

Four-b is correct here, I do not use any other method but the one he describes as it is the most reliable.  Shell execute has problems as I tried to tell you.

Paul

Mr Earl

Paul,
You misunderstood my reason for wanting a notification. I've been coding in assembler for a looong time because I enjoy the detail and doing most of the work myself so that I understand EXACTLY what is going on.  The API is another story.  I have no experience there.  Exactly WHEN the process ends is what I want to know.  That means I have to sit in a tight loop checking STILL_ACTIVE, consuming a lot of processor time.
00100b's suggestion using CreateProcess and WaitForSingleObject may be the way to go.  According to the API, WaitForSingleObject doesn't use a lot of processor time.
Thank you all for your help.
 

pbrennick

Mr Earl,
If you have been programming for a long time and don't understand the API, I guess that means you just recently made the switch to 32bit from 16bit.  Welcome aboard, sorry for my error.  The API learning curve is a bit steep in the beginning but you will quickly come up to speed and once you grow accustomed to it, you will not look back.  :U

CreateProcess is the best solution.

Paul

sluggy

Quote from: Mr Earl on March 16, 2005, 04:56:48 PM
00100b's suggestion using CreateProcess and WaitForSingleObject may be the way to go.  According to the API, WaitForSingleObject doesn't use a lot of processor time.
With WaitForSingleObject you still have to sit in a loop (unless you want to pause), so if you still want to do work while waiting then it might pay to create a new thread, and start the process from that, and do the Wait in the thread as well.

Mr Earl

Thanks Sluggy, I'll give that a try

P1

I use a custom loop with a Sleep in it to control uP burden and frequency of checking.

Regards,  P1  :8)