Dynamic Loading of DLLs |
Version 7 |
ATEasy allows the user to dynamically load and unload DLL library at run time using the LoadLibrary and UnloadLibrary statements. By default a DLL library is loaded by ATEasy when you start (run) the application. If the DLL is marked as LoadOnAccess (from the DLL properties window) loading is deferred until the time when a DLL procedure is first called.
Loading and Unloading a DLL allow the application to dynamically switch the module DLL implementation with another DLL's implementation. This can be useful when creating a DLL-based driver that supports different implementation but with the same procedures' names and parameters, each residing in a separate DLL. The DLL dynamically loaded should have similar exported functions and parameters (parameter count, type and return value) as defined in the module. However, the implementation of the exported functions can be different.
In order to use dynamic DLLs, first insert a DLL of the name symbol under submodule Libraries. The DLL name must be marked as public if the LoadLibrary and/or UnloadLibrary statements need to be called from external module.
During Insert DLL, you can Import ATEasy DLL Procedures automatically or you can provide C header file so that its procedures are to be inserted automatically. Otherwise, you need to the declare the DLL exported functions manually under the DLL Procedures submodule.
The LoadLibrary statement loads a DLL library. With the statement you specify two arguments: a DLL name symbol and its path to locate the DLL. DLL could be any DLL including ATEasy DLL. For example:
LoadLibrary gxsw32, "gxsw32.dll"
LoadLibrary MUX.gxsw, "c:\\gxsw32.dll"
The UnloadLibrary statement unloads a DLL library of name specified as its argument. For example:
UnloadLibrary gxsw32
UnloadLibrary Mux.gxsw
ATEasy does not perform any check to verify that the newly loaded DLL has the same export function names. This is done when the DLL procedure is actually called. In addition ATEasy cannot perform a check to see if the DLL procedure being called has the same parameters count and type since DLLs do not have this information. Calling a DLL procedure with different number of arguments (as defined in ATEasy) may result in run-time error or memory corruption. Special care must be taken when defining the ATEasy DLL procedure and parameters types to make sure it matches the actual DLL exported function.
The LoadLibrary statement will first unload the DLL if loaded already and load one newly of the path provided. If the path is not relative or absolute then ATEasy follows the searching rules: Windows directory followed by Windows System's directory, and followed by the directories listed to the PATH environment variable.
When the LoadLibrary is called, the DLL reference count is incremented. For non-ATEasy DLL the DLL LibMain is called to notify that it is loaded. For ATEasy DLL, Initialization (OnInit/OnInitSystem) events are called after the DLL is loaded and before a DLL procedure is called.
When the DLL is unloaded, it is removed from the process memory if the DLL's reference count is 0. Also the DLL LibMain (or OnEndSystem/OnEnd events for ATEasy DLL) will be called notifying the DLL that it is about to be unloaded from the memory.