Skip to main content
gamedev.net
🔒 Locked

Polymorphic dll

Started by Dirk Gregorius Dec 30, 2003 at 7:54 AM 2 replies 500+ views
Original Post
Dirk Gregorius
Dirk Gregorius
Hi everybody is it possible to export a function pointer from a dll? I want to set the function pointer in the DllMain and then call the function using a function pointer. Example: dll.h: void (*pfnFUNC)(void); // Call functions using the pointer dll.cpp: void FunctionA(void) {/* Implementation */} void FunctionB(void) {/* Implementation */} int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved) { if (A) pfnFUNC = FunctionA; // Set the func pointer if (B) pfnFUNC = FunctionB; // I get an error here, telling that the types don''t match return 0; } client.cpp: #include <dll.h> bool SomeFunction(void) { // Call the correct function, specified on loading time pfnFUNC(); } 1.) Can this be done this way? 2.) Is this a good idea :-) ? 3.) Is it possible to export a pointer at all - so I would use some kind of global functor using the strategy pattern. I was able to export a const char pointer. Any help is greatly appreciated.. -Dirk
paulbird
paulbird
As I far as I know this is impossible - why would you need to though?
jamessharpe
jamessharpe
Just create an interface function that the dll exports that either returns the function pointer or sets a pointer to a function pointer that the calling process passes e.g.


typedef void (*pfnFUNC)();

pfnFUNC dllpfn = NULL;

//set the pointer in dllMain

//Then export this kind of function
void __declspec(export) GetPFN(pfnFunc *func)
{
*func = dllpfn;
}

Dirk Gregorius
Dirk Gregorius
@jamessharpe:

Thanx, nearly what I did. You can export the function pointer directly.

header:
#ifdef MATHDLL_EXPORTS
#define MATH_API __declspec(dllexport)
#else
#define MATH_API __declspec(dllimport)
#endif

// Typedefs for betting reading
typedef void (CALLBACK COPY_TYPE)(const float*, float*);

// Modul interface declaration
extern MATH_API COPY_TYPE* mtxcpy;

source:
//
// Modul interface definitions
//
MATH_API COPY_TYPE* mtxcpy;

This reason I try is following:

@paulbird
I have written (for learning puposes) a mathlib. Now I want to implement e.g MatrixMult with SSE or 3DNow when supported. The DllMain is called (means the dll is loaded) before the actually application starts. This gives me the opportunity to check for processor feature support, set the correct function pointers and then call the matching function in the interface functions of the math objects. My main design goal was to call the matching functions (std, sse or 3dnow) without any "if-then" code and I don''t want to call a InitMathLib() function at all. At the moment it works, it seems as if there was very few indirection (Maybe I even can inline them - Petzold says something like this) and I haven''t got an error so far. Now I have to learn some assembler basics and profile a little - things I wanted to learn in this project.

If you see any problems or have another idea, please let me know!

All the best for the next year

-Dirk

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.