0

I am trying to compile a DLL which includes a CDialog Form. In detail I would like to include the resource containing the Layout and all the public methods.

The main goal is to export a form(and its methods) to use it in an external environment without messing with the IDDs of existing forms.

My (day-long) research did not show any possible solutions to this and as I am new to programming C++ with MFC, I do not know, how to get started.

Thanks for Your support!

  • Welcome to StackOverflow! Can you be more specific as to what your problem is? What have you tried so far? – Nate Barbettini Jan 20 '15 at 15:24
  • Hello Nate, I have tried to create a DLL-Project with VS2010 which compiles and works fine out of the box - thats were i am stuck right now. The next step that would have to be done is "importing" the existing form (code and resource) but I dont know, what has to be considered whilst doing this, to not end up in a dead end. I do not expect "the solution" but rather hope to find a direction that i can follow. – jbrockmannde Jan 20 '15 at 16:03

1 Answers1

0

I am new to programming C++ with MFC, I do not know, how to get started.

Here goes...You may have all ready taken some of these measures.

One way to accomplish this is to create a header file that can be used to direct the exporting and importing of your code. For example, in the header file of the dialog class, if you code something like,

#ifdef DllImpExp
#undef DllImpExp
#endif

#ifdef SomeDefineWeWillUseToControlImportExport
#define DllImpExp   __declspec( dllexport )
#else
#define DllImpExp   __declspec( dllimport )
#endif

the same header can be used to export and import the dialog class. The declaration of your dialog class should then specify “DllImpExp”. For example,

class DllImpExp SomeClass : public SomeBaseClass

In the project that creates the dll, you would add a preprocessor define for “SomeDefineWeWillUseToControlImportExport”. This forces the compiler to export your dialog class code. At this point, your dialog class is ready to be consumed by another object.

Since you’ve exported your class, your dll project has created an import lib with the same name. That lib is used as input into the other projects in which you’d like to use the dialog code. There are several ways to do it, but, simply listing that lib as input to the linker of the project that wants to use it should work. Additionally, you must not define "SomeDefineWeWillUseToControlImportExport" in the project, so that the class will be imported.

As for accessing your dialog resource in the dll, you’ll need to take some special steps. Before you try to display the dialog, you’ll need to point the current resource handle to the dll that contains the resource. Your app uses a single handle to know where to load resources (ie. dialogs). Since you have dialogs in one or more locations, you need to use two functions to manage that. You'll want to use AfxGetResourceHandle to save the current default handle that is used to load resources. Then, look at AfxSetResourceHandle to change the default handle to load from your dll. And, don’t forget to restore the saved handle when you are done working with the dialog from your dll. Failure to do that will likely result in some strange mfc asserts.

rrirower
  • 4,338
  • 4
  • 27
  • 45