user11343987
user11343987

Reputation: 13

How can I pass a preprocessor definition to DLL at library load time

I'm working on an application that links against a DLL which itself contains a data structure whose definition varies depending on a preprocessor definition:

struct Example
{
    uint8_t StringA[32];
    #if USE_B
    uint8_t StringB[32];
    #endif
};

I need to use both versions of the structure at different times in my application. It seems, therefore, that I need to load the DLL using LoadLibrary with USE_B undefined for part of the execution (this is standard and straightforward), unload it using FreeLibrary, and then load it again with USE_B defined to a nonzero value. I am unsure how to do this, and would appreciate any help.

Upvotes: 1

Views: 341

Answers (2)

perreal
perreal

Reputation: 98088

You need to compile two versions of the DLL with different values of the flag. You can then use them from the binary by loading the version you need dynamically.

Upvotes: 0

Michael Chourdakis
Michael Chourdakis

Reputation: 11178

LoadLibrary/FreeLibrary are run-time concepts and #ifs are compile-time concepts. If you compile a module without the #if defined, it will stay as such no matter how you load or reload it.

Define two classes, one with StringA and one inherited using also StringB.

Then add a virtual function to the class to make it polymorphric and decide at the run time which of the two you want by using dynamic_cast<>.

Upvotes: 2

Related Questions