Introduction
I saw many articles on CodeProject on using DLLs the easiest way, but none of them seemed very easy to me. All of them had some weird function that did all the GetProcAddress
and LoadLibrary
stuff. I didn't want that, I just wanted to make a DLL, link to it, include a header, and start calling functions from anywhere within my project. Finally, I found out how to do this.
Creating the Super Easy DLL
First, you just open Visual Studio and click File... New... and select Win32 Dynamic-Link Library. Then, when the next screen comes up, select A Simple DLL project and click Finish.
Now you have your base DLL project setup. Now, in order for us to be able to use our DLL functions in other projects, we must export them. So, first we have to add a .def file. Click File... New... and then select Text File and give it the name EasyDLL.def. Now, we are only going to add one function to our DLL for simplicity. So double click the EasyDLL.def file in your project and add the following text:
LIBRARY "EASYDLL.dll"
EXPORTS
EASYDLL_GetVersion
Next, you will need to double click the EasyDLL.cpp file and remove the following code:
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
Now, we should have a blank EasyDLL.cpp file except for the comment and the #include "stdafx.h"
line. So now, we add a new header file by clicking File... New... and selecting C/C++ Header File, and give it the name EasyDLL.h and click OK. Now, double click EasyDLL.h file and add the following code:
#ifdef __cplusplus
extern "C" {
#endif
#define WINEXPORT WINAPI
int WINEXPORT EASYDLL_GetVersion();
#ifdef __cplusplus
}
#endif
Now, we need to double click EasyDLL.cpp and add the following code:
#include "EasyDLL.h"
int WINEXPORT EASYDLL_GetVersion()
{
return 1;
}
Now your DLL is ready to go! All you have to do is build your project.
Using the Super Easy DLL
This is where the "Super Easy" part comes in. All you have to do to use your DLL now is link to your .lib file by going to Project.... Settings... and the Link tab. Add in EasyDLL.lib into the Object/Library modules text box. Then, just include EasyDLL.h wherever you want to use the functions from the DLL, and then you can just start calling functions.
#include <iostream.h>
#include <windows.h>
#include "EasyDLL.h"
int main()
{
int nVersion = EASYDLL_GetVersion();
return 0;
}
We need to include windows.h because of the WINAPI macro in the EasyDLL.h file.
Points of Interest
- The way of creating a DLL presented here removes the need to use
LoadLibrary
, GetProcAddress
and doing all those typedef
s every time you want to use a function.
- You can save space in your DLL by clicking on Project... Settings... and select Use MFC in a shared DLL instead of Not Using MFC.
- You can include your EasyDLL.h file in your StdAfx.h file of your MFC project, and you will be able to use the function calls from your DLL anywhere in your project.
Conclusion
I hope this helped some of you out. I know it makes it much easier for me to create a re-usable library of functions without having to worry about all those LoadLibrary
and GetProcAddress
calls. Not to mention I can just include the header file in the StdAfx.h file of my project and use the DLL functions anywhere in my project.
History
- December 2nd, 2004 - First version of EasyDLL uploaded to CodeProject.