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

Dlls are Simple. Part 1

, 9 Oct 2004
Rate this:
Please Sign up or sign in to vote.
How to create DLLs and use them.

Sample screenshot

Introduction

Some programmers think DLLs are complicated but it's true they are like EXEs. However I have seen programmers who can't build DLL. There are multiple way to create a DLL. For usage with exe, easiest way is "Implicit linking". In this way, we require .LIB file produced by DevStudio. Below, we create a DLL and then use it with exe. Inside DLL, we calculate (multiply) two numbers that has been send from the exe. Practically, exe is "Sender/Receiver" and DLL is "Calculator".

How to build MFC DLL?

  1. Run VC++.
  2. Choose : File > New.
  3. Create "MFC AppWizard (DLL)" (named e.g. : MyFirstDll).
  4. Declare in top of file:
    //
    #define DLLEXPORT __declspec(dllexport)
    //
    

    __declspec(dllexport)'s purpose is to add the " export directive " to the object file. So you don't need a .DEF file. To make code more readable, define a macro for __declspec(dllexport) : DLLEXPORT.

  5. Now for exporting per function that you want, type before it : "DLLEXPORT" like this:
    DLLEXPORT int Multiply(int ParOne,int ParTwo) 
    { 
       int Mlt=ParOne*ParTwo; 
       return Mlt; 
    } 
  6. Press Build button.
  7. Bring out DLL from oven!!

    Note that linker also builds an " import library " with the same DLL name but with .lib extension.

How to use MFC DLL?

  1. Run VC++.
  2. Choose : File > New.
  3. Create "MFC AppWizard (exe)".
  4. Choose "Dialog based".
  5. Choose : Project > Add To Project > New > C/C++ Header File.
  6. Name file e.g. : Imports.
  7. Declare in Imports.h:
    //
    #define DLLIMPORT __declspec(dllimport) 
    //

    __declspec(dllimport)'s purpose is to add the " import directive " to the object file. To make code more readable, define a macro for __declspec(dllimport) : DLLIMPORT.

  8. Type after it:
    DLLIMPORT int Multiply(int ParOne,int ParTwo);

    This is the same function that you defined in DLL. Here, it has been introduced as an "import function". But it is unrecognized for current project and we must resolve it for linker.

  9. Copy .lib file from release or debug folder (depends current project setting) in previous project to current directory project because it must link with exe. Inside .lib file (same import library), exists information about exported functions of DLL.
  10. Back to VC++ environment and Choose : Project > Settings > Link (tab).
  11. Type [.lib file previous project] in "Object/library modules:". For example, MyFirstDll.lib. Then press OK. It will resolve externals. Here, "int Multiply(int ParOne,int ParTwo);" function.
  12. Because we intend to use function in ...Dlg.cpp file (e.g. SimpleDllDlg.cpp), type in top of file:
    //
    #include "Imports.h"
    //
  13. Now you can use exported function of DLL like this:
    void CDllFunDlg::OnMltply() 
    { 
       UpdateData(TRUE); 
       m_Result=Multiply(m_ParamOne,m_ParamTwo); 
       UpdateData(FALSE); 
    }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Mahmoud Komeily
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
I born in tehran at 1975
and began programming with commodore 64
I established "Pishro Narmafzar Iran" Corporation in 2001.
i'm expert in VC++, VC#, MS SQL Server, ASP .NET & have developed :
1-Persian Photoshop (fully localized)
2-Persian Freehand (fully localized)
3-Pardis (persian/arabic typing in graphical and video editing programs include : Ulead video studio,Ulead media studio,Pinnacle studio, Premiere, Flash, Freehand, 3D Max, Auto CAD, Photoshop, CorelDraw, Ulead Cool 3D,...)
Pardis is only persian/arabic typing tool for Ulead Video Studio in world.
Mahmoud komeily, mahmood komeili, محمود کمیلی

Comments and Discussions

 
Questioni lose .lib Pinmemberraid_zh27-Mar-07 23:20 
AnswerRe: i lose .lib PinmemberMahmood Komeily30-Mar-07 13:30 
GeneralRe: i lose .lib Pinmemberraid_zh12-Apr-07 0:00 
GeneralDLL's PinmemberHuntedwabbit22-Feb-05 19:57 
GeneralRe: DLL's PinmemberMahmood Komeily13-Apr-05 10:14 
GeneralHow do I use a dll that contains a dialog? Pinmemberudiraz20-Dec-05 22:58 
GeneralHow do I use a dll that contains a dialog? Pinmemberudiraz20-Dec-05 23:02 
GeneralThank You!!!! PinsussAnonymous13-Dec-04 1:28 
GeneralRe: Thank You!!!! PinmemberMahmood Komeily20-Dec-04 19:12 
Dear friend,Syed Ali
excuse me for delay,i'm very busy for distributing Pardis.
there are ways for using unicode features but it is difficult,complicated and havent warranty.it is better using VC++ 7.0 accompany .NET programming.
describe details if possible, send to director@pishro-narmafzar.com
i will glad, if you accept Pardis as gift.
Very thanks,sincerely;
mahmoodSmile | :)
GeneralA Good one 5 out of 5 from me... PinmemberJigar Mehta7-Oct-04 19:16 
GeneralRe: A Good one 5 out of 5 from me... PinmemberMahmood Komeily9-Oct-04 0:37 
Questionhow to process strings and return from DLL Pinmemberacawhiz5-Oct-04 16:48 
AnswerRe: how to process strings and return from DLL PinmemberJigar Mehta7-Oct-04 19:24 
GeneralSalam PinmemberA. Riazi11-Sep-04 11:24 
GeneralRe: Salam PinmemberMahmood Komeily11-Sep-04 11:43 
GeneralRe: Salam PinmemberA. Riazi11-Sep-04 12:02 
Generalvery easy & best solution Pinmemberantarish konam11-Sep-04 10:49 
Generaladmirable! Pinmemberhomaioon sadeghi11-Sep-04 10:29 
GeneralNothing special PinmemberAlexander M.10-Sep-04 15:35 
GeneralRe: Nothing special PinmemberMahmood Komeily11-Sep-04 9:52 
GeneralRe: Nothing special PinmemberKevinHall12-Sep-04 13:21 
GeneralRe: Nothing special PinmemberManish K. Agarwal19-Sep-04 18:34 
GeneralRe: Nothing special PinmemberMahmood Komeily21-Sep-04 14:04 
GeneralRe: Nothing special PinmemberToasty020-Sep-04 5:14 
GeneralRe: Nothing special PinmemberMahmood Komeily21-Sep-04 14:11 
GeneralRe: Nothing special Pinmembercristian anders21-Sep-04 14:38 
GeneralRe: Nothing special Pinmemberkasahi yamuma11-Sep-04 10:02 
GeneralRe: Nothing special Pinmemberkdkd23-Sep-04 5:13 
GeneralRe: Nothing special PinmemberJigar Mehta7-Oct-04 19:32 
GeneralRe: Nothing special Pinmemberquangtin31-Nov-05 16:57 
Questionnice,please help? Pinmembereric naftonas10-Sep-04 14:22 
AnswerRe: nice,please help? PinmemberA. Riazi11-Sep-04 11:21 
GeneralRe: nice,please help? PinmemberJigar Mehta7-Oct-04 19:40 
Generalsimple and efficient Pinmemberothumo sabura10-Sep-04 13:34 
GeneralYeah! are fun Pinmemberrasen wiliams10-Sep-04 13:17 
GeneralRe: Yeah! are fun PinmemberTomKat11-Sep-04 4:21 
GeneralRe: Yeah! are fun PinmemberA. Riazi11-Sep-04 11:23 
GeneralRe: Yeah! are fun PinmemberJigar Mehta7-Oct-04 19:43 
GeneralRe: Yeah! are fun PinmemberRabidCow11-Sep-04 22:53 
GeneralOOH! very good Pinmemberrambod soraty10-Sep-04 13:09 

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 | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 9 Oct 2004
Article Copyright 2004 by Mahmoud Komeily
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid