[python-win32] os.startfile mysteriously failing

Thomas Heller theller at ctypes.org
Mon Apr 21 20:17:50 CEST 2008


Tim Golden schrieb:
> King Simon-NFHD78 wrote:
>> Hello all,
>> 
>> I am developing an application that, amongst other things, exports files
>> and then launches them in their associated applications using
>> os.startfile. It also uses webbrowser.open, which I see uses
>> os.startfile.
>> 
>> Everything works on my PC and on all the other PCs in the office that I
>> have tested on. However, when we released our first version, a few users
>> reported unhandled exceptions that seem to be coming from os.startfile.
>> Here are some examples:
> 
> [... snipped ...]
> 
> Unless someone comes in with something more authoritative, here
> are a few avenues to try:
> 
> 1) According to this:
> 
>   http://groups.google.co.uk/group/microsoft.public.platformsdk.shell/msg/943b9844d480b029
> 
> ShellExecute on a URL invokes a shell extension handler which will operate
> in apartment threading mode. (ie you're using COM whether you like it or
> not). Exactly how this affects your situation I'm sure I couldn't say, especially
> since you seem to have tried that route and you're not specifying  a COM
> threading model.

Some remarks that may or may not be useful:

AFAIK, a single threaded COM apartment needs to run a message loop,
according to the COM rules.  If the OP has a gui-application, and
calls os.startfile from the main thread, then this should be no problem.

If he has a console application, there normally is no message loop.

Looking into the MSDN docs for ShellExecuteEx [1], *this* function allows
to set additional flags in the SHELLEXECUTEINFO structure that you pass to it.
It seems that SEE_MASK_FLAG_DDEWAIT may be something worth to try out [2]:

"""
The SEE_MASK_FLAG_DDEWAIT flag must be specified if the thread calling ShellExecuteEx
does not have a message loop or if the thread or process will terminate soon after
ShellExecuteEx returns. Under such conditions, the calling thread will not be available
to complete the DDE conversation, so it is important that ShellExecuteEx complete the
conversation before returning control to the calling application. Failure to complete
the conversation can result in an unsuccessful launch of the document.
"""

[1] http://msdn2.microsoft.com/en-us/library/bb762154.aspx
[2] http://msdn2.microsoft.com/en-us/library/bb759784(VS.85).aspx

Thomas



More information about the python-win32 mailing list