13

I have some general purpose functions defines in my exe file like Log(char* str). This function takes a string as an input from the caller and writes it to the logfile defined for that application. Now I want to write DLL code which will include a function to upload a file to a server.

The objective is to import the upload function from the DLL into the exe and use it. However, if any error is encountered, then the upload function must call the Log(char* str) function to write the error into the log file.

The trouble is that this DLL needs to be used in multiple applications and each app will have a different logfile at a different location. I want to write the DLL in such a way that it calls the corresponding Log(char* str) defined in the application. Similarly I have some other functions that are application-specific and cannot be included in the DLL beforehand.

How can I write such DLL code where it only knows the function prototype, but not the function definition, which resides inside the exe?

3
  • 3
    Log(char const* str), surely? Commented Sep 13, 2011 at 15:41
  • Writing multi-language source files is hard work. I suggest you stick to one language -- unless you're doing it for fun. Commented Sep 13, 2011 at 15:44
  • possible duplicate of dll to main program communication Commented Sep 13, 2011 at 16:31

1 Answer 1

14

Have a function in your DLL that the .exe can call, passing in a function pointer to your Log function. Inside the DLL, store that function pointer and use it when you need to log things.

If there is more than one such function you need to pass to your DLL, consider using a struct to store all the relevant function pointers.

Here's a good tutorial on function pointers: The function pointer tutorial

To keep this interface simple, I would highly recommend keeping all these "customizable" functions in plain C, at least as a starting point. Dealing with pointer-to-member functions and passing C++ objects around through C wrappers is error-prone. (But doable.) Or stick with C++ all round.

Here's a short example. In a common header, put something like this:

typedef void (*logfunc)(char const*); 

to simplify passing around the customized function pointer.

In your DLL code, you could then have:

logfunc cust_log; void dll_init_logging(logfunc l) { cust_log = l; } void dll_do_something() { cust_log("hello"); } 

Modify that to make these functions exportable if necessary on your platform.

Then from your main code, all you need to do (assuming you're loading the DLL and making the exported functions available to your .exe with their original name) is:

void logit(char const* str) { printf("Log: %s\n", str); } int main (int argc, char const *argv[]) { // load the DLL dll_init_logging(logit); ... dll_do_something(); ... return 0; } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thnks a lot. It gave me some idea, however I am not 100% sure how to implement it in C. "Have a function in your DLL that the .exe can call, passing in a function pointer to your Log function. Inside the DLL, store that function pointer and use it when you need to log things." Could u plz write a code snippet for better understanding
@Shanty: added some example code. Probably won't work as-is with dynamic linking - depends on your platform, but you'll get the idea. (Works just fine if compiled statically.)
@Shanty: If this answer solved your problem, accept it. Reputation is the currency you spend to get answers on SO.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.