Click here to 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
raid_zh
23:20 27 Mar '07  
I Have A Dll And It's .h File,But Lose the .lib File .
How could I Use It?WTF

Thx For UR Answer.
AnswerRe: i lose .lib
Mahmood Komeily
13:30 30 Mar '07  
Dear raid_zh,
When you build a dll that write it in developer studio (Visual studio); plus a dll, automatically have buld a lib file.
Do you develope dll yourself?
GeneralRe: i lose .lib
raid_zh
0:00 12 Apr '07  
It didn't by myself ~~Laugh
GeneralDLL's
Huntedwabbit
19:57 22 Feb '05  
To help me in my learning. A DLL is a Dynamic Link Library. Now, can you tell me What it does and how it works?
GeneralRe: DLL's
Mahmood Komeily
10:14 13 Apr '05  
Well, a dll dose same actions that an exe file unless a dll can share but an exe can't. suppose an exe file have a member function named ControlRobotHand that control hand of robot. you can't use this member function in other program, you must write your own code; whenas if this member function have placed in a dll, you could using it. dlls are very beneficial. msdn have a lot of information about dll, i suggest you read msdn.
excuse for delay
Regards
GeneralHow do I use a dll that contains a dialog?
udiraz
22:58 20 Dec '05  
Hi,

is there a limitations ? I created a dialog in my dll and I on run time, dialog.DoModal() doesn't work.

I have no clue what is the problem.

I am using MFC static linking since my client does not use MFC, is there a problem with static linking?

Thanks in advance,

Udi Raz


GeneralHow do I use a dll that contains a dialog?
udiraz
23:02 20 Dec '05  
Hi,

is there a limitations ? I created a dialog in my dll and I on run time, dialog.DoModal() doesn't work.

I have no clue what is the problem.

I am using MFC static linking since my client does not use MFC, is there a problem with static linking?

Thanks in advance,

Udi Raz
GeneralThank You!!!!
Anonymous
1:28 13 Dec '04  
Dear Mr.Mehnood

First of all I would like to thank you for this wonderful help.I am a beginner and it was really very helpful for me.
I will highly appriciate if you could send me some information or a small sample project on implementing unicode in VC++ 6.0.

Once more thanks a lot
Keep it up.

Warm Regards
Syed Ali Sajjad Raza
GeneralRe: Thank You!!!!
Mahmood Komeily
19:12 20 Dec '04  
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...
Jigar Mehta
19:16 7 Oct '04  
Hye,
Thanks for giving such a good article... Your language is so simple, I suggest you to take hard topics and explain in your simple language... A suggestion from me to you is Templates, STL etc.

Mail me, if you put any new article about templates or STL..

Jigar Mehta
(jigarmehta@gatescorp.com)

Software Developer
Gates Information Systems
India
GeneralRe: A Good one 5 out of 5 from me...
Mahmood Komeily
0:37 9 Oct '04  
hi Jigar
very thanxSmile
Generalhow to process strings and return from DLL
acawhiz
16:48 5 Oct '04  
Hi do you have an idea how to process strings in dll's ?
For example I have a fucntion with a string parameter and it returns another string...

regards,
Andrew
GeneralRe: how to process strings and return from DLL
Jigar Mehta
19:24 7 Oct '04  
You can give char * as a parameter and return char * from the DLL function and receive it from the calling program, its that easy !!

Rose

Jigar Mehta
(jigarmehta@gatescorp.com)

Software Developer
Gates Information Systems
India
GeneralSalam
A. Riazi
11:24 11 Sep '04  
Nice to see another article from Iranian people.

Best regards,
A. Riazi

GeneralRe: Salam
Mahmood Komeily
11:43 11 Sep '04  
my dear friend, old too!
are you remember me? smartapp!
you gladen me with your message.
very thanx
sincerely
mahmoodSmileWink
GeneralRe: Salam
A. Riazi
12:02 11 Sep '04  
How are you? I remember you.

Nice to see you again.
Do you visit the "Your favourite MFC / C++ articles from August" page?

if not, please see and vote me for article: "System Dialogs".

A. Riazi Smile
Generalvery easy & best solution
antarish konam
10:49 11 Sep '04  
hello,
can you describe more, about implicit linking
what is .LIB file?
Generaladmirable!
homaioon sadeghi
10:29 11 Sep '04  
hi mahmood
it was open sesame for me.very thanksSmileSmile
GeneralNothing special
Alexander M.
15:35 10 Sep '04  
1. The title of the article is quite strange.
2. The article itself is not detailed at all.
3. The article is not really a big help for beginners because important information is missing.
4. Why are you using a MFC dll where it is absolutly not necessary? (and this in a beginner article)
5. The fact that you are using a MFC application in a beginners article for DLLs is quite strange. (a console app would be much easier to understand)
6. You should have mentioned exporting symbols via module definition (.def) file.
7. Source code comments are totally missing. (you have just thrown around with some pieces of information)

I'm sorry, but you should really revise your article!

Don't try it, just do it! Wink
GeneralRe: Nothing special
Mahmood Komeily
9:52 11 Sep '04  
1- title,changed.
2- it is clear,description of details convert it to complicated article.
3- while babies begin to walk,they can't understand running rules.it is sufficient.
4,5- we really use not console apps nowadays.it is aged.
6- Unfortunately, you dont read carefully.i mentioned, DLL creation is multi way
7- Article described here

GeneralRe: Nothing special
KevinHall
13:21 12 Sep '04  
Just because console apps "are aged", that doesn't mean people should use MFC DLLs. A regular DLL would suffice whether or not you used an MFC application or not.

Anyway, if you really follow the "aged" argument, I'll say MFC is aged and that people should be creating .NET applications now.

Personally, I agree with each and every one Alexander's statements. If this article is really built for beginners, then you should discuss the benefits and costs of using DLLs including some sample scenarios. If I teach you a concept, but don't help you understand why it is useful or possibly harmful based on the circumstances, then it does you very little good.
GeneralRe: Nothing special
Manish K. Agarwal
18:34 19 Sep '04  
It will be nice if you can include something about class export.

How to export a class ? What about base classes if we are exporting a derived class?

Manish


Sonork ID 100:25668                                                  Home Page
GeneralRe: Nothing special
Mahmood Komeily
14:04 21 Sep '04  
hi Manish,
it could be found in part 2
GeneralRe: Nothing special
Toasty0
5:14 20 Sep '04  
Mahmood Komeily wrote:
4,5- we really use not console apps nowadays.it is aged.

Still loads and loads of folk and services using console. Poke tongue

Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes
Toasty0.com
GeneralRe: Nothing special
Mahmood Komeily
14:11 21 Sep '04  
u can develop console app, this article isn't for u;P


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