I'm developing a shell extension that adds a context menu item to explorer. In DllRegisterServer and DllUnregisterServer I have the neccesary code to add or remove it from the registery respectively and also added a call to SHChangeNotify(SHCNE_ASSOCCHANGED, ...) at the end of these functions in order for Explorer to show or remove the shell extension immidiately. It all works fine, except of one thing: the dll remains loaded in the Explorer.exe process.
The problem with that is that in order to upgrade that dll to a newer version, Explorer must be restarted (otherwise the file is locked and you get an Access Denied error), which is a very ugly user experience! Note that when Explorer is restarted it closes all of its open windows...
How can I make my dll unload from Explorer without having to restart it?
Unloading the DLL
The Shell automatically unloads a DLL when its usage count is zero, but only after the DLL has not been used for a period of time. This inactive period might be unacceptably long at times, especially when a Shell extension DLL is being debugged. For operating systems prior to Windows 2000, you can shorten the inactive period by adding the following information to the registry.
In the right hand pane look for the "(Default)" String [REG_SZ] value. Make sure its value is 1.
If not present, create a new Registry subkey: highlight the "explorer" subkey (see above) -> right-click in the left hand pane -> select New -> Key -> type AlwaysUnloadDll -> hit Enter -> highlight the new "AlwaysUnloadDll" subkey -> double-click on the "(Default)" String Value in the right hand pane -> type 1 -> click OK or hit Enter
And as all some DLLs are stay-resident DLLs: they get loaded and stay loaded. Others get loaded when they're called and unloaded after. It's up to the DLL developer whether or not their DLL should be stay-resident.
Thanks Mike --------Please mark as answer if it is useful----------
Well guys, I have a question regarding this, does the AlwaysUnloadDll registry key still work for Windows XP, Vista and 7? because this article http://support.microsoft.com/kb/196480 says that: Important This registry key is no longer supported in Microsoft Windows 2000 or later. So, is this registry key really having an effect, or is it just staying there doing nothing?
I didn't find a solution for unloading the DLL, but we found a workaround for the upgrade problem:
If the file is in use, we copy the new file with a different name and load it (after we unregistered the old one). This makes the 2 dlls loadedm, but only the new one is in used until the next restart. In addition, we add a RunOnce command to delete the old dll and rename the new one to its original name.