Archive: Plugin - Load an unload of DLL


Plugin - Load an unload of DLL
I currently help Aleksander (aka Instructor) to port his Registry Library (Plugin) to support PocketPC. The goal is, that you can manipulate the Registry of a Windows Mobile based device inside a NSIS script BEFORE you submit a CAB file. To do that we need to add support for RAPI to Aleksanders Plugin.

After a lot of debugging I found, why Aleksanders code did not work. It has something to do with the way NSIS loads and unloads plugins (DLL's).

Could someone confirm that:

- if the parser finds something like <MyPlugin>::<MyFunction> in the script it does:
HMODULE hModule = LoadLibrary <MyPlugin>.dll
call <MyFunction>
FreeLibrary(hModule) which causes an unload of <MyPlugin>.dll if the reference counter is 0.

For RAPI calls this has some bad consequences! To be able to call an API on a PocketPC remotely, you need to initialise communications between the PC and the mobile device. This is done through ActiveSync using an API called CeRapiInit(). This call will load another DLL called Rapi.dll. It stores there a communication handle.

Fist Aleksander exported functions CeRapiInit() and CeRapiUninit(). This would have allowed the developers to write scripts like:

PPCregistry::CeRapiInit()
PPCregistry::Read "HKEY_LOCAL_MACHINE\SomeKey\Key\...." $RegVar
PPCregistry::Write "HKEY_LOCAL_MACHINE\SomeKey\...." "ValueName" "Value" REG_SZ
PPCregistry::CeRapiUninit()

Since all DLLs are unloaded after every call to PPCregistry the Rapi.dll is also unloaded and therefore the handle to the mobile device is lost or freed. So the concept shown above will not work. Communication with the PocketPC has to be established in the implementation of every exported function of the PPCregistry plugin. This is not very efficient but works. Could anybody confirm that NSIS behaves that way?

Tom


Tomas see email


That's how it works. You can use the /NOUNLOAD switch to prevent the call to FreeLibrary or you can call LoadLibrary yourself to increase the reference count.