Mixed DLL
-
Hi! I'm referring on the article Calling Managed Code from Unmanaged Code and vice-versa[^], where callback functions in managed C++ code is called from unmanaged code using delegates. On that way I have programmed a Windows Forms GUI, whose functions are delegated to unmanaged (because I had an unmanaged Interface to implement). The whole marshalling is done in one VS-project. Now I want to compile it as DLL and call the unmanaged functions to control the GUI: PROBLEM: To initialise the callbacks the first thing to do is to call managed code to activate the delegates and so the callbacks. Now it would be nice to do that in the DLL-main function (entry point). But it is not possible to call managed code in DLL main, what should I do? I want to initialize the callbacks on DLL loading, so that the functions are directly useable. Any idea? Thanks, cherry
-
Hi! I'm referring on the article Calling Managed Code from Unmanaged Code and vice-versa[^], where callback functions in managed C++ code is called from unmanaged code using delegates. On that way I have programmed a Windows Forms GUI, whose functions are delegated to unmanaged (because I had an unmanaged Interface to implement). The whole marshalling is done in one VS-project. Now I want to compile it as DLL and call the unmanaged functions to control the GUI: PROBLEM: To initialise the callbacks the first thing to do is to call managed code to activate the delegates and so the callbacks. Now it would be nice to do that in the DLL-main function (entry point). But it is not possible to call managed code in DLL main, what should I do? I want to initialize the callbacks on DLL loading, so that the functions are directly useable. Any idea? Thanks, cherry
What you can do is have a flag variable that keeps track of whether the delegates are initialized. Then, in every exported function, the first thing you do is check to see if the delegates have been initialized. If they have not been initialized (according to the flag variable,) then you call the initialization function from there. That way, the initialization is performed on the thread that is calling your Dll, instead of the thread that is calling DLLMain.