[python-win32] Re: [Py2exe-users] DllUnregister Question.
Blake Winton
bwinton at latte.ca
Thu Oct 7 21:08:18 CEST 2004
Thomas Heller <theller at python.net> wrote:
> Blake Winton <bwinton at latte.ca> writes:
>>I've written a COM server in Python, and am in the process of
>>packaging it up with an installer/uninstaller, but I'm running into
>>some strange behaviour when I try to uninstall my COM server.
>>Specifically, the uninstaller loads the dll, calls an exported
>>function by the name of DllRegisterServer, then unloads it. But after
>>it's done unloading it, the process (as shown by Process Explorer
>>v8.52, from www.sysinternals.com) still has a bunch of python-related
>>DLLs loaded.
>>
>>Does the py2exe-d version of a COM server load the dlls it needs when
>>it's being unregistered? And should it unload them when it's being
>>unloaded?
> I'm not really sure but I guess that Python in-process com servers never
> free all the python libraries, until the process terminates. Best would
> be to ask Mark Hammond, or post a question to the python win32 mailing
> list.
So that's what I'm doing now. I hope you don't mind being cc-ed in on
the discussion, Thomas. Let me know if you've subscribed to
python-win32, and I'll remove your name from future replies.
> For unregistering, you could try to do it by calling regsvr32.exe.
That's a work-around, but it's recommended by the InnoSetup people that
I don't do that, and instead use the built-in "regserver" flag, which
will cause the setup program to load the dll, call its DllRegister
method, and then unload it. (And the uninstall program to load the dll,
call its DllUnregister method, and then unload it.)
The reason I need to unload all the other dlls is that they're about to
be deleted as part of the uninstall. Currently it fails, complaining
that they're in use, which they are, by the uninstaller which has
inadvertantly loaded them by loading and unloading my COM server.
>>I've looked through the newsgroups (both -users and -checkins), and
>>the code in CVS, but nothing obvious jumped out at me (which isn't
>>completely surprising. If I was a COM master, I probably would have
>>written my conduit in C++, not Python). Specifically, I couldn't find
>>any calls to "FreeLibrary", which I would have expected to appear in
>>py2exe/source/run_dll.c, possibly in the DLL_PROCESS_DETACH portion of
>>DllMain. I suppose it could be mapped to the call to
>>PyCom_CoUninitialize, but that seems to be commented out.
> AFAIK, MS does not allow LoadLibrary or FreeLibrary calls in DllMain
> (but again, I'm only guessing right now).
I looked that up, and indeed, MS doesn't recommend you call LoadLibrary
or FreeLibrary in DllMain. So my next suggestion is to call it in
DllUnregisterServer. Specifically, DllUnregisterServer calls
check_init, which calls load_pythoncom, which calls LoadLibraryEx. I
suggest that we add a check_deinit() method, to be called just before we
return, which calls unload_pythoncom, which calls FreeLibrary.
Is there any reason not to do that? (Other than the arbitrary
"unload_pythoncom" call, which I added for a sense of duality, that is.)
Thanks,
Blake.
More information about the Python-win32
mailing list