Dynamically loading a class from a dll.

By: Vincent Drake

Abstract: Includes a short explaination and simple example of using virtual base classes as interfaces across an exe and a run-time loaded dll.

Using a class from a dll is easy when the dll is statically (or load-time) linked to your executable. The compiler gets the information it needs from the dll's header and the linker gets all information it needs from the import library. Periodically, someone posts to our newsgroups asking how to newsgroups load a class from a dll completely at run-time. The following is a simple answer to this question, using some of the basic techniques behind COM and CORBA.

Create the class dll:

  1. Create a virtual base class that contains all of the methods you would need to call from the class. This will be the interface your dll class will support.
  2. Make the dll class derive from that interface.
  3. Include the interface definition in the executable that will use the dll class.
  4. Export a function from the dll that will create a new instance of the dll class and return it's address (I will call this function CreateClassInstance()).

To use the class in your executable:

  1. Call LoadLibrary() on the dll that contains the class.
  2. Call GetProcAddress() to gain access to the CreateClassInstance() function.
  3. Call CreateClassObject() and store the returned address in an interface pointer.

Make calls to the object using the interface pointer and these calls will foreward to the derived object. Don't forget to delete the pointer when done with the object.

Depending on the complexity of the applicaiton, you may have to deal with reference counting issues to determine when to delete the pointer you received from the dll. This example does not take these considerations into account.



This example consists of two projects and six source files:
	header files:
	"FooInterface.h" -- definition for IFoo, the base class/interface
	"FooClass.h" -- definition for FooClass, deriving from IFoo
	"DllExports.h" -- dll's exported functions

	dll project:
	"DllMain.cpp" -- main cpp file for the dll project
	"FooClass.cpp" -- contains implementation for FooClass

	exe project:
	"ExeMain.cpp" -- main cpp file for exe project	


//-------- FooInterface.h --------//

class IFoo
	int GetNumber() = 0;
	void SetNumber( int & ) =0;


//-------- FooClass.h --------//
#ifndef FOOCLASS_H
#define FOOCLASS_H

#include "FooInterface.h"

class FooClass :public IFoo
	const int& GetNumber();
	void SetNumber( int & );
	int number;

#endif 	// FOOCLASS_H

//-------- FooClass.cpp --------//

	number = 0;

int FooClass::GetNumber()
	return number;

void FooClass::SetNumber(int &arg)
	number = arg;

//-------- DllExports.h --------//

#ifdef __dll__
#define IMPEXP __declspec(dllexport)
#define IMPEXP __declspec(dllimport)
#endif 	// __dll__

#include "FooClass.h"

extern "C"
void* IMPEXP CreateFooClassInstance();

#endif	// DLLEXPORTS_H

//-------- DllMain.cpp --------//
#define __dll__
#include "DllExports.h"

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
        return 1;

void* IMPEXP CreateFooClassInstance();
	return static_cast< void* > (new FooClass);

//-------- ExeMain.cpp --------//
#include "FooInterface.h"
#include <windows.h>
#include <iostream.h>
#include <conio.h>

int main(int argc, char* argv[])
	IFoo* piFoo = NULL;
	typedef void* (*pvFunctv)();
	pvFunctv CreateFoo;

	hdll = LoadLibrary("DllMain.dll");		// load the dll
	CreateFoo = static_cast < fpFunctv> (		// get the function pointer
		GetProcAddress( hdll, "_CreateFooClassInstance" ) );

	piFoo = static_cast< piFoo* > ( CreateFoo() );	// get pointer to object

	piFoo->SetNumber(8);				// start using the object
	cout 	<< "Foo::number is epual to: "
		<< piFoo-> GetNumber()
		<< endl;

	cout	<< "Enter new value for Foo::number: ";
	int temp;
	cin	>> temp;
	piFoo-> SetNumber(temp);
	cout	<< "Foo::number is now: "
		<< piFoo->GetNumber()
		<< endl;
	delete piFoo = NULL;				// delete the object
	FreeLibrary(hdll);				// free the dll

Server Response from: ETNASC03