Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Chapter 10 Linking and Loading. Separate assembly creates “.mob” files.
Memory Protection: Kernel and User Address Spaces  Background  Address binding  How memory protection is achieved.
Linking & Loading CS-502 Operating Systems
Chapter 3 Loaders and Linkers
Computer Organization CS224 Fall 2012 Lesson 12. Synchronization  Two processors or threads sharing an area of memory l P1 writes, then P2 reads l Data.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
Dragan Bojić University of Belgrade Porting XCTL from Borland C++ to Visual C++
1 Lecture 11 Operating System Interaction This lecture covers three items, thread, is the most interesting as it makes the system faster if you know how.
Here’s how to start the Microsoft Visual C++ program; click on start, then programsclick on start, then programs Choose Microsoft Visual C++ or Microsoft.
C++ fundamentals.
Chapter 91 Memory Management Chapter 9   Review of process from source to executable (linking, loading, addressing)   General discussion of memory.
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
1 Introduction to Tool chains. 2 Tool chain for the Sitara Family (but it is true for other ARM based devices as well) A tool chain is a collection of.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
Separate Assembly allows a program to be built from modules rather than a single source file.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
1 I-Logix Professional Services Specialist Rhapsody IDF (Interrupt Driven Framework) CPU External Code RTOS OXF Framework Rhapsody Generated.
chap13 Chapter 13 Programming in the Large.
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Getting Started The structure of a simple wxWidgets program, Look at where and how a wxWidgets application starts and ends, how to show the main window,
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Win32 Programming Lesson 20: Advanced DLL Techniques.
Linking and Loading Linker collects procedures and links them together object modules into one executable program. Why isn't everything written as just.
Chapter 6: User-Defined Functions
Saves memory and reduces swapping No recompilation on changes A DLL can provide after-market support Programs written in different languages can call the.
Renesas Technology America Inc. 1 SKP8CMINI Tutorial 2 Creating A New Project Using HEW.
Lecture 11 Dynamic link libraries. Differences between static libraries and DLLs In static library code is added to the executable. In DLL, the code is.
1 Memory Management Requirements of memory management system to provide the memory space to enable several processes to execute concurrently to provide.
Module 6: Debugging a Windows CE Image.  Overview Debug Zones IDE Debug Setup IDE Debug Commands Platform Builder Integrated Kernel Debugger Other Debugging.
CSE451 Linking and Loading Autumn 2002 Gary Kimura Lecture #21 December 9, 2002.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
C/C++ Programming Environment
111 Introduction to OGRE3D Programming: Main Loop.
Dynamic Link Libraries: Inside Out. Dynamic Link Libraries  About Dynamic-Link Libraries  Dynamic-Link Libraries Hands On  Dynamic Link Library Reference.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Win32 Programming Lesson 19: Introduction to DLLs.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Program Libraries 1. What is a program library? A library is a collection of implementations of behavior, written in terms of a language, that has a well-defined.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Linking & Loading.
Separate Assembly allows a program to be built from modules rather than a single source file assembler linker source file.
14. THE PREPROCESSOR.
CS-3013 Operating Systems C-term 2008
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Pre-processor Directives
Dynamic Link Libraries (DLL)
1. Open Visual Studio 2008.
C Preprocessor(CPP).
Memory Management Tasks
Linking & Loading CS-502 Operating Systems
CSE 303 Concepts and Tools for Software Development
Writing Large Programs
Module 6: Debugging a Windows CE Image
Linking & Loading CS-502 Operating Systems
The Three Attributes of an Identifier
Executable program (p1)
SPL – PS1 Introduction to C++.
Presentation transcript:

Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system to locate the DLL. 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. The operating system calls the entry-point function DllMain() whenever a process or a thread loads or unloads the DLL. If the system cannot find the DLL or if the entry-point function returns FALSE, LoadLibrary() or LoadLibraryEx() returns NULL.

Writing a Run Time DLL 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(). Once the application has the address of the DLL function it can use a pointer to call the function. When the DLL module is no longer needed, the process can call FreeLibrary() or FreeLibraryAndExitThread().

Writing a Run Time DLL Writing a Run Time DLL in Visual C++ requires you to add a definition file, a.def file, to export the functions inside the DLL.

Advantages of Run-time DLL It enables the application to continue running even if a DLL is not available. For example, the process could notify the user of the error and have the user specify another path to the DLL. To improve startup performance, an application can implicitly link to those DLLs needed immediately after loading and wait to explicitly link to the other DLLs when they are needed.

Advantages of Run-time DLL The name of the DLL might not be available at compile time and thus the application might need to obtain the name of the DLL and the exported functions from a configuration file. Explicit linking eliminates the need to link the application with an import library. If changes in the DLL cause the export ordinals to change, applications using explicit linking do not have to relink, whereas applications using implicit linking must relink to the new import library.

Disadvantage of Run Time DLL Using a Run Time DLL forces the programmer to explicitly load the DLL as part of the code. Alternative method is to write a Load Time DLL.

Load Time Dynamic Linking A Load Time DLL is said to be linked implicitly to the application whereas a Run Time DLL is linked explicitly, hence the terms implicit and explicit linking. Implicit linking occurs at compile time when an application’s code makes a reference to an exported DLL function. When the source code for the application is compiled, the call to the DLL function translates to an external function reference in the object code. To resolve this external reference, the application must link with the import library (.LIB file) that is produced when the DLL is built. In the VC++ IDE before compiling the application one specifies the import library in the Project | Settings Dialog on the Link tab.

Load Time Dynamic Linking It is also necessary to identify the DLL functions used by the application as being imported functions. Do this by listing, at the top of the source file, the DLL functions being called and placing __declspec(dllimport) to the left of each of the DLL function names. You can then directly make a call to the functions of the DLL by calling them just like normal functions. After compiling the project, copy the Load Time DLL to the directory in which the application is present and run the application as normal.

Load Time Dynamic Linking When the application implicitly links to the DLL it looks just like static linking. However the library that the application links to does not contain the DLL code. The import library only contains information about the exported symbols from the DLL and no actual code that helps the linker resolve any function calls made to the DLL. If the linker finds the function export information in a lib file (import library), it assumes that the code for that function exists in a DLL, and to resolve references to that function the linker simply adds information to the final executable file that can be used by the system loader when the process starts. These are known as stubs, i.e. function references that are used during execution time to find the DLL functions. Hence it is still Dynamic Linking. As the process is loaded into Ram, the loader modifies the executable code of the process to point to DLL functions wherever a reference to them is made.

Load Time Dynamic Linking When the system starts a program that uses load-time dynamic linking, it uses the information in the file to locate the names of the required DLL(s). Following search sequence will be used to locate DLLs. 1.The directory from which the application loaded. 2.The current directory. 3.The Windows system directory. Use the GetSystemDirectory function to get the path of this directory. 4.The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System. 5.The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. 6.The directories that are listed in the PATH environment variable. The DLL is mapped into the virtual address space of the process during its initialization and is loaded into physical memory only when needed.

Using Load Time Loading Add a new “Win32 Console Application” project with name “UsingLoadTimeDLL”. Choose an empty project Using Project->Dependencies, add the dependence to LoadTimeDLL. Add a new file named “UsingLoadTimeDLL.cpp”. Add code shown below to “UsingLoadTimeDLL.cpp”

Using Load Time Loading ( Contd … ) UsingLoadTimeDLL.cpp // Using LoadTimeDlls functionality by importing header file. // Don't define LOAD_TIME_DLL_EXPORTS symbol. Absence of this // symbol makes the __declspec(dllimport) visible to this file and hence // forces the compiler to fetch the definition form the DLL. #include “../LoadTimeDLL/LoadTimeDLL.h” int main() { // You can directly make a call to exported functions of the load time DLL. // Call a function of the Load time loaded DLL. SayLoadTimeDLL(); return 0 ; }

Using A Load Time DLL ( Contd … ) LoadTimeDLL.h //Add the include guards to protect from cyclic an redundant inclusions #ifndef _LOAD_TIME_DLL_H #define _LOAD_TIME_DLL_H #ifdef LOAD_TIME_DLL_EXPORTS // This is syntax that has to be followed to export a function that can be used // from other module, which loaded this dll. “__declspec(dllexport)” tells the // compiler to export the definition of this function. extern __declspec(dllexport) void SayLoadTimeDLL() ; #else // This is syntax that has to be visible for the module that uses this function. // “__declspec(dllimport)” tells the compiler to bring the definition of this // function from a DLL. extern __declspec(dllimport) void SayLoadTimeDLL() ; #endif #endif

Using Load Time Loading ( Contd … ) Compile the project. Copy the LoadTimeDLL.dll to the directory in which UsingLoadTimeDLL.exe presents. Run UsingLoadTimeDLL.exe. This will pop up a message box saying “I am a load time DLL”.

Dynamic Link Library Creation –Creating Source Files –Exporting Functions –Creating an Import Library

Creating A Load Time DLL Add a new “Win32 Dynamic-Link Library” project with name “LoadTimeDLL” to the workspace “DLLsTraining”. Choose an empty DLL project Add a new file named “LoadTimeDLL.cpp” and “LoadTimeDLL.h” to LoadTimeDLL project. Add code shown below to “LoadTimeDLL.cpp” and “LoadTimeDLL.h”

Creating A Load Time DLL ( Contd … ) LoadTimeDLL.cpp #include // Define the symbol LOAD_TIME_DLL_EXPORTS before including // "LoadTimeDll.h". This makes the __declspec(dllexport) visible to the // implementing.cpp file. When using this header in other modules, don’t define // this symbol. Then those modules will see __declspec(dllimport). This makes // those modules to import the function form the DLLs. #define LOAD_TIME_DLL_EXPORTS #include "LoadTimeDLL.h" void SayLoadTimeDLL() { MessageBox(NULL, TEXT(“Information"), TEXT("I am a load time DLL"),MB_OK); }

Exporting Load Time DLL Functions LoadTimeDLL.h //Add the include guards to protect from cyclic an redundant inclusions #ifndef _LOAD_TIME_DLL_H #define _LOAD_TIME_DLL_H #ifdef LOAD_TIME_DLL_EXPORTS // This is syntax that has to be followed to export a function that can be used // from other module, which loaded this dll. “__declspec(dllexport)” tells the // compiler to export the definition of this function. extern __declspec(dllexport) void SayLoadTimeDLL() ; #else // This is syntax that has to be visible for the module that uses this function. // “__declspec(dllimport)” tells the compiler to bring the definition of this // function from a DLL. extern __declspec(dllimport) void SayLoadTimeDLL() ; #endif #endif

Creating A Load Time DLL ( Contd … ) Compile the project LoadTimeDLL. The out put would be: Configuration: LoadTimeDLL - Win32 Debug Compiling... LoadTimeDLL.cpp Linking... Creating library Debug/LoadTimeDLL.lib and object Debug/LoadTimeDLL.exp LoadTimeDLL.dll - 0 error(s), 0 warning(s) If the generation of “LoadTimeDLL.lib” is absent, it means no functions were exported and DLL cannot be loaded used. (To test change the name of the SayLoadtimeDLL to SayLoadtimeDll. Observe case.)

Dynamic Link Library Entry Point The system calls the entry-point function whenever processes and threads load or unload the DLL. Calling the Entry-Point Function The system calls the entry-point function whenever any one of the following events occurs:  A process loads the DLL.  A process unloads the DLL.  A new thread is created in a process that has loaded the DLL. Use DisableThreadLibraryCalls function to disable notification when threads are created.  A thread of a process that has loaded the DLL terminates normally, not using TerminateThread or TerminateProcess.

Dynamic Link Library Entry Point ( Contd … ) Entry-Point Function Definition The DLL entry-point function must be declared with the standard-call calling convention. DLL entry point can be called in following scenarios:  A process loads the DLL (DLL_PROCESS_ATTACH)  The current process creates a new thread (DLL_THREAD_ATTACH)  A thread exits normally (DLL_THREAD_DETACH)  A process unloads the DLL (DLL_PROCESS_DETACH) Should perform only simple initialization tasks, such as setting up thread local storage (TLS), creating synchronization objects, and opening files

Dynamic Link Library Entry Point ( Contd … ) Entry-Point Function Example BOOL WINAPI DllMain( HINSTANCE hinstDLL, // handle to DLL module DWORD fdwReason, // reason for calling function LPVOID lpReserved ) // reserved { // Perform actions based on the reason for calling. switch( fdwReason ) { case DLL_PROCESS_ATTACH: // Initialize once for each new process. Return FALSE to fail DLL load. break; case DLL_THREAD_ATTACH: // Do thread-specific initialization. break; case DLL_THREAD_DETACH: // Do thread-specific cleanup. break; case DLL_PROCESS_DETACH: // Perform any necessary cleanup. break; } return TRUE; // Successful DLL_PROCESS_ATTACH. }

Dynamic Link Library Entry Point ( Contd … ) Entry-Point Function Return Value TRUE indicates success. FALSE indicates failure.