Hi, I was recently trying to produce a very simple DLL library under Visual Studio 2013. However I was never doing it before and thus I had some bugs in my code,which I would like to post about. At first the .DLL consists of 3 files total:

  • .dll + .lib + .h

So if you are about to create a DLL,make sure you have all 3 files. The first 2 (.dll and .lib) are the output from the Visual Studio Project after you build your solution (Make sure you are using the “Release” build). Now  onto .h (header). When you are creating a project in VS,start by creating an empty project (Just recommendation) and by choosing C++ –> Win32 –> Console Application –> DLL. After that,Add 2 files to your project: Header.h and Source.cpp. Now comes the funny part.

All of your classes and functions needs to have a definition here in Header.h. Please note the word definition. Not declaration. Lets take a look at some basic implementation:

Note that I am defining all functions as __declspec(dllexport). This is very important itself,but whats more,both of my functions are just defined. Which translates into: “I have these 2 functions with this input paramaters” Note that there is no declaration of the function (What they are doing). The same apply for my “Geforce” struct,which is just defined in the code. There is no variable associated with the struct,so I cant access any values (chip/speed/vram/name). Now lets see what kind of code to put into our Source.cpp. As you may be thinking about it right now, we will declare what these 2 functions do and add a variable for our created struct:

Its a very simple project made for my own demonstration purposes,but I believe somebody might find it useful. At first I am using the same namespace MSI. Then I point at my Functions (nVidia::Init & nVidia::print()) and declare what they are doing. Also I declare a variable GTX, that will hold all of the info needed for my Library and for my 2 functions. Once the variable GTX is declared,I can access it in my functions as GTX.something. Thats pretty much all about DLL library.

  • There is a nice post on MSDN about creating a DLL,however I had some mess with definition X declaration,so I was just trying to make it clear what both of these terms mean.

Once everything is done, press build, copy your Library.dll to your debug/release/windows directory, include your Header.h in the project where you would like to use the library and dont forget to tell the linker,that it needs to use your Library.lib as well (Tell him where your Library.lib is located and also add it to “Additional dependencies” ) !

*Please note that the Struct is defined as “static”,which makes it a global variable. This translates into: “Lets create 2 objects O1 and O2 from our Library and adding some values to O1.” The result is that both O1 and O2 now have the same values regardless of wheather we have Initialized O1 or O2 with the values. To solve this (if you need),use the “private” definition.

Share This