-
March 17th, 2004, 02:08 PM
#1
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.
Humble thanks.
-
March 17th, 2004, 04:21 PM
#2
If someone told you to wait you would ask wait for what.
You are asking how to wait without specifying what to wait for.
-
March 17th, 2004, 04:35 PM
#3
Thank you for your response!
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.
Thanks, again.
-
March 17th, 2004, 04:44 PM
#4
Please allow me to rephrase:
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.
Thanks for your indulgence.
-
March 17th, 2004, 05:01 PM
#5
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?
-
March 17th, 2004, 05:20 PM
#6
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?
Thank you for your reponses and patience.
-
March 17th, 2004, 05:29 PM
#7
Okay...now I understood...
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...
-
March 17th, 2004, 06:01 PM
#8
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.
-
March 17th, 2004, 07:00 PM
#9
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.
And thank you, Sam, for your clarification.
-
March 17th, 2004, 07:14 PM
#10
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.
-
March 17th, 2004, 07:26 PM
#11
Sam,
I was under the impression that CreateProcess solves the
problem you mentioned:
"There is a problem in situations where a single application is used for multiple filees, such as Internet Explorer, Word and Excel."
ShellExecuteEx gave me a NULL process handle when the
associated file was Word and Word was already running.
CreateProcess _does_ give me a valid handle in this situation,
but I'm having trouble with WaitForSingleObject not waiting.
It is returning WAIT_OBJECT_0, but Word session launched by
CreateProcess is still open.
-
March 17th, 2004, 08:19 PM
#12
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.
-
March 18th, 2004, 12:01 PM
#13
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.
-
March 18th, 2004, 12:10 PM
#14
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.
-
May 25th, 2004, 09:59 PM
#15
I have the Same problem
Hi all,
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|