DLL Part One - Creating DLL Files

Building Dynamic Libraries in Windows

    Introduction

    As you have had many years experience seeing DLL files using Windows OS, you might not have been aware of what they were and what they do. DLL means Dynamic Link Libraries, as they link libraries to applications during the runtime of the program. As you should know by now libraries are pre-written and compiled code that allows you to include, embed or link to your main program; as they contain ready to use functions and frameworks to work within the program. With Static Libraries in Windows (extension .lib), which can be compiled within the program code during the build; DLL files (extension .dll) are independent as they are linking externally to the program during runtime. Code that is build within the DLL is totally independent, so if you want to change the DLL file, without rebuilding the application; it is possible. The caveat to that is not to make too many drastic changes, or they will conflict with the executing program as it may depend on specific functions used from within the DLL file.

    DLL In a Nutshell

    The beauty of DLL files within development, is that they are interchangeable not only with applications; but they are also handy for web development, plug ins and other appliances which you can use within a Windows-based system. As the functions within the DLL file are most commonly written in C, the possibility of porting to other languages is easily accomplished. With these advantages, the key point of using a DLL file is to keep your code modular; instead of recompiling the large code base of your application, you can place code with specific tasks within the DLL and change and recompile when necessary. DLL also helps keep your program streamline with this modular architecture as you only allow the specific portion of code to load during runtime.

    Setting up a DLL Project

    Now we will get on with building our own DLL files within a Visual Studio Express project. The example program we will create in the tutorial is going to be simple enough that you will understand the simple concept of building and running a DLL file. We will create a simple DLL file, which the program will run during its initialising before executing. First we will create a solution like what we have done before; in this example, we are creating a simple console application, which will be called MainApplication.

    Once the first project which the console application will be under, create a simple source file (in this case named main.c). Once created, leave it empty for now. Then inside the Solution Explorer, select the Solution icon above at the top of the structured tree (like in the image above), and right click and select in the menu Add > New Project. In this example, it is named DLLModule.

    Then we will get the Application Wizard to create a new Project inside our Solution. This time, inside Application Settings, select DLL inside Application Type (shown in image above). Now that the DLL project is ready, we will create new source and header files (these will be called interface.c for source and interface.h for header, like image below). For now the source files will be blank, so we can concentrate on organising projects and dependencies; so the coding work we will be doing later will be done without problems.

    To make sure the MainApplication program understands code the DLLModule uses, we have to link it's header files to the program. So to do that we have to link the header files from the DLLModule Project. First, link the 'includes' directory path inside the MainApplication project by going to Properties Pages, open Configuration Properties > C/C++ > General, then inside the Additional Include Directories, place in the correct path (like shown below).

    Once the header file is linked, we need to let the compiler know where the dependencies are whilst building the program. To do this we need to go into Project Dependencies by selecting MainApplication inside the Solution Explorer, then right click simply select Project Dependencies, once the window comes up (see image below), select MainApplication inside the dropdown menu and make sure you have the checkbox for DLLModule checked. As the project dependencies are established, when creating the DLL file, you are also creating a static library (*.lib) file. This will automatically bind the DLL as the glue to the application during compilation. In doing this we have linked the DLL project (in this case DLLModule) to the MainApplication program, which it will depend on. This is important for compiling and building the final program.

    Example Code In Detail

    With the necessary projects linking to make it possible to compile the code, we can proceed with the code. We have already got three source and header files ready, so the first one we will work with will be interface.h, which will be located inside the DLLModule. This header file will define the functions used within the DLL file.

    #ifndef INTERFACE_H
    #define INTERFACE_H
    
    #include <windows.h>
    #include <stdio.h>

    First, as shown above; we will define the interface header file to begin with as usual. and include windows.h and stdio.h header files. You cannot create or run a DLL file without including the window.h header file. As you might have noticed, this DLL file, as well as the main program will be written purely in C; mainly for simple clarity.

    #ifdef RELIANT_EXPORTS
    #define RELIANT_API __declspec(dllexport)
    #else
    #define RELIANT_API __declspec(dllimport)
    #endif
    
    RELIANT_API void SimpleString();

    Now, as you can see above there has been an macro conditional statement (using #ifdef which follows the macro RELIANT_EXPORTS. This checks if the corresponding source file is importing or exporting your function from the DLL. The macro condition will check if this flag has been declared, and if it has it will call the macro __declspec(dllexport) within the newly defined macro RELIANT_API. __declspec(dllexport) is a keyword specifier which means the function and member data variables used within the DLL be able to be exported to the executable application.

    After the macro conditions, there is only one function been defined, SimpleString(), shown as a prototype in the header code. As you can see it uses the RELIANT_API macro, which as was explained earlier defines if the function should be exported or allow a function to be imported. So now we will work on the interface.c source code file which will also be located in the DLLModule project.

    #define RELIANT_EXPORTS
    #include "interface.h"

    interface.c begins with the RELIANT_EXPORTS macro being defined. This as mentioned earlier means that we will be exporting the functions that will be used in this tutorial program. As we are only using one function for this example; you can use as many functions with this setting as you want, as long as you pre-define your functions in your header file.

    void SimpleString()
    {
    	printf("This is a string written in the DLL Module\n");
    	printf("Which is called at Program Load Time\n");
    	printf("Press Enter Key to Exit");
    	fflush(stdin);
    	getchar();
    }

    The function in use SimpleString(), is self-explanatory in what it does. There are no WinAPI functions inside this code, as they are not necessary. It just prints a message and exits the program once you press the Enter key. So now the code for the DLLModule is now done, we will move to the MainApplication project. So inside the MainApplication, we will work on the main.c source file.

    #include "interface.h"
    
    int main()
    {
    	// CALLING THIS FUNCTION STRAIGHT FROM THE DLL FILE
    	SimpleString();
    	return 0;
    }

    With the main.c file, you can see it calls the SimpleString() function from the DLLModule DLL file. Before the program runs, it will load the DLL file as it will initialise at the same time as the program. While you are compiling and building the code despite the fact you have linked dependencies and projects; the MainApplication program is still unaware of the location of the DLL file during the execution of the program. So make sure that you link the MainApplication to the Reference (like image below) whilst the program is loading the DLL at load time or runtime. To do this you go into the MainApplication menu in the Solution Explorer and select References (or Properties) > Common Properties > Frameworks and Properties. And at the bottom of the main references pane, click on Add New Reference, then simply click on the project DLL you want to link, which of course is DLLModule.

    If the DLL file is missing, then you will get a error prompt (see below). While you are running the program, make sure the DLLModule.dll file is situated in the same folder as MainApplication.exe.

    When the program runs, it uses the DLLModule. Once the program closes, the DLL module will also be removed in memory.

    Summary

    So there you have it; Creating and loading a DLL is that simple. The written code was simple enough to follow for clarity so you know how get to the basic set up. In more sophisticated programs which deals with DLL files, you will be dealing with more complex code; so experiment with the code and build more functions so you can get more of an hang of how to handle DLL files. In the next tutorial, we will create a program which loads and unloads the DLL during runtime.

    #ifndef INTERFACE_H
    #define INTERFACE_H
    
    #include <windows.h>
    #include <stdio.h>
    
    #ifdef RELIANT_EXPORTS
    #define RELIANT_API __declspec(dllexport)
    #else
    #define RELIANT_API __declspec(dllimport)
    #endif
    
    RELIANT_API void SimpleString();
    
    #endif
    /*
            Creating and Using DLL File Part 1 - interface.c      Reliant Code
            Creating Functions in the DLL, which 
    		will be used within the Program
    */
    #define RELIANT_EXPORTS
    #include "interface.h"
    
    void SimpleString()
    {
    	printf("This is a string written in the DLL Module\n");
    	printf("Which is called at Program Load Time\n");
    	printf("Press Enter Key to Exit");
    	fflush(stdin);
    	getchar();
    }
    /*
            Creating and Using DLL File Part 1 - main.c      Reliant Code
            Creating and Using a DLL File During Load Time
    */
    
    #include "interface.h"
    
    int main()
    {
    	// CALLING THIS FUNCTION STRAIGHT FROM THE DLL FILE
    	SimpleString();
    	return 0;
    }
    blog comments powered by Disqus
    Download Files Here

    PDF Version Here

    Download this Article in PDF

    Feedback on Article

    Report Bugs, Errors or Send Feedback

    CreateDLLFile_MSVC2010

    Windows File - File Size 5.77 KB

    MD5 Hash Check : What is MD5?