How to start using Dynamic-link libraries (DLL)

Published on

Last Updated on

Estimated Reading Time: 1 min

Since we will be using DLL's to have 1 single copy of our engine code, let's understand the basics of DLL's. For those of you who are on the fence about whether to use a DLL or not. Here are some of the advantages of using a dll as per MSDN

We will be using __declspec(dllexport) and __declspec(dllimport) to export and import functions, data and objects to and from a DLL. We will mainly be using it to export/import functions or interface declarations and not member variables.

I prefer to have a header file that can encapsulate the above function calls. I will explain it with an example.

#ifdef project_EXPORTS
#define project_API __declspec(dllexport)
#else
#define project_API __declspec(dllimport)
#endif

For the project from which we want to export functions, project_EXPORTS macro will be defined so that project_API automatically map to __declspec(dllexport). For all other projects, project_API will map to __declspec(dllimport)

Now for whichever interface we need to export all/the functions, all we need to do is include the above header and add the following code.

class project_API className
{
};

This takes care of exporting/importing all the functions, member variables in the class className.