Article ID: 114376
Article Last Modified on 11/21/2006
if ((hDlgInit = ::FindResource(AfxGetInstanceHandle(),
lpszResourceName, RT_DLGINIT)) != NULL)
This forces the use of only those resources bound to the executing
module. For localization and other uses, it must be possible to load
resources from other sources.
HINSTANCE ResourceInit(LPCSTR szResourceName,LPCSTR szResType)
{
// first check the app
HINSTANCE hInstApp = AfxGetResourceHandle();
if (::FindResource(hInstApp, szResourceName,
szResType) != NULL)
return hInstApp;
// check for DLLs in proper order
CDynLinkLibrary* pDLL;
for (pDLL = _AfxGetAppData()->pFirstDLL; pDLL != NULL;
pDLL = pDLL->m_pNextDLL)
{
if (pDLL->m_hResource != NULL &&
::FindResource(pDLL->m_hResource,
szResourceName, szResType) != NULL)
{
// found it in a DLL, use it's resource handle
return pDLL->m_hResource;
}
}
// if failed to find resource, fail initialization
TRACE("\n Resource not found \n");
return (HINSTANCE)NULL;
}
Make a call to this function in the CDialog derived class' override
of OnInitDialog(). The ResourceInit() function will check the list
of CDynLinkLibrary objects for the application and return the
appropriate resource handle (or NULL if not found). The OnInitDialog()
function may look like this:
BOOL CDerivedDlg::OnInitDialog()
{
// attempt to find dialog box template
HINSTANCE hResInst = ResourceInit(m_lpDialogTemplate, RT_DIALOG);
if(NULL == hResInst) return FALSE;
// get app's resource handle so we can set it back later
HINSTANCE hAppInst = AfxGetResourceHandle();
// temporarily set to DLL's resource handle
AfxSetResourceHandle(hResInst);
// Call base class which initializes the dialog
// and loads the template
BOOL bInit = CDialog::OnInitDialog();
// restore the app's resource handle
AfxSetResourceHandle(hAppInst);
return bInit;
}
In addition, the resource handle must be set correctly before the
call to DoModal(). The easiest way to do this is to:
CDerivedDlg::CDerivedDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDerivedDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDerivedDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
AfxSetResourceHandle(ResourceInit(m_lpDialogTemplate,RT_DIALOG));
}
Here is an example of the destructor:
CDerivedDlg::~CDerivedDlg()
{
AfxSetResourceHandle(AfxGetApp()->m_hInstance);
// reset resource to application's instance handle
}
The ResourceInit() function may be used at any point for any type of
resource that may be located in a DLL or in the application's
resources.
Additional query words: 1.00 2.00 2.10 international language afxdll
Keywords: kbbug kbfix KB114376