1 / 11

Advantages of Dynamic Linking

Advantages of Dynamic Linking. Saves memory and reduces swapping No recompilation on changes A DLL can provide after-market support Programs written in different languages can call the same DLL functions. Run Time Dynamic Linking. Loaded using LoadLibrary or LoadLibraryEx

edna
Download Presentation

Advantages of Dynamic Linking

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advantages of DynamicLinking Saves memory and reduces swappingNo recompilation on changesA DLL can provide after-market supportPrograms written in different languages can call the same DLL functions

  2. Run Time Dynamic Linking • Loaded using LoadLibrary or LoadLibraryEx • Standard search sequence will be used to locate DLLs. • Two DLLs that have the same base file name and extension but are found in different directories are not considered to be the same DLL. • If the system cannot find the DLL or if the entry-point function returns FALSE, LoadLibrary or LoadLibraryEx returns NULL. • The process can use GetProcAddress to get the address of an exported function in the DLL using a DLL module handle returned by either LoadLibrary, LoadLibraryEx, or GetModuleHandle

  3. Run Time Dynamic Linking When the DLL module is no longer needed, the process can call FreeLibrary or FreeLibraryAndExitThread. Run-time dynamic linking enables the process to continue running even if a DLL is not available.

  4. Using Run Time Loading • Add a new “Win32 Console Application” project with name “UsingRunTimeDLL” to the workspace “DLLsTraining”. • Choose an empty project • Add a new file named “UsingRunTimeDLL.cpp”. • Add code shown below to “UsingRunTimeDLL.cpp”

  5. Using Run Time Loading UsingRunTimeDLL.cpp #include <windows.h> int main() { // Load the run time DLL and ask for the address of the exported function. // It is always advised to check the return values of the calls, as it is quite // possible to get the NULL pointers as return values. HMODULE runTimeDLLHandle = LoadLibrary(TEXT("RunTimeDll.DLL")); if(runTimeDLLHandle == NULL) { MessageBox(NULL, TEXT("Failed to load the DLL"), TEXT("Error"), MB_OK ); return 0 ; }

  6. Using Run Time Loading else { // Define a user define Function Pointer type. This type will vary from the // function to function depending on their return types and parameters list. typedef void (*MyProc)(); // Query for the address of the function “SayRuntimeLoadDLL” MyProc runtimeDLLFn = (MyProc)GetProcAddress(runTimeDLLHandle, TEXT("SayRuntimeLoadDLL")); if( NULL == runtimeDLLFn ) { MessageBox(NULL, TEXT("SayRunTimeDLL function not exposed."), TEXT("Error"), MB_OK ); return 0 ; } runtimeDLLFn (); } return 1 ; }

  7. Creating A RunTime DLL • Add a new “Win32 Dynamic-Link Library” project with name “RunTimeDLL” to the workspace “DLLsTraining”. • Choose an empty DLL project • Add a new file named “RunTimeDLL.cpp”, “RunTimeDLL.h”and “RunTimeDLL.def” to RunTimeDLL project. • Add code shown below to “RunTimeDLL.cpp” and “RunTimeDLL.h”

  8. Creating A RunTime DLL RunTimeDLL.cpp #include <windows.h> #include "RunTimeDLL.h" // Definition of the function which will be exported to other modules. void SayRunTimeDLL() { MessageBox( NULL, TEXT("I am a run time DLL"), TEXT(“Information"), MB_OK ); }

  9. Creating A RunTime DLL RunTimeDLL.h // Add the include guards to protect from cyclic an redundant inclusions #ifndef _RUN_TIME_DLL_H #define _RUN_TIME_DLL_H // Here the function will be exported using .def file. So no need to export using the // declspec. This is the way to export the function from an dll that can be loaded at // runtime and asked for the addresses of the function. void SayRunTimeDLL() ; #endif

  10. Creating A RunTime DLL • RunTimeDLL.def LIBRARY "RunTimeDLL" EXPORTS SayRuntimeDLL

  11. Creating A RunTime DLL • Compile the project RunTimeDLL. The out put would be: --------------------Configuration: RunTimeDLL - Win32 Debug-------------------- Compiling... RunTimeDLL.cpp Linking... Creating library Debug/RunTimeDLL.lib and object Debug/RunTimeDLL.exp RunTimeDLL.dll - 0 error(s), 0 warning(s) • If the generation of “RunTimeDLL.lib” is absent, it means no functions were exported and DLL cannot be loaded or used. • (To test change the name of the SayRunTimeDLL to SayRunTimeDll in .def file. Observe case.)

More Related