Click here to Skip to main content
15,890,557 members
Articles / Desktop Programming / MFC
Article

Super Easy DLL

Rate me:
Please Sign up or sign in to vote.
3.00/5 (39 votes)
2 Dec 2004CPOL3 min read 145.7K   1.1K   31   36
How to create a DLL the easiest way! No need for LoadLibrary, GetProcAddress, or typedefs! Just link to the lib, include the header file, and start calling functions from your DLL! Super Easy!

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)


Written By
Web Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThank you. Pin
froma0toz922-Feb-10 18:10
froma0toz922-Feb-10 18:10 
QuestionLib File? Pin
Ian W20-Feb-08 5:50
Ian W20-Feb-08 5:50 
AnswerRe: Lib File? Pin
Greg Ellis6-Feb-09 10:27
Greg Ellis6-Feb-09 10:27 
Generalsimple dll Pin
With_problem15-Nov-06 19:50
With_problem15-Nov-06 19:50 
Questionpassing arrays and parameters between executables using DLL Pin
shabya21-Apr-06 1:29
shabya21-Apr-06 1:29 
GeneralAbout DLL files Pin
sruz26-Jan-06 17:40
sruz26-Jan-06 17:40 
GeneralRe: About DLL files Pin
DgMv4-Oct-06 11:14
DgMv4-Oct-06 11:14 
Generalperfect for old unix hacker Pin
chzwiz0077-Mar-05 10:51
chzwiz0077-Mar-05 10:51 
GeneralError Message Pin
lfp200013-Dec-04 23:41
lfp200013-Dec-04 23:41 
GeneralJust some notes ... Pin
Zac Howland7-Dec-04 12:37
Zac Howland7-Dec-04 12:37 
GeneralSo why not use... Pin
bikram singh6-Dec-04 22:40
bikram singh6-Dec-04 22:40 
GeneralRe: So why not use... Pin
Anonymous7-Dec-04 22:04
Anonymous7-Dec-04 22:04 
GeneralThanks. Pin
Mark F.5-Dec-04 13:16
Mark F.5-Dec-04 13:16 
GeneralDYNAMIC library Pin
Savostin2-Dec-04 20:31
Savostin2-Dec-04 20:31 
GeneralRe: DYNAMIC library Pin
Lars [Large] Werner2-Dec-04 23:48
professionalLars [Large] Werner2-Dec-04 23:48 
QuestionGreg Ellis? Pin
tjackson_12-Dec-04 9:13
tjackson_12-Dec-04 9:13 
AnswerRe: Greg Ellis? Pin
Greg Ellis2-Dec-04 9:20
Greg Ellis2-Dec-04 9:20 
AnswerRe: Greg Ellis? LOL!! Pin
Nitron3-Dec-04 2:47
Nitron3-Dec-04 2:47 
GeneralRe: Greg Ellis? LOL!! Pin
Greg Ellis3-Dec-04 4:37
Greg Ellis3-Dec-04 4:37 
GeneralROTFL Pin
Rolf Schaeuble2-Dec-04 9:07
Rolf Schaeuble2-Dec-04 9:07 
GeneralRe: ROTFL Pin
Kappy2-Dec-04 9:24
Kappy2-Dec-04 9:24 
GeneralRe: ROTFL Pin
Rolf Schaeuble2-Dec-04 9:46
Rolf Schaeuble2-Dec-04 9:46 
GeneralRe: ROTFL Pin
Kappy2-Dec-04 9:55
Kappy2-Dec-04 9:55 
GeneralRe: ROTFL Pin
Rolf Schaeuble2-Dec-04 10:43
Rolf Schaeuble2-Dec-04 10:43 
GeneralRe: ROTFL PinPopular
Greg Ellis2-Dec-04 13:04
Greg Ellis2-Dec-04 13:04 
To Rolf,

According to you, articles for beginners do not belong on the codeproject. I find that funny considering that there is a difficulty level combobox that has a category called "Beginner". That would suggest to me that there is a place for beginner articles on the codeproject.

You may not find the article useful since you are obviously not a beginner. Or are you? Why did you look at the article to begin with? I mean, it's labeled Super Easy DLL and categorized as a beginner article. If you are such an advanced programmer and dll's are such a peice of cake for you, then why would you even click the link?

The beginner articles on this site helped me immensely and I learned a lot from them when I first started programming. I owe a lot to this site and the people that post articles here so I try to give back whenever possible. I noticed you have not done the same since becoming a member in 2003. Since you are such an advanced programmer and this article is so beneath you, then why haven't you shared your vast knowledge with the rest of us? Don't give me the, "I'm too busy" pitch because you are obviously not too busy to trash someone's article that you claim to be a waste of your time. Which is ironic considering you wasted your time typing that garbage message of yours in the message forum.

Maybe instead of Rolling On The Floor Laughing, you should Get Up and Write a Peice of Code. It'll serve you better than copying and pasting from codeproject.

So you think I should write a sequel called 'how to turn on your pc?'. I think it would be more beneficial for everyone if I wrote an article called 'how to turn off your pc for dummies' dedicated in your name. *Hint* *Hint*!

Nothing annoys me more than RTFM people such as yourself. It's people like you that force these articles to be written in the first place. If I asked how to write a dll like this in a newsgroup you would be the first one to tell me to RTFM! Well that's not good enough. This article will probably save people from having to search through msdn or books as you suggest they do. Plus it gives them example code to work from.

P.S. Rolf, maybe you should write that article on how to delcare a variable. It looks like you need an idea to use for an article to submit.

To everyone else,

Sorry for the rant. Sometimes I just get really annoyed with people's comments, especially when the comments are from people who have contributed JACK S*** to the codeproject. Comments like these make me want to remove every article I have ever submitted to this site.

Cheers,
Greg

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

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