Creating DLLs with Visual Studio

DLLs are an important part of the Windows Operating system that will allow a program to dynamically load and execute code. This means that you could build an executable one day, and still make updates without re-releasing it.

Creating a DLL isn’t really all that difficult, and it only takes a few steps. This guide is particular to C++ and Visual Studio, so I don’t know whether or not you could use the same process for other compilers.

The first step is to create the code that you want to use. You could create the class or the functions, it doesn’t really matter. Take a look at the following example:

class testClass
{
public:
void testingFunction();
};

Now, this is your standard C++ basic class (without any definitions for the class, this will not link properly if you attempt to use it). All that needs to be done to make this class ready to use with a DLL is to add the Microsoft code for exporting a function/clas with __declspec(dllexport). So this is what the result is…

#define DLL_EXPORT __declspec(dllexport)

class DLL_EXPORT testClass
{
public:
void testingFunction();
};

Now the only difference is that there is a macro that places the dll export call into the class. Now the great part about this method is that should you ever need to maintain a static library and a dynamic library, you’ll be able to not modify any code (except for the DLL_EXPORT macro).

All the code is now present and everything should work. When you build the DLL it should also automatically build a import library (a lib).

__declspec(dllexport)

Tags: , ,

Leave a Reply