Click here to Skip to main content
Click here to Skip to main content

Super Easy DLL

By , 2 Dec 2004
Rate this:
Please Sign up or sign in to vote.

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 typedefs 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Greg Ellis
Web Developer www.clubdjpro.com
Canada Canada
I am the co-founder of Cube Software Solutions Inc. and ClubDJ Pro DJ Software.
 
DJ Software - www.clubdjpro.com
Company Website - Web design edmonton

Comments and Discussions

 
GeneralThank you. Pinmemberfroma0toz922-Feb-10 18:10 
QuestionLib File? PinmemberIan W20-Feb-08 5:50 
AnswerRe: Lib File? PinmemberGreg Ellis6-Feb-09 10:27 
Generalsimple dll Pinmemberabrakadbra15-Nov-06 19:50 
Questionpassing arrays and parameters between executables using DLL Pinmembershabya21-Apr-06 1:29 
GeneralAbout DLL files Pinmembersruz26-Jan-06 17:40 
GeneralRe: About DLL files PinmemberDgMv4-Oct-06 11:14 
Generalperfect for old unix hacker Pinmemberjimmyjack7-Mar-05 10:51 
GeneralError Message Pinmemberlfp200013-Dec-04 23:41 
GeneralJust some notes ... PinmemberZac Howland7-Dec-04 12:37 
The method you use has a few downsides that should be mentioned (especially in a beginners' article):
 
1) You cannot export objects this way, only functions. Lets say you create your own dynamic array class and want to put it in a library. Using the method described in this article is not for you (the MFC extended DLL method would be more useful)
 
2) By linking to the dll, you are making it a static dll. That is, the dll is loaded at when the program first starts (even if no features that use it have been loaded yet). So, if the dll does not exist or is in the wrong location, you will get an error message and will not be able to run the program until you get the DLL in the right place. Using the LoadLibrary/GetProcAddress functions allows you to run the program and attempt to load the libraries yourself so you can catch the errors and decide if the program can still be run (perhaps without the use of some features).
 
3) For many third-party DLLs (and even some system DLLs), you will not have access to the header files for them, so you will need to use the LoadLibrary/GetProcAddress method (so you should learn how to do that).
 
4) If you are creating your own DLLs and intend to do this, many times you would be better off creating a static library (which will compile to a .lib file and will link into whatever projects you need it in) instead. You will have dupilicate code if you use it in many projects, but you will also avoid what is affectionately known as "DLL HELL" should you need to revise the DLL.
 
"If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week"
GeneralSo why not use... Pinmemberbikram singh6-Dec-04 22:40 
GeneralRe: So why not use... PinsussAnonymous7-Dec-04 22:04 
GeneralThanks. PinmemberMark F.5-Dec-04 13:16 
GeneralDYNAMIC library PinsussSavostin2-Dec-04 20:31 
GeneralRe: DYNAMIC library PinmemberLars [Large] Werner2-Dec-04 23:48 
QuestionGreg Ellis? Pinmembertjackson_12-Dec-04 9:13 
AnswerRe: Greg Ellis? PinmemberGreg Ellis2-Dec-04 9:20 
AnswerRe: Greg Ellis? LOL!! PinmemberNitron3-Dec-04 2:47 
GeneralRe: Greg Ellis? LOL!! PinmemberGreg Ellis3-Dec-04 4:37 
GeneralROTFL PinmemberRolf Schaeuble2-Dec-04 9:07 
GeneralRe: ROTFL PinmemberKappy2-Dec-04 9:24 
GeneralRe: ROTFL PinmemberRolf Schaeuble2-Dec-04 9:46 
GeneralRe: ROTFL PinmemberKappy2-Dec-04 9:55 
GeneralRe: ROTFL PinmemberRolf Schaeuble2-Dec-04 10:43 
GeneralRe: ROTFL PinmemberGreg Ellis2-Dec-04 13:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140421.2 | Last Updated 2 Dec 2004
Article Copyright 2004 by Greg Ellis
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid