windows memory management, memory- mapped files, and dlls

58
Windows Memory Management, Memory- Mapped Files, and DLLs

Upload: baldwin-hutchinson

Post on 27-Dec-2015

259 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Windows Memory Management, Memory- Mapped Files, and DLLs

Windows Memory Management, Memory-

Mapped Files, and DLLs

Windows Memory Management, Memory-

Mapped Files, and DLLs

Page 2: Windows Memory Management, Memory- Mapped Files, and DLLs

– 2 –

OBJECTIVES (1 of 2)OBJECTIVES (1 of 2)

Upon completion of this Chapter you will be able to:Upon completion of this Chapter you will be able to: Describe the Windows memory management architecture and

the role of heaps and memory-mapped files Use multiple independent heaps in applications requiring

dynamic memory management Use Structured Exception Handling to respond to memory

allocation errors Use memory-mapped files

Page 3: Windows Memory Management, Memory- Mapped Files, and DLLs

– 3 –

OBJECTIVES (2 of 2)OBJECTIVES (2 of 2)

Determine when to use the independent heaps and when to use memory-mapped files and to describe the advantages and disadvantages of each

Describe Windows dynamic link libraries (DLLs) Describe the difference between static, implicit, and explicit

linking Describe the advantages and disadvantages of each Use DLLs to load different implementations of the same

function

Page 4: Windows Memory Management, Memory- Mapped Files, and DLLs

– 4 –

OVERVIEW (1 of 2)OVERVIEW (1 of 2)

32-bit operating system, so pointers are 4-byte objects32-bit operating system, so pointers are 4-byte objects

Win64 provides 64-bit pointersWin64 provides 64-bit pointers

Processes have a private 4GB virtual address spaceProcesses have a private 4GB virtual address space Half (2GB) is available to a process Remainder allocated to shared data and code Win64 enlarges VA space; required for many applications

Programs can create independent memory “heaps” Programs can create independent memory “heaps” Processes can map files to memoryProcesses can map files to memory Processes can share memory through a mapped file Fast and convenient for some file processing

Page 5: Windows Memory Management, Memory- Mapped Files, and DLLs

– 5 –

OVERVIEW (2 of 2)OVERVIEW (2 of 2)

Dynamic Link Libraries with Monolithic ProgramsDynamic Link Libraries with Monolithic Programs Gather all the source code, including commonly used Chapters

such as utility functions Put all the source code in a single project Build, test, debug, and use the program Inefficiency

Recompile same code in all projectsAll executables include the same object codeWaste of disc space and physical memory at run

time Maintenance complexity as shared code changes

Page 6: Windows Memory Management, Memory- Mapped Files, and DLLs

Part IPart I

Memory Management and HeapsMemory Management and Heaps

Page 7: Windows Memory Management, Memory- Mapped Files, and DLLs

– 7 –

Windows Program

Heap API: HeapCreate, HeapDestroy, HeapAlloc, HeapFree

MMF API: CreateFileMapping, CreateViewOfFile

Virtual Memory API

Windows Kernel withVirtual Memory Manager

PhysicalMemory

Disc &FileSystem

C library: malloc, free

Memory Management ArchitectureMemory Management Architecture

Page 8: Windows Memory Management, Memory- Mapped Files, and DLLs

– 8 –

HEAPS (1 of 2)HEAPS (1 of 2)

Pools of memory within the process virtual address spacePools of memory within the process virtual address space

Every process has a default process heapEvery process has a default process heap

A process may have more than one heap. Benefits of A process may have more than one heap. Benefits of separate heaps include:separate heaps include: Fairness (between threads and between uses) Allocation efficiency (fixed size blocks in each heap) Deallocation efficiency (you can deallocate a complete data

structure with one call) Locality of reference efficiency

Page 9: Windows Memory Management, Memory- Mapped Files, and DLLs

– 9 –

HEAPS (2 of 2)HEAPS (2 of 2)

Every process has a process heapEvery process has a process heap

Every heap has a handleEvery heap has a handle

The programmer can use the process heap or create new The programmer can use the process heap or create new onesones

HANDLE GetProcessHeap (VOID)HANDLE GetProcessHeap (VOID)

Return: The handle for the process’ heap; Return: The handle for the process’ heap; NULLNULL on failure on failure

Page 10: Windows Memory Management, Memory- Mapped Files, and DLLs

– 10 –

Node

Node

Node

Record

Record

Record

Not allocated

Not allocated

Not allocated

Not allocated

ProcessHeap

RecHeap

NodeHeap

Virtual Address Space Program

ProcHeap = GetProcessHeap ( );pRoot = HeapAlloc (ProcHeap);

RecHeap = HeapCreate ( );NodeHeap = HeapCreate ( );

while ( ) { pRec = HeapAlloc (RecHeap); pNode = HeapAlloc (NodeHeap);

· · ·}

HeapFree (RecHeap, 0, pRec);HeapFree (NodeHeap, 0, pNode);HeapDestroy (RecHeap);HeapDestroy (NodeHeap);

· · ·

· · ·

· · ·

MEMORY MGT. IN MULTIPLE HEAPSMEMORY MGT. IN MULTIPLE HEAPS

Page 11: Windows Memory Management, Memory- Mapped Files, and DLLs

– 11 –

HEAP MANAGEMENT (1 of 2)HEAP MANAGEMENT (1 of 2)

HANDLE HeapCreate (DWORD flOptions, HANDLE HeapCreate (DWORD flOptions, DWORD dwInitialSize, DWORD dwMaximumSize)DWORD dwInitialSize, DWORD dwMaximumSize)

Return: A heap handle or Return: A heap handle or NULLNULL on failure on failure

dwMaximumSizedwMaximumSize — How large the heap can become — How large the heap can become 0 — “growable heap”; no fixed limit non-zero — “non-growable heap”

The entire block is allocated from the virtual address space

But only the initial size is committed in the paging file

Page 12: Windows Memory Management, Memory- Mapped Files, and DLLs

– 12 –

HEAP MANAGEMENT (2 of 2)HEAP MANAGEMENT (2 of 2)

flOptionsflOptions is a combination of two flags: is a combination of two flags: HEAP_GENERATE_EXCEPTIONS HEAP_NO_SERIALIZE

By generating exceptions, you can avoid explicit tests By generating exceptions, you can avoid explicit tests after each heap management callafter each heap management call

Page 13: Windows Memory Management, Memory- Mapped Files, and DLLs

– 13 –

HEAPSHEAPS

BOOL HeapDestroy (HANDLE hHeap)BOOL HeapDestroy (HANDLE hHeap) hHeap — a heap generated using HeapCreate Do not destroy the process’ heap (obtained using GetProcessHeap)

Benefits of HeapDestroy:

No data structure traversal codeNo need to deallocate each individual data

structure element, which can be time-consuming

Page 14: Windows Memory Management, Memory- Mapped Files, and DLLs

– 14 –

MANAGING HEAP MEMORY (1 of 4)MANAGING HEAP MEMORY (1 of 4)

LPVOID HeapAlloc (HANDLE hHeap, DWORD dwFlags, LPVOID HeapAlloc (HANDLE hHeap, DWORD dwFlags, DWORD dwBytes)DWORD dwBytes)

Return: A pointer to the allocated memory block (of size Return: A pointer to the allocated memory block (of size dwBytesdwBytes) or ) or NULLNULL on failure (unless exception on failure (unless exception generation is specified)generation is specified)

hHeaphHeap — Handle from — Handle from GetProcessHeapGetProcessHeap or or HeapCreateHeapCreate

dwFlagsdwFlags — A combination of: — A combination of: HEAP_GENERATE_EXCEPTIONS HEAP_NO_SERIALIZE HEAP_ZERO_MEMORY — Allocated memory initialized to zero

Page 15: Windows Memory Management, Memory- Mapped Files, and DLLs

– 15 –

MANAGING HEAP MEMORY (2 of 4)MANAGING HEAP MEMORY (2 of 4)

BOOL HeapFree (HANDLE hHeap, DWORD dwFlags,BOOL HeapFree (HANDLE hHeap, DWORD dwFlags,

LPVOID lpMem)LPVOID lpMem)

dwFlagsdwFlags — Should be zero (or — Should be zero (or HEAP_NO_SERIALIZEHEAP_NO_SERIALIZE))

lpMemlpMem — Should have a value returned by — Should have a value returned by HeapAllocHeapAlloc or or HeapReAllocHeapReAlloc

hHeaphHeap — Should be the heap that — Should be the heap that lpMemlpMem was allocated was allocated fromfrom

Page 16: Windows Memory Management, Memory- Mapped Files, and DLLs

– 16 –

MANAGING HEAP MEMORY (3 of 4)MANAGING HEAP MEMORY (3 of 4)

LPVOID HeapReAlloc (HANDLE hHeap, DWORD dwFlags,LPVOID HeapReAlloc (HANDLE hHeap, DWORD dwFlags,

LPVOID lpMem, DWORD dwBytes)LPVOID lpMem, DWORD dwBytes)

Return: Pointer to the reallocated block. Failure returns Return: Pointer to the reallocated block. Failure returns NULLNULL or causes exception. or causes exception.

dwFlagsdwFlags — Some essential control options: — Some essential control options: HEAP_GENERATE_EXCEPTIONS and HEAP_NO_SERIALIZE HEAP_ZERO_MEMORY — Only newly allocated memory is

initialized HEAP_REALLOC_IN_PLACE_ONLY — Do not move the block

lpMemlpMem — Existing block in — Existing block in hHeaphHeap to be reallocated to be reallocated

dwBytedwByte — New block size — New block size

Page 17: Windows Memory Management, Memory- Mapped Files, and DLLs

– 17 –

MANAGING HEAP MEMORY (4 of 4)MANAGING HEAP MEMORY (4 of 4)

DWORD HeapSize (HANDLE hHeap, DWORD dwFlags,DWORD HeapSize (HANDLE hHeap, DWORD dwFlags,

LPVOID lpMem)LPVOID lpMem)

Return: The size of the block or zero on failure.Return: The size of the block or zero on failure.

Page 18: Windows Memory Management, Memory- Mapped Files, and DLLs

– 18 –

HEAP FLAGS (1 of 2)HEAP FLAGS (1 of 2)

HEAP_NO_SERIALIZEHEAP_NO_SERIALIZE Specified in HeapCreate, HeapAlloc, and other functions Performance gain (about 15% in tests) as functions do not provide

mutual exclusion to threads accessing the heap Can safely be used if (BUT, BE CAREFUL):

Your process uses only a single threadEach thread has its own heap(s) that no other thread

can accessYou provide your own mutual exclusion mechanism to

prevent concurrent access to a heap by several threadsYou use HeapLock and HeapUnlock

Page 19: Windows Memory Management, Memory- Mapped Files, and DLLs

– 19 –

HEAP FLAGS (2 of 2)HEAP FLAGS (2 of 2)

HEAP_GENERATE_EXCEPTIONSHEAP_GENERATE_EXCEPTIONS Allows you to avoid error tests after each allocation

Page 20: Windows Memory Management, Memory- Mapped Files, and DLLs

– 20 –

OTHER HEAP FUNCTIONSOTHER HEAP FUNCTIONS

HeapValidateHeapValidate Determine whether a heap has been corrupted

HeapCompactHeapCompact Combine adjacent free blocks; decommit large free blocks

HeapWalkHeapWalk Determine all blocks allocated within a heap

Page 21: Windows Memory Management, Memory- Mapped Files, and DLLs

– 21 –

EXAMPLE-AEXAMPLE-A

Write a program, Write a program, sortHPsortHP, which reads fixed-size , which reads fixed-size records from a file into a memory-allocated buffer in a records from a file into a memory-allocated buffer in a heap, where the first 8 characters are a birth date heap, where the first 8 characters are a birth date (CCYYMMDD format). The rest of the record is a line of (CCYYMMDD format). The rest of the record is a line of text.text. Enter each date in an array, along with a file position. Each

array element will contain the date and the file position of the record (which is not fixed length).

Sort the array using the C library qsort function. Print out the complete file sorted by birth date. Repeat the process for each file on the command line. Before

each new file, destroy the heaps from the preceding file.

Page 22: Windows Memory Management, Memory- Mapped Files, and DLLs

– 22 –

EXAMPLE-AEXAMPLE-A

The The TestDataTestData directory contains two text files with 64- directory contains two text files with 64-byte records that can be used to test your program. Or, byte records that can be used to test your program. Or, use the use the RandFileRandFile program to generate sortable files of program to generate sortable files of any size.any size.

Page 23: Windows Memory Management, Memory- Mapped Files, and DLLs

– 23 –

EXAMPLE-AEXAMPLE-A

Modify the sort program to create Modify the sort program to create sortBTsortBT, which enters , which enters the records in to a binary search tree and then scans the records in to a binary search tree and then scans the tree to display the records in order.the tree to display the records in order. Allocate the tree nodes and the data in separate heaps. Destroy the heaps before sorting the next file, rather than

freeing individual tree nodes and data elements. Test the program with and without heap serialization and

determine whether there is a detectable performance difference.

Page 24: Windows Memory Management, Memory- Mapped Files, and DLLs

Part IIPart II

Memory-Mapped FilesMemory-Mapped Files

Page 25: Windows Memory Management, Memory- Mapped Files, and DLLs

– 25 –

MEMORY-MAPPED FILESMEMORY-MAPPED FILES

Advantages to mapping your virtual memory space Advantages to mapping your virtual memory space directly to normal files rather than the paging file:directly to normal files rather than the paging file: You never need to perform direct file I/O Data structures you create are saved in the file You can use in-memory algorithms (string processing, sorts,

search trees) to process data even though the file may be much larger than available physical memory

There is no need to manage buffers and the file data they contain

Multiple processes can share memory (this is the only way), and the file views will be coherent

There is no need to consume space in the paging file

Page 26: Windows Memory Management, Memory- Mapped Files, and DLLs

– 26 –

PROCESS ADDRESS SPACEMAPPED TO A FILEPROCESS ADDRESS SPACEMAPPED TO A FILE

Program

fH = CreateFile ( );mH = CreateFileMapping (fH);

while ( ) { pRecA = MapViewOfFile (mH); pRecB = MapViewOfFile (mH); pRecB -> Data = pRecA -> Data;

· · · UnmapViewOfFile (pRecA); UnmapViewOfFile (pRecB);}CloseHandle (mH);CloseHandle (fH);

· · ·

ProcessAddressSpace

File

Page 27: Windows Memory Management, Memory- Mapped Files, and DLLs

– 27 –

FILE-MAPPING OBJECTS (1 of 4)FILE-MAPPING OBJECTS (1 of 4)

HANDLE CreateFileMapping (HANDLE hFile,HANDLE CreateFileMapping (HANDLE hFile, LPSECURITY_ATTRIBUTES lpsa,LPSECURITY_ATTRIBUTES lpsa, DWORD dwProtect, DWORD dwMaximumSizeHigh,DWORD dwProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCTSTR lpMapName)DWORD dwMaximumSizeLow, LPCTSTR lpMapName)

Return: A file mapping handle or Return: A file mapping handle or NULLNULL

Page 28: Windows Memory Management, Memory- Mapped Files, and DLLs

– 28 –

FILE-MAPPING OBJECTS (2 of 4)FILE-MAPPING OBJECTS (2 of 4)

ParametersParameters hFile — Open file handle; protection flags compatible with dwProtect

LPSECURITY_ATTRIBUTES — NULL for now dwProtect — How you can access the mapped file:

PAGE_READONLY — Pages in the mapped region are read only

PAGE_READWRITE — Full access if hFile has both GENERIC_READ and GENERIC_WRITE access

PAGE_WRITECOPY — When you change mapped memory, a copy is written to the paging file

Page 29: Windows Memory Management, Memory- Mapped Files, and DLLs

– 29 –

FILE-MAPPING OBJECTS (3 of 4)FILE-MAPPING OBJECTS (3 of 4)

dwMaximumSizeHigh and dwMaximumSizeLow — Specify the size of the mapping object; 0 for current file size. The file is extended if the current file size is smaller than the map size.

lpMapName — Names the mapping object, allowing other processes to share the object

Page 30: Windows Memory Management, Memory- Mapped Files, and DLLs

– 30 –

FILE-MAPPING OBJECTS (4 of 4)FILE-MAPPING OBJECTS (4 of 4)

You can also obtain a file-mapping handle by specifying You can also obtain a file-mapping handle by specifying an existing mapping object namean existing mapping object name

HANDLE OpenFileMapping (DWORD dwDesiredAccess,HANDLE OpenFileMapping (DWORD dwDesiredAccess,

BOOL bInheritHandle, LPCTSTR lpNameP)BOOL bInheritHandle, LPCTSTR lpNameP)

Return: A file mapping handle or Return: A file mapping handle or NULLNULL

CloseHandleCloseHandle destroys mapping handles destroys mapping handles

Page 31: Windows Memory Management, Memory- Mapped Files, and DLLs

– 31 –

MAPPING PROCESS ADDRESS SPACE (1 of 3)MAPPING PROCESS ADDRESS SPACE (1 of 3)

LPVOID MapViewOfFile (HANDLE hMapObject,LPVOID MapViewOfFile (HANDLE hMapObject, DWORD dwAccess, DWORD dwOffsetHigh,DWORD dwAccess, DWORD dwOffsetHigh, DWORD dwOffsetLow, DWORD cbMap)DWORD dwOffsetLow, DWORD cbMap)

Return: The starting address of the block (file view) or Return: The starting address of the block (file view) or NULLNULL on failure on failure

hMapObjecthMapObject — Identifies a file-mapping object — Identifies a file-mapping object

dwAccessdwAccess — Must be compatible with mapping object’s — Must be compatible with mapping object’s access:access: FILE_MAP_WRITE FILE_MAP_READ FILE_MAP_ALL_ACCESS

Page 32: Windows Memory Management, Memory- Mapped Files, and DLLs

– 32 –

MAPPING PROCESS ADDRESS SPACE (2 of 3)MAPPING PROCESS ADDRESS SPACE (2 of 3)

dwOffsetHighdwOffsetHigh and and dwOffsetLowdwOffsetLow Starting location of the mapped file region Must be a multiple of 64K Zero offset to map from beginning of file

cbMapcbMap — Size in bytes of the mapped region — Size in bytes of the mapped region Zero indicates entire file

Note: The map size is limited by the 32-bit address

Page 33: Windows Memory Management, Memory- Mapped Files, and DLLs

– 33 –

MAPPING PROCESS ADDRESS SPACE (3 of 3)MAPPING PROCESS ADDRESS SPACE (3 of 3)

MapViewOfFileExMapViewOfFileEx is similar, but you can specify an is similar, but you can specify an existing addressexisting address

BOOL UnmapViewOfFile (LPVOID lpBaseAddress)BOOL UnmapViewOfFile (LPVOID lpBaseAddress) To release file views

Page 34: Windows Memory Management, Memory- Mapped Files, and DLLs

– 34 –

FILE-MAPPING LIMITATIONSFILE-MAPPING LIMITATIONS

Disparity between Windows’s 64-bit file system and 32-bit addressing

With a large file (greater than 4GB) you cannot map everything into virtual memory space

Process data space is limited to 2GB You cannot use all 2GB; available contiguous blocks will be

smaller When dealing with large files, you must create code that

carefully maps and unmaps file regions as you need them

Page 35: Windows Memory Management, Memory- Mapped Files, and DLLs

– 35 –

BASED POINTERS (1 of 2)BASED POINTERS (1 of 2)

If you use pointers in a mapped file region, they should If you use pointers in a mapped file region, they should be of type be of type _based_based A conventional pointer refers to the virtual address This address base will almost certainly be different the next

time that file is mapped or a new view is created of the same region

The pointer should be based on the view address

Page 36: Windows Memory Management, Memory- Mapped Files, and DLLs

– 36 –

BASED POINTERS (2 of 2)BASED POINTERS (2 of 2)

int *pi;int *pi; int __based(pi) *bpi, i;int __based(pi) *bpi, i; ...... pi = MapViewOfFile (...);pi = MapViewOfFile (...); *pi = 3;*pi = 3; bpi = pi;bpi = pi; i = *bpi;i = *bpi; ......

Page 37: Windows Memory Management, Memory- Mapped Files, and DLLs

– 37 –

EXAMPLE-BEXAMPLE-B

Rewrite the Rewrite the atouatou (ASCII to UNICODE) program to create (ASCII to UNICODE) program to create atouMMatouMM Use memory mapping only; do not use ReadFile and WriteFile

You do not need to change the main function in atou.c. Instead, change the asc2un.c function to create asc2unMM.c.

Page 38: Windows Memory Management, Memory- Mapped Files, and DLLs

– 38 –

EXAMPLE-BEXAMPLE-B

Rewrite the sort program of the previous section to Rewrite the sort program of the previous section to create create sortMMsortMM, so that key records (in the array) are , so that key records (in the array) are mapped to a “key” filemapped to a “key” file Do not use the file pointers; instead, use based pointers to

address in a view of the original file As part of the test of _based pointers, have a program option to

simply use the saved key file to produce a sorted listing without actually performing a sort. The next slide shows diagrams the operation.

This is a difficult exercise!

Page 39: Windows Memory Management, Memory- Mapped Files, and DLLs

– 39 –

sortMM OPERATIONsortMM OPERATIONsortMM MyFile

MyFile.idx

Ki: Key

Si: String

Pi: Based Pointer

Ki Pi Kj Pj Kk Pk···

K0 S0 K1 S1 K2 S2MyFile ···

K0 P0 K1 P1 K2 P2···

qsort

Page 40: Windows Memory Management, Memory- Mapped Files, and DLLs

Part IIIPart III

Dynamic Link LibrariesDynamic Link Libraries

Page 41: Windows Memory Management, Memory- Mapped Files, and DLLs

– 41 –

STATIC LIBRARIESSTATIC LIBRARIES

Build one or more libraries as “static libraries” Link the libraries with each project as needed

AdvantagesAdvantages Simplifies and expedites project building

DisadvantagesDisadvantages Disc and memory space issues Maintenance requires relinking and redistribution

Different programs may use different library versionsPrograms cannot use alternate utility

implementations for different situations

Page 42: Windows Memory Management, Memory- Mapped Files, and DLLs

– 42 –

DYNAMIC LINK LIBRARIES (1 of 4)DYNAMIC LINK LIBRARIES (1 of 4)

DLLs solve these and other problems very neatlyDLLs solve these and other problems very neatly Library functions are linked at:

Program load time — implicit linkingProgram run time — explicit linking

Program image can be much smaller

It does not include the library functions Multiple programs can share a single DLL

Only a single copy will be loaded into memoryAll programs map their process address space to DLL codeEach thread will have its own copy of non-shared storage

on the stack

Page 43: Windows Memory Management, Memory- Mapped Files, and DLLs

– 43 –

DYNAMIC LINK LIBRARIES (2 of 4)DYNAMIC LINK LIBRARIES (2 of 4)

New versions or alternate implementations:New versions or alternate implementations: Supplying a new version of the DLL All programs can use the new version without modification

Explicit linking:Explicit linking: Program decides at run time which library version to use Different libraries may be alternate implementations of the same

function May carry out totally different tasks

Just as separate programs do The library will run in the same process and thread as the

calling program

Page 44: Windows Memory Management, Memory- Mapped Files, and DLLs

– 44 –

DYNAMIC LINK LIBRARIES (3 of 4)DYNAMIC LINK LIBRARIES (3 of 4)

DLLs are used in nearly every operating systemDLLs are used in nearly every operating system Including UNIX and Windows 3.1 Windows (all versions) uses DLLs to implement the OS

interfaces, among other things Windows 3.1 DLLs run at the same address space for all

processes Windows DLLs run in the process’ virtual address space

Page 45: Windows Memory Management, Memory- Mapped Files, and DLLs

– 45 –

DYNAMIC LINK LIBRARIES (4 of 4)DYNAMIC LINK LIBRARIES (4 of 4)

Multiple Windows processes can share DLL codeMultiple Windows processes can share DLL code

Code, when called, runs as part of the calling process Code, when called, runs as part of the calling process and threadand thread Library can use the calling process’ resources (file handles, ...) Uses the calling thread’s stack

DLLs must be thread-safeDLLs must be thread-safe

DLLs can also export variables as well as function entry DLLs can also export variables as well as function entry pointspoints

Page 46: Windows Memory Management, Memory- Mapped Files, and DLLs

– 46 –

IMPLICIT LINKING (1 of 2)IMPLICIT LINKING (1 of 2)

Implicit, or load-time, linking is the easiest of the two Implicit, or load-time, linking is the easiest of the two techniquestechniques

Steps:Steps: Collect and built function source as a DLL Build process constructs a .LIB library file “stub” for the actual code Place .LIB in project library directory Build process also constructs a .DLL file

Page 47: Windows Memory Management, Memory- Mapped Files, and DLLs

– 47 –

IMPLICIT LINKING (2 of 2)IMPLICIT LINKING (2 of 2)

Contains the actual executable image Placed in the same directory as the application that uses it The current working directory is the secondary location Then system directory, Windows directory, PATH The program loads the DLL during its initialization You must “export” the function interfaces in the DLL source

Page 48: Windows Memory Management, Memory- Mapped Files, and DLLs

– 48 –

EXPORTING AND IMPORTING INTERFACES (1 of 3)EXPORTING AND IMPORTING INTERFACES (1 of 3)

DLL entry point must be declaredDLL entry point must be declared Microsoft C, using the _declspec (dllexport) storage

modifier:

_declspec (dllexport) _declspec (dllexport)

DWORD MyFunction (...);DWORD MyFunction (...);

Calling program declares the function is to be importedCalling program declares the function is to be imported Use the _declspec (dllimport) storage modifier

Page 49: Windows Memory Management, Memory- Mapped Files, and DLLs

– 49 –

EXPORTING AND IMPORTING INTERFACES (2 of 3)EXPORTING AND IMPORTING INTERFACES (2 of 3)

Standard technique in include fileStandard technique in include file Use a preprocessor variable such as “MYPROJ_EXPORTS“

“MYPROJ” is the project name

#ifdef MYPROJ_EXPORTS#define LIBSPEC _declspec (dllexport)#else#define LIBSPEC _declspec (dllimport)#endifLIBSPEC DWORD MyFunction (...);

Page 50: Windows Memory Management, Memory- Mapped Files, and DLLs

– 50 –

EXPORTING AND IMPORTING INTERFACES (3 of 3)EXPORTING AND IMPORTING INTERFACES (3 of 3)

The DLL project defines MYPROJ_EXPORTS Calling application leaves MYPROJ_EXPORTS undefined

You can export and import variables as well as function You can export and import variables as well as function entry pointsentry points

Page 51: Windows Memory Management, Memory- Mapped Files, and DLLs

– 51 –

EXAMPLE–CEXAMPLE–C

Build one or more of the ASCII to Unicode functions Build one or more of the ASCII to Unicode functions ((asc2unasc2un) as a DLL. Export the entry point.) as a DLL. Export the entry point. Chapter 2 version is straightforward file I/O. It can be made

faster with larger buffers, sequential scan flags, etc. Chapter 4 version uses memory mapping The two versions exhibit different performance characteristics

depending on the file system type (NTFS or FAT)

Rebuild the Rebuild the atouatou calling program (Chapter 2) so that it calling program (Chapter 2) so that it implicitely links to a implicitely links to a asc2unasc2un DLL DLL

Page 52: Windows Memory Management, Memory- Mapped Files, and DLLs

– 52 –

EXPLICIT LINKING (1 of 4)EXPLICIT LINKING (1 of 4)

Explicit (run-time) linking requires:Explicit (run-time) linking requires: Program loads a DLL be loaded — LoadLibrary Finds the address of the entry point(s) — GetProcAddress Cast the address pointer to the function type Call the function using the pointer Optionally free the library — FreeLibrary NOTE: The function is not declared in the calling program; you

declare a variable as a pointer to a function. Therefore, there is no need for the .LIB file at link time

Page 53: Windows Memory Management, Memory- Mapped Files, and DLLs

– 53 –

EXPLICIT LINKING (2 of 4)EXPLICIT LINKING (2 of 4)

HINSTANCE LoadLibrary (LPCTSTR lpLibFileName)HINSTANCE LoadLibrary (LPCTSTR lpLibFileName)

Returned handle is Returned handle is NULLNULL on failure on failure

HINSTANCEHINSTANCE, rather than a conventional , rather than a conventional HANDLEHANDLE It contains different information

Page 54: Windows Memory Management, Memory- Mapped Files, and DLLs

– 54 –

EXPLICIT LINKING (3 of 4)EXPLICIT LINKING (3 of 4)

BOOL FreeLibrary (HINSTANCE hLibModule)BOOL FreeLibrary (HINSTANCE hLibModule)

You are done with the library or want a different versionYou are done with the library or want a different version

LoadLibraryExLoadLibraryEx is similar is similar Several flags for specifying alternate search paths and loading

the library as a data file

Page 55: Windows Memory Management, Memory- Mapped Files, and DLLs

– 55 –

EXPLICIT LINKING (4 of 4)EXPLICIT LINKING (4 of 4)

To obtain the entry point:To obtain the entry point: FARPROC GetProcAddress (HMODULE hModule,

LPCSTR lpProcName)

hModulehModule is an instance produced by is an instance produced by LoadLibraryLoadLibrary Or GetModuleHandle (not described here)

lpProcNamelpProcName is the entry point name is the entry point name Cannot be Unicode

NULLNULL in case of failure in case of failure

FARPROCFARPROC, like “long pointer,” is an anachronism, like “long pointer,” is an anachronism

Page 56: Windows Memory Management, Memory- Mapped Files, and DLLs

– 56 –

Loading a DLLLoading a DLL

BOOL (*Asc2Un)(LPCTSTR, LPCTSTR, BOOL);BOOL (*Asc2Un)(LPCTSTR, LPCTSTR, BOOL); FARPROC pA2U;FARPROC pA2U;

/* Load the ASCII to Unicode function DLL file *//* Load the ASCII to Unicode function DLL file */ hDLL = LoadLibrary (argv [LocDLL]);hDLL = LoadLibrary (argv [LocDLL]); /* Get the entry point address *//* Get the entry point address */ pA2U = GetProcAddress (hDLL, "Asc2Un");pA2U = GetProcAddress (hDLL, "Asc2Un"); /* Convert to a function pointer *//* Convert to a function pointer */ Asc2Un = (BOOL (*)(LPCTSTR, LPCTSTR, BOOL)) pA2U;Asc2Un = (BOOL (*)(LPCTSTR, LPCTSTR, BOOL)) pA2U; /* Call the function *//* Call the function */ Asc2Un (argv [LocFileIn], argv [LocFileOut],Asc2Un (argv [LocFileIn], argv [LocFileOut], FALSE);FALSE);

Page 57: Windows Memory Management, Memory- Mapped Files, and DLLs

– 57 –

EXAMPLE–D (1 of 2)EXAMPLE–D (1 of 2)

Modify the Modify the atouatou program so that the name of the DLL program so that the name of the DLL file is on the command linefile is on the command line Then load the DLL and call it, as illustrated on the previous

slide

Page 58: Windows Memory Management, Memory- Mapped Files, and DLLs

– 58 –

EXAMPLE–D (2 of 2)EXAMPLE–D (2 of 2)

Here are some suggested extensions, not included in Here are some suggested extensions, not included in the lab solution:the lab solution: Modify the calling program so that it sequentially loads and

calls several alternative implementations Modify the calling program so that it determines the operating

environment (OS version, file system types, etc.) and then loads the most efficient implementation for the environment. Use your knowledge from previous performance experiments to determine the best implementation for a given situation.