Docsity

Understanding Windows DLLs: Processes, Threads, Linking, and DLL Exports, Slides of Windows Programming

Partial preview of the text

Download Understanding Windows DLLs: Processes, Threads, Linking, and DLL Exports and more Slides Windows Programming in PDF only on Docsity!

LL e c te c t u ru r ee

Review of Last Lecture

  • Windows Common Controls
  • Common controls library commctl32.dll
  • InitCommonControlsEx()
  • Image Lists
  • List View control

Basic concepts and today’s topics

  • What is a Process? A running application that consists of a private virtual address space, code, data, and other operating-system resources, such as files, pipes, and synchronization objects that are visible to the process. A process also contains one or more threads that run in the context of the process

What is a Thread?

  • What is a Thread? A thread is basically a path of execution through a program. It is also the smallest unit of execution that Win32 schedules. A thread consists of a stack, the state of the CPU registers, and an entry in the execution list maintained by Windows. Each thread shares all of the process’s resources.

contd…

Linking and the Compiled Code

  • What is compiled .OBJ code?
  • The purpose of the linker
  • Static Linking
  • Dynamic Linking
  • Dynamic Link Libraries (DLLs)

Dynamic Link Libraries

  • Why a DLLs in not an EXE?
  • Basic structure of a DLL
  • The DLL entry point function
  • DllMain() function

BOOL WINAPI DllMain( HINSTANCE hinstDLL, // handle to DLL module DWORD fdwReason, // reason for calling function LPVOID lpvReserved // reserved );

DLL exports and imports

  • The export table
  • How to export and import code (functions) in a DLLs
  • Import data
  • __declspec( dllimport ) int i;
  • Export code
  • __declspec( dllexport ) void function(void);

Calling Conventions and DLLs

  • Significance of Calling Conventions of the caller and the called function in a DLL
  • C++ uses same calling convention / parameter passing as C, but performs name decoration
  • extern “C” { … … function declarations … } prevents C++ name-decoration

Load-time vs. Runtime Dynamic Linking

  • Load-time Dynamic Linking: .LIB file contains all exported function addresses
  • Runtime Dynamic Linking: LoadLibrary()
  • .DEF module definition files can be used instead of dllexport/dllimport.
  • Using .LIB is safer sometimes in the sense that the programme stub refuses to load the main programme if some DLL can not be loaded.
  • .DEF files are less common now, but are more powerful. Docsity.com

Loading a DLL and calling functions in it

HMODULE LoadLibrary( LPCTSTR lpFileName // file name of module );

FreeLibrary(hModule);

FARPROC GetProcAddress( HMODULE hModule, // handle to DLL module LPCSTR lpProcName // function name );

Example

Using myDll.DLL in your programme

  • Call LoadLibrary() at runtime to load the DLL
  • Call GetProcAddress() to get a pointer to the function sum() in your programme
  • Use indirection to this pointer to function as normal to call the function.

The Import Libraries .LIB

  • Concept of an import library .LIB
  • Import Library is statically linked
  • How to create an import library?
  • Important System DLLs: Kernel32.dll, User32.dll, Gdi32.dll
  • Import Libraries of System DLLs: Kernel32.lib, User32.lib, Gdi32.lib

DLL versions

  • Why versioning?

The comctl32.dll example

  • The VERSIONINFO resource

statement

VERSIONINFO resource statement VS_VERSION_INFO VERSIONINFO FILEVERSION 1,0,0, PRODUCTVERSION 1,0,0, FILEFLAGSMASK 0x3fL FILEFLAGS 0x1L FILEOS 0x40004L FILETYPE 0x1L FILESUBTYPE 0x0L BEGIN BLOCK "StringFileInfo" BEGIN BLOCK "040904b0" BEGIN VALUE "CompanyName", "Virtual University\0" VALUE "LegalCopyright", "Copyright © 2002 Virtual University\0" END ... ... ... ... END END