# Calling a form in an MFC DLL

This topic is 3905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I have an old EXE that I have basically "Shared" into a DLL for this application ( The same code has to exist in the EXE as the dll ). I'm trying to call up a new Form from the DLL inside the main EXE for our new project, but it hasn't exactly gone well.. I'm currently using VS 2003, C++, and this is an MFC project. I currently have the following form ( Much snipped out. It reallyl is basic ) Header:
#include "Resource.h"
// frmEPTReportBuilder dialog

class frmEPTReportBuilder : public CDialog
{
DECLARE_DYNAMIC(frmEPTReportBuilder)
public:
frmEPTReportBuilder(CWnd* pParent = NULL);   // standard constructor
virtual ~frmEPTReportBuilder();
// Dialog Data
enum { IDD = IDD_DLGEPTRPTBLDG };

protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
afx_msg void OnBnClickedCancel();
};


Body:
#include "stdafx.h"
#include "frmEPTReportBuilder.h"

// frmEPTReportBuilder dialog

IMPLEMENT_DYNAMIC(frmEPTReportBuilder, CDialog)
frmEPTReportBuilder::frmEPTReportBuilder(CWnd* pParent /*=NULL*/)
: CDialog(IDD_DLGEPTRPTBLDG, pParent)
{
}

frmEPTReportBuilder::~frmEPTReportBuilder()
{
}
BEGIN_MESSAGE_MAP(frmEPTReportBuilder, CDialog)
ON_WM_DESTROY()
ON_BN_CLICKED(IDOK, OnBnClickedOk)
ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
END_MESSAGE_MAP()

void frmEPTReportBuilder::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
void frmEPTReportBuilder::OnBnClickedOk()
{
OnOK();
}
void frmEPTReportBuilder::OnBnClickedCancel()
{
OnCancel();
}


Nothing special here. Note the "CDialog(IDD_DLGEPTRPTBLDG" for calling the base constructor. I did have the default "frmEPTReportBuilder::IDD" in here, and it doesn't make a differance either way. So I have made a single class that has a static function that takes a window handle, and calls the frmEPTReportBuilder constructor, setting the CWnd* as the parent to the form, and calling DoModal(). Here it is... Header:
#pragma once

#ifdef _EPT_EXPORTS
#define EPTREPORTBUILDER__API __declspec(dllexport)
#else
#define EPTREPORTBUILDER__API __declspec(dllimport)
#endif

class EPTREPORTBUILDER__API CEPTReportBuilder
{
public:
static void SpawnReportBuilderForm ( CWnd * parent );


Body:
#include "StdAfx.h"
#include "EPTReportBuilder.h"
#include "frmEPTReportBuilder.h"
void CEPTReportBuilder::SpawnReportBuilderForm ( CWnd * parent )
{
frmEPTReportBuilder dlg;
dlg.SetParent ( parent );
//Set some things

int iResult = dlg.DoModal();
if( IDOK == iResult )
{
}

CString sTemp = "";
}


I have a menu item in the main program that simply calls CEPTReportBuilder::SpawnReportBuilderForm ( this ) { this being the MainFrame class in the MDI }. When dlg.SetParent gets called, it errors out ( Debug Assertion Failed ), but I cannot get to the code where this happens ( atlmfc\include\afxwin2.inl, line 284 ). If I take out setting the parent, the next line will always return a -1, and the form will never show. This I can step into ( dlgCore.cpp ). Basically, in the DoModal Dialog is a line ( 501 ) that says " if (lpDialogTemplate == NULL) return -1; ". My only real guess here is that I can't find the correct Resources in the DLL from the main EXE. This is my first real stab at creating a DLL inside MFC. I've been at the problem for a week, and I have too much else to get done to stay stuck.. Thanks for any help

• 40
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631368
• Total Posts
2999595
×