This background is necesssary to understand the relationship of DLL's to Windows.
The first versions of Microsoft Windows ran every program in a single address space. Every program was a child of the GUI interface. All Operating-System level operations were provided by the underlying operating system: MS-DOS. All higher level services were provided by Windows Libraries Dynamic Link Libraries. The Drawing API, GDI, was implemented in a DLL called GDI.EXE, the user interface in USER.EXE. These extra layers on top of DOS had to be shared across all running windows programs, not just to enable Windows to work in a machine with less than a megabyte of RAM, but to enable the programs to co-operate with each other. The Graphics Device Interface code in GDI needed to translate drawing commands to operations on specific devices. On the display, it had to manipulate pixels in the frame buffer. When drawing to a printer, the API calls had to be transformed into requests to a printer. Although it could have been possible to provide hard-coded support for a limited set of devices (like the Color Graphics Adapter display, the HP LaserJet Printer Command Language), Microsoft chose a different approach. GDI would work by loading different pieces of code to work with different output devices—pieces of code called 'Device Drivers'.
The same architectural concept that allowed GDI to load different device drivers is that which allowed the Windows shell to load different windows programs, and for these programs to invoke API calls from the shared USER and GDI libraries. That concept was Dynamic Linking.
In a conventional non-shared, static library, sections of code are simply added to the calling program when its executable is built at the linking phase; if two programs use the same routine, the code has to be included in both. With dynamic linking, shared code is placed into a single, separate file. The programs that call this file are connected to it at run time, with the operating system (or, in the case of early versions of Windows, the OS-extension), performing the binding.
For those early versions of Windows (1.0 to 3.11), the DLLs were the foundation for the entire GUI.
Windows Screen savers were an example of DLLs with a .SCR extension.
This notion of building up the operating system from a collection of dynamically loaded libraries is a core concept of Windows that persists even today. DLLs provide the standard benefits of shared libraries, such as modularity. Modularity allows changes to be made to code and data in a single self-contained DLL shared by several applications without any change to the applications themselves.
Another benefit of the modularity is the use of generic interfaces for plug-ins. A single interface may be developed which allows old as well as new modules to be integrated seamlessly at run-time into pre-existing applications, without any modification to the application itself. This concept of dynamic extensibility is taken to the extreme with the Component Object Model, the underpinnings of ActiveX.
In Windows 1.x, 2.x and 3.x, all windows applications shared the same address space, as well as the same memory. A DLL was only loaded once into this address space; from then on all programs using the library accessed it. The library's data was shared across all the programs. This could be used as an indirect form of Inter-process communication, or it could accidentally corrupt the different programs. With Windows 95 and successors every process runs in its own address space. While the DLL code may be shared, the data is private except where shared data is explicitly requested by the library. That said, large swathes of Windows 95, Windows 98 and Windows Me were built from 16-bit libraries, a feature which limited the performance of the Pentium Pro microprocessor when launched, and ultimately limited the stability and scalability of the DOS-based versions of Windows.
While DLLs are the core of the Windows architecture, they have a number of drawbacks, collectively called "DLL hell". Currently, Microsoft promotes Microsoft .NET as one solution to the problems of DLL hell, although they now promote Virtualization based solutions such as Microsoft Virtual PC and Microsoft Application Virtualization, because they offer superior isolation between applications. An alternative mitigating solution to DLL hell has been the implementation of Side-by-Side Assembly.
The use of DLLs helps promote modularization of code, code reuse, efficient memory usage, and reduced disk space. Therefore, the operating system and the programs load faster, run faster, and take less disk space on the computer.
When an EBASIC program uses a DLL, an issue that is called dependency may cause the program not to run. When a program uses a DLL, a dependency is created. If another program overwrites and breaks this dependency, the original program may not successfully run.
By using a DLL, a program can be modularized into separate components. For example, an accounting program may be sold by module. Each module can be loaded into the main program at run time if that module is installed. Because the modules are separate, the load time of the program is faster, and a module is only loaded when that functionality is requested.
Additionally, updates are easier to apply to each module without affecting other parts of the program. For example, you may have a payroll program, and the tax rates change each year. When these changes are isolated to a DLL, you can apply an update without needing to build or install the whole program again.
The following list describes some of the files that are implemented as DLLs in Windows operating systems:
The following list describes some of the advantages that are provided when a program uses a DLL:
Emergence BASIC supports using external DLL's to extend the capabilities of the language through the use of import libraries. An import library is a special .LIB file that contains the names and entry points of all of the functions contained within a particular DLL. The Windows API is a collection of functions in system DLL's located in either the windows\system or windows\system32 directory.
EBASIC includes import libraries for many of the standard Windows API DLL's. All import libraries are located in the libs directory in the main installation directory for Emergence BASIC. See the list at the end of the topic.
An include file containing declares, types and constants for much of the Windows API is included with your installation. All Win API function names are aliased with a leading underscore and to remove the 'A' on ANSI function names. Example:
To create an import library for a DLL select the Tools menu and select Create Import Library . Browse to the DLL you wish to use in your programs and click on the Open button. The new import library will have a .lib extension with the same name as the DLL and will be copied automatically to the libs directory. You only need to do this once for a new DLL. Once the import library is added you can use it in any program.
Including the import library in the build
After successfully creating an import library for a DLL you need to tell the Emergence BASIC Linker that you wish to include it when compiling programs. Use the $USE preprocessor command to include linker and import libraries.
You may also specify a full path name.
You do not need to use the $USE command to access any of the functions available in the default Windows API DLL's. The linker automatically includes the import libraries for the DLL's listed at the end of the topic.
To use a function in a DLL it must first be declared using the IMPORT keyword. The IMPORT keyword tells the compiler that the
function is located in either a DLL or import library.
Note the comma in the DECLARE statement. You can alias function names to resolve naming conflicts or to declare the same function more than once with different parameter types.
Functions that use the C calling convention will usually be noted in the documentation for the DLL and can be used by including the CDECL keyword. The Windows API function wsprintf uses the C calling convention. <
The C runtime DLL uses the import library crtdll.lib. However it is linked differently from other import libraries. To use the functions contained within the C runtime DLL use the EXTERN keyword instead of the IMPORT keyword.
All functions in the C runtime library require the CDECL keyword and usually begin with an underscore.
Calling functions with C name mangling
Certain C and C++ compilers create DLL's with what's known as mangled function names. The format of the mangling varies from compiler to compiler but is generally the function name with a beginning underscore and a trailing @ symbol with a number. The number is used by other linkers to determine how many parameters are pushed on the stack. The DLL's creator can map the function names to normal ones but in most cases the name mapping is not done.
To use DLL functions with mangled function names you must use the EXTERN keyword instead of the IMPORT keyword. For example if a C DLL was created with a function named INIT_the_system(bInit as INT),INT then the actual name exported in the DLL would be:
The correct DECLARE would look like:
Import libraries included with Emergence BASIC.
The list of import libraries included with the distribution is subject to change. To be sure check the contents of the libs directory. Other import libraries internal to the use of the compiler are not listed here.