Skip to main content
Email Password   helpLost your password?

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); 
    }
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Questioni lose .lib Pin
raid_zh
23:20 27 Mar '07  
AnswerRe: i lose .lib Pin
Mahmood Komeily
13:30 30 Mar '07  
GeneralRe: i lose .lib Pin
raid_zh
0:00 12 Apr '07  
GeneralDLL's Pin
Huntedwabbit
19:57 22 Feb '05  
GeneralRe: DLL's Pin
Mahmood Komeily
10:14 13 Apr '05  
GeneralHow do I use a dll that contains a dialog? Pin
udiraz
22:58 20 Dec '05  
GeneralHow do I use a dll that contains a dialog? Pin
udiraz
23:02 20 Dec '05  
GeneralThank You!!!! Pin
Anonymous
1:28 13 Dec '04  
GeneralRe: Thank You!!!! Pin
Mahmood Komeily
19:12 20 Dec '04  
GeneralA Good one 5 out of 5 from me... Pin
Jigar Mehta
19:16 7 Oct '04  
GeneralRe: A Good one 5 out of 5 from me... Pin
Mahmood Komeily
0:37 9 Oct '04  
Generalhow to process strings and return from DLL Pin
acawhiz
16:48 5 Oct '04  
GeneralRe: how to process strings and return from DLL Pin
Jigar Mehta
19:24 7 Oct '04  
GeneralSalam Pin
A. Riazi
11:24 11 Sep '04  
GeneralRe: Salam Pin
Mahmood Komeily
11:43 11 Sep '04  
GeneralRe: Salam Pin
A. Riazi
12:02 11 Sep '04  
Generalvery easy & best solution Pin
antarish konam
10:49 11 Sep '04  
Generaladmirable! Pin
homaioon sadeghi
10:29 11 Sep '04  
GeneralNothing special Pin
Alexander M.
15:35 10 Sep '04  
GeneralRe: Nothing special Pin
Mahmood Komeily
9:52 11 Sep '04  
GeneralRe: Nothing special Pin
KevinHall
13:21 12 Sep '04  
GeneralRe: Nothing special Pin
Manish K. Agarwal
18:34 19 Sep '04  
GeneralRe: Nothing special Pin
Mahmood Komeily
14:04 21 Sep '04  
GeneralRe: Nothing special Pin
Toasty0
5:14 20 Sep '04  
GeneralRe: Nothing special Pin
Mahmood Komeily
14:11 21 Sep '04  


Last Updated 9 Oct 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009