Read FAQ but how to open assoc'd file and wait till done?
The FAQ "How can I start a process?" suggests using ShellExecute/Ex to open a document with its associated application...
And FAQ "How do I wait until a process ends?" suggests using WaitForSingleObject(hProcess, INF) from CreateProcess' piProcessInfo...
But, how can I wait for an arbitrary application to end? Is there a way to use CreateProcess w/o specifying an application?
Using ShellExecuteEx to get the process handle is, as has been said, risky because if, say, IE is already running, you won't get a valid handle for the existing process.
If I don't know what the document type is, (.txt, .doc, .htm, etc)
how else can I open it besides letting ShellExecute/Ex do the lookup for the association?
If I do that, I'm not sure how to wait for whatever it was that did the opening.
Assuming I can't know what the document type might be, (.txt, .doc, .htm, etc)
how else can I cause it to be opened besides letting ShellExecute/Ex do the lookup for the association?
If I do that, I'm not sure how to wait for whatever it was that did the opening.
Well...I assume you meant that you do know the extension and want to find the corresponding application? Then, in this case 'ShellExecute()'/'ShellExecuteEx()' would be the simplest solution to open that file.
If you don't know the extension, then there is actually no assocation available...due to the missing extension.
So, I am sorry, but I still might not understand your actual problem...what are you trying to accomplish?
Ciao, Andreas
"Software is like sex, it's better when it's free." - Linus Torvalds
I apologize for not being clear. Posting with clarity is harder than it seems.
I have a filename provided by a user.
All I know for sure is that the file exists.
I want to open it with whatever application is associated with
this file and wait until that application is closed before proceeding.
If ShellExecute/Ex (or whatever I end up using) cannot find an
association, I tell the user that I can't open the file and carry on.
If I am able to open the file, I want to wait until that application
(whatever it is) is closed before continuing with my processing.
So, I've learned how ShellExecute/Ex can execute the associated
application for me, but I do not understand how to wait for
whatever application that was actually executed to finish
because I don't know how to get a (guaranteed) viable process
handle from ShellExecute/Ex.
And I don't know how to make use of CreateProcess in this
situation to get that viable handle.
Is there another API I can use, to the registry or somewhere, to
tell me, up front, what the file association is so I can use
CreateProcess?
Well...you can use 'CreateProcess()' instead, however, you would need to do find the associated application for the specific extension on your own, since 'CreateProcess()' will not do it on its own.
Nevertheless, getting the correct application is not that hard...there exists a function named 'FindExecutable()' which will actually do the lookup...
Ciao, Andreas
"Software is like sex, it's better when it's free." - Linus Torvalds
You say "ShellExecute/Ex" but there are critical differences between ShellExecute and ShellExecuteEx in this context. ShellExecuteEx will accomplish what you need as much as is reasonably possible. In the SHELLEXECUTEINFO structure that ShellExecuteEx uses is hProcess, which can be used to wait for the process. There is a problem in situations where a single application is used for multiple filees, such as Internet Explorer, Word and Excel. I think there is a KB article describing the problem but I forget if there is a solution provided.
Thank you Andreas. That, of course, is what I needed.
I guess I was led slightly astray by a previous thread about file
associations that mentioned ShellExecute, ShellExecuteEx,
CreateProcess and ("why didn't the poster first check") the FAQ.
I don't know what the advantage is of using CreateProcess and FindExecutable. I don't think they can do anything relevant that ShellExecuteEx can't do and I think ShellExecuteEx would be easier.
Well I did say that there is a KB article. I think it does say that the returned handle will be NULL if there is process already running. So CreateProcess might be a partial solution.
I think the problem with Word can be solved using WaitForInputIdle.
However the CreateProcess solution might be a problem when a file needs to be opened using more than passing the file on a command line, such as when DDE is used.
Thank you, Sam, for your continued interest in my issue.
W.r.t. WaitForInputIdle, do you mean to perform this before
WaitForSingleObject? If so, it didn't seem to have any effect.
Both calls returned 0 (success).
I have tried to familiarize myself with the basic concepts of DDE,
but I'm afraid I don't understand what you mean about
CreateProcess possibly being problematic when DDE is used.
Originally posted by joleary
I have tried to familiarize myself with the basic concepts of DDE,
but I'm afraid I don't understand what you mean about
CreateProcess possibly being problematic when DDE is used.
Yes it probaly is difficult to find information about that unless you look at the registrations (associations?) for various file types. You will see that many file types use DDE to open them.
I am also having problems with createprocess and WaitForSingleObject.
The process launches documents (xls,doc,mpp,ppt) and then uses WaitForSingleObject, users can change the document and then save those changes.
The program works fine, unless documents of the same type are open (i.e. if am launching a word document and I have other word docs open WaitForSingleProcess returns 0 even though the document is still open)
I have used WaitForInputIdle but the process is initiated fine.
Do you have multiple instances of the app running as well?
Bookmarks