130 likes | 249 Views
Win32 Programming. Lesson 19: Introduction to DLLs. Where are we?. Memory allocation: sorted Processes and Threads: Done But… how does Windows structure its executables – what are DLLs and how can we use them to our advantage?. DLLs. Understanding DLLs is essential to understanding Windows
E N D
Win32 Programming Lesson 19: Introduction to DLLs
Where are we? • Memory allocation: sorted • Processes and Threads: Done • But… how does Windows structure its executables – what are DLLs and how can we use them to our advantage?
DLLs • Understanding DLLs is essential to understanding Windows • Three most important: • Kernel32: Memory, processes and threads • User32: User interface tasks • GDI32: Graphics • However, Windows contains lots of DLLs • The entire Win32 API is in DLLs
DLL Features • Using DLLs: • Extends the features of an application • Language-independent (that is, you can write DLLs in many different languages) • Simplify project management (modular development and versioning) • Conserve memory (remember, DLLs share memory) • Resource sharing (shared dialogue boxes, for example) • Helpful for localization (different DLLs for different locales) • Platform independence (loading on older versions, for example) • Special Functions (like hooking into existing applications :: evil grin :: )
DLLs and the Process • To use a DLL a Process needs to load it into its address space • Once this occurs, the DLL looks, for all intents and purposes, like the part of the process, but the DLL is shared between multiple processes • How? Remember PAGE_WRITECOPY?
Implications • Because of the close relationship of the DLL to the process objects created by the DLL are owned by the process • If the DLL calls VirtualAlloc, for example, that memory counts against the process’ usage • If the DLL is later unmapped this memory is not automatically freed
Code Problem • The temptation is to always free memory allocated by the DLL directly – that is, if the DLL uses malloc to provide some memory, the process should call free when it’s done • But this is WRONG! • What if the underlying libraries are different? • If a DLL allocated memory, it should provide a call to free it. Ignore this at your own peril.
How DLLs work • For the sake of this discussion, we’ll talk about “executables” (programs) and DLLs (modules) even though DLLs are executable themselves • We’ll look at the lifecycle of a DLL and executable
First • Before we can use a DLL you need to build it • This means we tell Visual Studio that we’re building a DLL • Create a header file that details the exported types • Linker produces a .lib file if at least one function is exported • Linker produces a .dll file
Second • Build an application using the DLL • Compiler uses DLL header file to build .obj files • The .lib file is used to resolve imported functions
Headers • One of the most important parts of the DLL is the header file, as this will be used when compiling programs which use the DLL • Best to use the same header file for building the DLL as for building files which use the DLL • You can look at exports and imports using dumpbin (from VS 2013)
Running the Executable • The OS looks for the needed imports • Where? • The directory containing the executable image file • The process’s current directory • The Windows system directory • The Windows directory • The directories listed in the PATH environment variable
Assignment • Write your own DLL • Create a .h file which can be used by programs which call your DLL • Recast your linked list program (remember that first assignment!) as a linked-list DLL which exports the functions I specified • Make sure you work out how to free memory correctly in your DLL • Write a program which compiles and runs using your linked list DLL