CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Mar 2004
    Posts
    28

    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.

  2. #2
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    If someone told you to wait you would ask wait for what.

    You are asking how to wait without specifying what to wait for.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  3. #3
    Join Date
    Mar 2004
    Posts
    28
    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.

  4. #4
    Join Date
    Mar 2004
    Posts
    28
    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.

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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?

  6. #6
    Join Date
    Mar 2004
    Posts
    28
    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.

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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...

  8. #8
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  9. #9
    Join Date
    Mar 2004
    Posts
    28
    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.

  10. #10
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  11. #11
    Join Date
    Mar 2004
    Posts
    28
    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.

  12. #12
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  13. #13
    Join Date
    Mar 2004
    Posts
    28
    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.

  14. #14
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  15. #15
    Join Date
    May 2004
    Posts
    7

    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
  •  





Click Here to Expand Forum to Full Width

Featured