2
votes

I have two projects: one MFC .exe and an MFC .dll. I have an MFC dialog defined in a DLL. It has a resource associated to it and it also has a class CToolboxDiag derived from CDialog.

The dialog has a simple button that shows a message dialog when clicked.

void CToolboxDiag::OnBnClickedButton()
{
   MessageBox(_T("Test"), _T("T"));
}

I can export the resource from the DLL to my code, and create a standard CDialog with the correct appearance using the following code:

CDialog *diag = new CDialog;

HINSTANCE hClientResources = AfxGetResourceHandle();

//Tell the client to use the .DLL's resources
AfxSetResourceHandle(dll);

// resource_id is the resource_id in the DLL
diag->Create(resource_id, NULL);

//Restore the client application resource handle
AfxSetResourceHandle(hClientResources);

But this only results in the dialog showing up but the controls (i.e. the button) performs no action when clicked, as it doesn't have the linkage to the CToolboxDiag definition in the .exe.

I wanted to export the dialog (with the button code) without having to export the class definition to the .exe. In other words, I want to export a fully functional dialog, including its buttons actions, without having the CToolboxDialog definition on my .exe, so that this can be fully modular. How could I do this?

1
put AFX_MANAGE_STATE(AfxGetStaticModuleState()); while calling dll functionsSantosh Dhanawade
@SantoshDhanawade yes thank you. But what I want to know is how to import the fhole dialog functionality from the DLLmanatttta

1 Answers

2
votes

This can't work in this way. The resource template has no direct connection to your code in the DLL. Your code just created an "empty" CDialog class that has no handlers hat all, expect the default handlers (OnOk, OnClose...)

So you need to create the object CToolboxDiag and this has to happen wehere the dialog code is located. This is inside the DLL.

The easiest way is to export a function that just creates the dialog inside the DLL and just returns a CDialog* to your application.

Be aware that this is only allowed and will work without problems when you are using the DLL shared version of the MFC.