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

How to load a dynamic link library (DLL) into a Microsoft Visual C++ 6.0 project

, 6 May 2004
Rate this:
Please Sign up or sign in to vote.
This article gives guidelines for loading a DLL into your project and accessing the services offered by the DLL.

Introduction

In this article, I will summarize the results of my investigation with respect to including a dynamic link library (DLL) into a Microsoft Visual C++ 6.0 project, and using services which are offered by the loaded DLL.

The purpose of this article is to explain in short, but explicitly, what you need to do in order to be able to use services included in a DLL (by services, I refer to any function, a class or a parameter exported into DLL). Three weeks of “DLL hell” I went through can be avoided by doing the steps described below.

Case 1: A DLL you are supplied with is provided by another application and has been generated with the help of OLE (COMM).

Example: I needed to access an external interface of Rose-RT, which has been provided by the developers of Rose-RT as RrtRes.dll in order to control Rose-RT from external application.

Steps to perform:

  • Create in Microsoft Visual C++ 6.0 an MFC application (.dll or .exe);
  • Go to the menu View, ClassWizard.
  • Select an option Add Class…, from a type library.
  • Browse to the location of your *.dll.
  • A window, which displays the content of your *.dll, will appear. Select all classes you want to include in your project (Ctrl+A – select all content, or Ctrl + mouse click – for selecting a specific set of classes).
  • Push the button Open. Header file *.h and *.cpp implementation file will be generated.
  • Close the ClassWizard.

Now, all services you selected for use in you project are available!

Case 2: A DLL you are supplied with has been generated as a MFC-shared DLL and it is to be used in a single-threaded code

Example: Suppose you are supplied with LoadMe.dll, services of which should be accessed, but which cannot be exported as mentioned in case 1.

//You need to declare types to point on classes/functions in LoadMe.dll
//Assume, you have a function in your LoadMe.dll with a name 
//EntryPoint, which takes two parameters of types int and const char *, 
//and is of type void. You need to create a new type as a 
//pointer to that function as it is shown below.

typedef void (*EntryPointfuncPtr)(int argc, const char * argv );  
 
//Declare an HINSTANCE and load the library dynamically. Don’t forget 
//to specify a correct path to the location of LoadMe.dll

HINSTANCE LoadME;
LoadMe = LoadLibrary("..\\enter a Path To Your Dll here\\LoadMe.dll");
 
// Check to see if the library was loaded successfully 
if (LoadMe != 0)
    printf("LoadMe library loaded!\n");
else
    printf("LoadMe library failed to load!\n");

//declare a variable of type pointer to EntryPoint function, a name of 
// which you will later use instead of EntryPoint
EntryPointfuncPtr LibMainEntryPoint;            

// GetProcAddress – is a function, which returns the address of the 
// specified exported dynamic-link library (DLL) function. After 
// performing this step you are allowed to use a variable 
// LibMainEntryPoint as an equivalent of the function exported in 
// LoadMe.dll. In other words, if you need to call 
// EntryPoint(int, const char *) function, you call it as 
// LibMainEntryPoint(int, const char *)

LibMainEntryPoint = (EntryPointfuncPtr)GetProcAddress(LoadMe,"entryPoint");

Now, you can access any of the services of LoadMe.dll. Good luck!

Case 3: A DLL you are supplied with has been generated as a MFC-shared DLL and it is to be used in a multithreaded code

Use AfxLoadLibrary() instead of LoadLibrary() mentioned in case 2.

When you are done, don't forget to free the memory and unload the library: FreeLibrary(LoadMe).

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

Nataliya Mulyar
Web Developer
Netherlands Netherlands
I have BSc and MSc in Automatics and Computer Control Systems. After graduation I followed a post-master program Software Technology. Until the end of august 2004 I was involved in 9-month graduation project at the Embedded Systems Institute (Netherlands), where I was busy with coupling of Matlab/Simulink and Rational Rose RealTime. From September I switched to a new domain, so now I work on the development of patterns for process-aware information systems. My hobbies are painting, chess and dynamic sport.

Comments and Discussions

 
GeneralMy vote of 4 PinmemberOshtri Deka7-Apr-11 22:30 
QuestionGetProcAddress Fail in VC++ App while using VB Dll PinmemberJothiMurugeswaran12-Nov-07 20:29 
QuestionNot Working PinmemberJothiMurugeswaran12-Nov-07 18:45 
GeneralThanks Pinmembernickdoe20-Oct-07 14:43 
QuestionHow to use VB6 DLL in VC++ Pinmembercall4e_friend11-Jul-07 3:21 
AnswerRe: How to use VB6 DLL in VC++ PinmemberNataliya Mulyar11-Jul-07 3:42 
QuestionRe: How to use VB6 DLL in VC++ [modified] Pinmembercall4e_friend11-Jul-07 20:39 
GeneralRe: How to use VB6 DLL in VC++ PinmemberJothiMurugeswaran12-Nov-07 18:52 
Generalspirit of freshness Pinmemberi_a_z9-Aug-06 1:01 
Generaltaliya,difference between loadlibrary and afxloadlibrary Pinmemberpsyclope13-Jun-06 19:15 
Generaltaliya,difference between loadlibrary and afxloadlibrary Pinmemberpsyclope13-Jun-06 19:13 
Generalhi nataliya mulyar iam new to DLL...please help me... Pinmemberthukaram.vh1-Apr-06 1:42 
GeneralRe: hi nataliya mulyar iam new to DLL...please help me... PinmemberNataliya Mulyar2-Apr-06 23:46 
Generalmemory issue Pinmemberrahulbkarn6-Jan-06 0:40 
GeneralLoading dll's as Rose Real Time Add-in PinmemberDaniel Lélis Baggio21-Dec-05 7:00 
GeneralBest Wishes ! Pinmemberprogman10-Nov-05 10:23 
GeneralGlobal variable is always NULL in my dlls PinmemberDuke_Matheffy19-Jul-05 20:38 
GeneralRe: Global variable is always NULL in my dlls PinsussAnonymous19-Jul-05 23:43 
GeneralRe: Global variable is always NULL in my dlls PinmemberDuke_Matheffy19-Jul-05 23:57 
GeneralRe: Global variable is always NULL in my dlls PinsussAnonymous3-Aug-05 21:22 
GeneralRe: Global variable is always NULL in my dlls PinmemberDuke_Matheffy4-Aug-05 0:53 
GeneralRe: Global variable is always NULL in my dlls PinmemberIndian_metallica21-May-07 22:50 
GeneralTapping on cards.dll Pinmembermbolanosd19-Jul-05 0:16 
GeneralRe: Tapping on cards.dll PinsussAnonymous19-Jul-05 23:32 
Generalexternal interface of Rose-RT PinmemberMehdi Abolghasemi18-Jul-05 4:32 
Dear Nataliya,
 
I read your article about "loading a dynamic link library (DLL) into a Microsoft Visual C++ 6.0 project". It is good and perfect.
But, I am working in Industrial automation field and have designed my real time application with ROSE-RT. Now i need to creat an interface in VC++ for controlling and calling some methods in ROSE_RT. Please help how can i do this
 

 
Mehdi Abolghasemi,

GeneralRe: external interface of Rose-RT PinsussAnonymous18-Jul-05 22:08 
GeneralRe: external interface of Rose-RT PinsussAnonymous18-Jul-05 23:16 
GeneralLoading DLL class explicitly Pinsussmsoto197924-Nov-04 14:39 
GeneralHappy Birthday ! Pinmemberprogman10-Nov-04 9:29 
GeneralLoading DLL Pinsusswhils2-Nov-04 20:13 
Generalusing Cobol Dll inside Visual C++ Pinmembersevim26-Oct-04 6:46 
GeneralRe: using Cobol Dll inside Visual C++ PinmemberNataliya Mulyar27-Oct-04 0:51 
Questionhow to prepare ? Pinmemberf214-Oct-04 6:43 
Generaldifferent ways to us a DLL Pinsusschuddy2-Jul-04 0:53 
GeneralRe: different ways to us a DLL PinmemberNataliya Mulyar2-Jul-04 1:00 
GeneralRe: different ways to us a DLL PinsussChuddy2-Jul-04 7:12 
GeneralRe: different ways to us a DLL PinmemberNataliya Mulyar4-Jul-04 20:42 
QuestionGood article but... ? Pinmemberd00_ape16-Jun-04 1:28 
AnswerRe: Good article but... ? PinmemberNataliya Mulyar16-Jun-04 1:32 
GeneralRe: Good article but... ? Pinmemberd00_ape16-Jun-04 1:56 
GeneralRe: Good article but... ? PinmemberNataliya Mulyar16-Jun-04 1:58 
GeneralRe: Good article but... ? Pinmemberd00_ape16-Jun-04 2:26 
GeneralRe: Good article but... ? PinmemberNataliya Mulyar16-Jun-04 2:28 
GeneralRe: Good article but... ? Pinmemberd00_ape16-Jun-04 2:39 
GeneralRe: Good article but... ? PinmemberNataliya Mulyar16-Jun-04 2:46 
GeneralRe: Good article but... ? Pinmemberd00_ape16-Jun-04 2:55 
GeneralRe: Good article but... ? PinmemberNataliya Mulyar16-Jun-04 3:01 
GeneralI am more interested in coupling MATLAB (Simulink) with a FEM software Pinmemberpredagabi10-Jun-04 17:07 
GeneralRe: I am more interested in coupling MATLAB (Simulink) with a FEM software PinmemberNataliya Mulyar10-Jun-04 20:47 
GeneralFunction with a object PinmemberLuu Khoa31-May-04 23:11 

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 | Mobile
Web01 | 2.8.141022.1 | Last Updated 7 May 2004
Article Copyright 2004 by Nataliya Mulyar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid