Click here to Skip to main content
15,914,222 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Browser Information Callers Graph Pin
palbano29-Aug-03 5:44
palbano29-Aug-03 5:44 
Generalproblem with a dll Pin
Anonymous28-Aug-03 8:18
Anonymous28-Aug-03 8:18 
GeneralRe: problem with a dll Pin
User 665828-Aug-03 8:36
User 665828-Aug-03 8:36 
GeneralRe: problem with a dll Pin
David Crow28-Aug-03 10:23
David Crow28-Aug-03 10:23 
Generalsolution! Pin
Alexander M.,28-Aug-03 11:00
Alexander M.,28-Aug-03 11:00 
GeneralRe: solution! Pin
Anonymous28-Aug-03 11:30
Anonymous28-Aug-03 11:30 
GeneralRe: solution! Pin
Alexander M.,28-Aug-03 12:42
Alexander M.,28-Aug-03 12:42 
GeneralRe: solution! Pin
Antti Keskinen29-Aug-03 9:13
Antti Keskinen29-Aug-03 9:13 
Yes there is..

In fact, I pondered over this same problem a while back. The idea was that I was supposed to use a class which is completely declared inside a DLL. I only had the header files for reference.

So, here we go, first the actual class, declared with typedef. Do this in a seperate header file:

[code]

typedef class {

public:
// Some initialization functions ?
virtual HRESULT InitModule(void);
virtual HRESULT QuitModule(void);

// A member variable perhaps ?
int iMemberVariable;

} MyExportedClassType;

// Pointer-to-function returning address-of WindowPlugin class
typedef MyExportedClassType* (*GetClass)(void);

[/code]

What ? A pointer-to-function blablabla ? What's that ?
Simple: That is the key to this whole mess: a pointer to a function. Why is it declared there ? Because the same header file is used in the loading application. Just for the sake of simplicity.

Now, when we have the actual class, let's see the DLL exporting it:

[code]

// On global level, declare an instance of the class

MyExportedClassType MyClass;

// At the DllMain, initialize the class' members

int WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpVoid)
{
.... Some code here

MyClass.iMemberVariable = 10;

.... More code here

return TRUE

}

// Add an exported function, which returns the address of our class

__declspec(dllexport) MyExportedClassType* ProvideAddressOfClass(void)
{
return &MyClass;
}

[/code]

Lastly, create a module definition file

[code]

LIBRARY
EXPORTS
ProvideAddressOfClass @1

[/code]

Ok, now the DLL is ready, go compile it. Next up, the loading application:

[code]

// We must include the typedef declaration
#include "myclasstypedef.h"

HDLL hDll;

// See here: let's create a pointer-to-function which returns a class' address.. Remember, we exported this type of function from the DLL !

GetClass MyClassProvider;

// A pointer to our class type
MyExportedClassType* lpImportedClass;

// Your main function
int main(void)
{

.... Some code ?

// Load module
hDll = LoadLibrary(ModuleName[iCount]);

// Resolve address of exported function
MyClassProvider = (GetClass)GetProcAddress(hDLL, "ProvideAddress");

// Get address of class within the DLL, using the exported function
lpImportedClass = MyClassProvider();

// Let's use it !
lpImportedClass->iMemberVariable = 1;

.... Some more code

// Remember to unload the library !
UnloadLibrary(hDll);

// End application
return 0;
}

[/code]

Seems difficult ? Well, it really isn't.. To sum it all up programmer-wise:

1. Create a class using typedef
2. Create a function which returns the address of your class
3. Export this function in the DLL with __declspec AND .def file
4. Create a typedef for 'pointer-to-function' of the address-provider function

5. Load the DLL in another application
6. Get the address of the exported function
7. Use the function to get the address of the class
8. Use the class

9. NULLify pointers, unload the DLL etc clean-up work
10. Quit and get some sleep Smile | :)

I have created an example of this, an application which actually creates it's main window in a seperate DLL, and adds event handlers to this window's procedure in another DLL. You can download the entire workspace (3 projects: WinDLL, InpDLL, WinLoader) from here.

Enjoy !

-Antti
GeneralRepositioning cursor in an edit box Pin
Vikram A Punathambekar28-Aug-03 8:14
Vikram A Punathambekar28-Aug-03 8:14 
GeneralRe: Repositioning cursor in an edit box Pin
User 665828-Aug-03 8:41
User 665828-Aug-03 8:41 
GeneralRe: Repositioning cursor in an edit box Pin
Vikram A Punathambekar28-Aug-03 8:59
Vikram A Punathambekar28-Aug-03 8:59 
GeneralRe: Repositioning cursor in an edit box Pin
David Crow28-Aug-03 9:11
David Crow28-Aug-03 9:11 
GeneralRe: Repositioning cursor in an edit box Pin
User 665828-Aug-03 9:31
User 665828-Aug-03 9:31 
GeneralRe: Repositioning cursor in an edit box Pin
David Crow28-Aug-03 8:45
David Crow28-Aug-03 8:45 
GeneralRe: Repositioning cursor in an edit box Pin
Vikram A Punathambekar28-Aug-03 9:04
Vikram A Punathambekar28-Aug-03 9:04 
GeneralCOleSafeArray::PutElement Pin
act_x28-Aug-03 6:27
act_x28-Aug-03 6:27 
GeneralRe: COleSafeArray::PutElement Pin
Steve S29-Aug-03 2:18
Steve S29-Aug-03 2:18 
GeneralCreating CRichEditCtrl Version 2.0/3.0 Pin
Larry J. Siddens28-Aug-03 6:03
Larry J. Siddens28-Aug-03 6:03 
GeneralRe: Creating CRichEditCtrl Version 2.0/3.0 Pin
David Crow28-Aug-03 7:02
David Crow28-Aug-03 7:02 
GeneralRe: Creating CRichEditCtrl Version 2.0/3.0 Pin
Steve S28-Aug-03 7:08
Steve S28-Aug-03 7:08 
GeneralRe: Creating CRichEditCtrl Version 2.0/3.0 Pin
Larry J. Siddens28-Aug-03 7:41
Larry J. Siddens28-Aug-03 7:41 
GeneralRe: Creating CRichEditCtrl Version 2.0/3.0 Pin
David Crow28-Aug-03 8:00
David Crow28-Aug-03 8:00 
GeneralRe: Creating CRichEditCtrl Version 2.0/3.0 Pin
Larry J. Siddens28-Aug-03 8:06
Larry J. Siddens28-Aug-03 8:06 
GeneralRe: Creating CRichEditCtrl Version 2.0/3.0 Pin
David Crow28-Aug-03 8:41
David Crow28-Aug-03 8:41 
GeneralRe: Creating CRichEditCtrl Version 2.0/3.0 Pin
Larry J. Siddens28-Aug-03 8:44
Larry J. Siddens28-Aug-03 8:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.