C++ MFC - Sharing data between MFC DLL and WTL exe?

Go To StackoverFlow.com


I have a MFC project that provides a GUI interface for manipulating a complicated data structure (let's call it ComplicatedClass). I have a WTL project that needs to use this class a lot. I would like to be able to manage only one instance of this class and don't want each project to have its own copy of ComplicatedClass.

If I want these projects to share one instance of this class, how would I go about doing this in a safe and reliable manner? It seems like the only alternative to me is to basically have two copies of the class (one managed by the MFC project and one managed by the WTL side).

2012-04-03 22:09
by user1040229


You don't need two copies. You don't mention how your application is structured, but I assume the MFC project is an .exe and the WTL one is a dll. Export a function from the dll that takes a ComplicatedClass* and store it for use in your dll. I assume here that ComplicatedClass doesn't have any UI data - i.e., no CWnd-derived objects. If it does, you'll need to be careful to wrap your HWND's properly, or stick to win32-only messaging (i.e., only use ::SendMessage).

This is just general advise, your question isn't specific enough for anything else. For example I don't see why you conclude that you have to have two copies. If you just pass a pointer from one to the other, they can both access them.

2012-04-04 15:23
by Roel