Sunday, April 24, 2005

DLL Hell part 2

It's interesting how life works. It's only been a couple of days that I had to load a DLL dynamically at work and now I have to do the same for a project I'm working at home. The only difference is that I need to load child dialog resources into my application.

You would think that it wouldn't be too hard, but MFC always manages to make it hard. When will I learn to not use MFC? Anyways what I needed was to call the create on a dialog passing it my parent window.

DLL

CDlg::InitDlg( CWnd* pParent )
{
VERIFY( Create( IDD, pParent ) );
}
Now you would think that by calling this function in the CDialog derived class that it would work. But somehow, that's does not work. At first it was errors because it could not the resource. This is easily repaired by doing this:
DLL

CDlg::InitDlg( CWnd* pParent )
{
AFX_MANAGE_STATE(AfxGetStaticModuleState( ));

VERIFY( Create( IDD, pParent ) );
}
But now, I was getting errors because of a map somewhere in MFC which was NULL. I checked everywhere but I did not find answers. So ended coding it like this:

App

CApp::ShowDllResource()
{
HINSTANCE hResApp = AfxGetResourceHandle();
HINSTANCE hResExt = GetModuleHandle(_T("DllName.dll"));
if(hResExt)
{ // loading resources was OK


AfxSetResourceHandle(hResExt);

m_pDlgResource->GetDlgWnd()->Create( m_pDlgResource->GetDlgID(), this);

m_pDlgResource->GetWnd()->ShowWindow(SW_SHOW);

AfxSetResourceHandle(hResApp);
}
}
By magic, this worked. It's not very elegant but this was done late at night. If I change this, I'll post another Blog about it.

This example was found on CodeProject in the threads at the end of the article.

0 Comments:

Post a Comment

<< Home