Archived:Loading DLL by RLibrary
This code snippet shows how to load a polymorphic DLL dynamically.
A static DLL is automatically loaded in the RAM when a program that uses it is started. It is also automatically unloaded when it is not needed anymore.
A polymorphic DLL is loaded by calling RLibrary::Load() and unloaded using RLibrary::Close(). Several polymorphic DLLs can show the same interface to their clients. This kind of DLLs are generally used by a framework to provide plug-in features.
The following capabilities and libraries are required:
- UID2 must be 0x1000008d in DLL's MMP file.
- DLL must have equal or greater CAPABILITIES than the loading process. Once loaded, DLL runs at the capability level of the loading process. Therefore, a DLL must have all capabilities required by all its client executables, even if the code within the DLL itself does not require some of these capabilities.
- Define VIRTUAL INTERFACE class that the polymorphic DLLs will implement
- DLL should have one EXPORTED static function that returns an instance of the interface's class.
Your DLL header
virtual TInt Data() = 0;
class CMyDll : public MMyDll
IMPORT_C static MMyDll* NewL();
virtual TInt Data();
EXPORT_C MMyDll* CMyDll::NewL()
return new CMyDll();
Loading DLL dynamically
// Load dll
// Find exported function
MMyDll* mydll=(MMyDll*) NewL();
// Close the library
TInt value = mydll->Data();
The DLL is loaded dynamically.