Click here to Skip to main content
15,851,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I have created a simple MFC Application. And I also have created a new dll.

Now i want to link/create the object of the dll from the MFC application which I create.

How is this possible?

Steps to do this will be much appricated.

Thanks,
Skunkhead :)
Posted
Comments
[no name] 18-Mar-11 3:51am    
Do want to call function of dll of something else.
skunkhead 18-Mar-11 4:10am    
call function of the dll. your link on MFC>DLL think will helpfull.thanks :)
[no name] 26-Mar-11 4:13am    
Its Ok.

Santosh's answer is correct (expect for extern: it is not necessary if you provide the .h files from your dll project).

I will just explain a little bit more about statically linking the dll:

First you should declare your functions properly in your dll project. If this doesn't already exist in your project, I suggest you add a file (for example ImportExport.h) with the following defines:

C++
#ifdef YOURDLL_EXPORTS
#define YOURDLL_API __declspec(dllexport)
#else
#define YOURDLL_API __declspec(dllimport)
#endif


And go to your project properties, C/C++, Preprocessor, in the Preprocessor Definitions, add YOURDLL_EXPORTS to the list. Do that for all configurations.

Now inside your dll project, modify all your class and functions definitions using YOURDLL_API to export them (you need to include ImportExport.h:
class YOURDLL_API MyClass { ... };
YOURDLL_API void MyFunction();


The ImportExport.h technic is widely used and makes maintenance much easier.

After that, after compilation, 2 important files will be produced:
- the .dll file (needed at runtime)
- the .lib file (needed at compilation time by the application that will use the dll)

Make sure you have the .lib file before continuing. If you can't find it, check your project properties. You can find its path under, Linker, Advanced, Import Library.

After compilation, you can make sure all exports are OK with a very useful tool: http://www.dependencywalker.com/[^]
Use that tool to check that all your functions/classes are properly exported.

Now, to use the dll in your other projects:
- for coding, use the necessary .h files from your dll projects to code whatever you want
- DO NOT define YOURDLL_EXPORTS in your project, so the compile will import these functions automatically (so we don't need to declare the functions as extern).
- to make project compile, you need to give the .lib file to the compiler. Go to the projet properties, Linker, Input, and add your .lib file in the Additional Dependencies field. Don't forget to do that for all configurations (Debug, Release, ...).

To run the application, don't forget to copy the dll next to the final exe.
 
Share this answer
 
v3
Comments
Espen Harlinn 18-Mar-11 5:48am    
Very nice effort - my 5, Dependency Walker is a teriffic tool :)
Olivier Levrey 18-Mar-11 5:51am    
Yes it is!! Thanks.
[no name] 18-Mar-11 5:49am    
hey adding and including .h of dll in calling project sound greate
take my 5+
Olivier Levrey 18-Mar-11 5:51am    
Thank you santosh ;)
Albert Holguin 18-Mar-11 13:43pm    
very good explanation oliver! ...to add a little extra info, MFC provides macros for __declspec() but personally, I'd stay away from those...
First thing if you cerate function dll using
__declspec(dllimport)
then just define function extern and called the function in mfc project.
if you created function for dialog based dll appilcation then
read my answer
DLL Dialog[^]

and for more information about dll linking please read the information given
MFC .DLL [^]

EDIT:
As you say you want call function then
Its so simple if you used
SQL
__declspec(dllimport)

just define dll function extern and on command line of linker just provide dll .lib file name
in dll project
 
Share this answer
 
v2
Comments
Olivier Levrey 18-Mar-11 5:42am    
Voted 4 because although everything is correct, using extern is not the best way to use dlls.
Albert Holguin 18-Mar-11 13:39pm    
agree with oliver...
[no name] 18-Mar-11 5:47am    
Hey i want to more information about linking without extern can you provide its have saw my dll problwm questions

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900