Introduction
I have created this class to help me load a different resource DLL if it is present during runtime. An example would be a multi-language application. If another resource DLL (with a name specified at compile time) is present, it can load and use it as the resource for the application.
Let's look at the class header. It is quite simple and I think the comments describe well about the individual methods.
class CResourceManager
{
public:
CResourceManager();
virtual ~CResourceManager();
BOOL Setup(CString);
BOOL SetupEx(CString);
CString GetString(UINT);
private:
HINSTANCE m_hExternalResource;
HINSTANCE m_hApplicationResource;
};
Using the code
Below is an example of how you may use the methods of the class. First you need to create an object of CResourceManager
. You can put it as global or as a member of your application class. Then, call the Setup(...)
method in InitInstance(...)
as shown below.
BOOL CtestApp::InitInstance()
{
InitCommonControls();
CWinApp::InitInstance();
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
m_oResourceManager.SetupEx(_T("C:\\MyProjects\\anotherres.dll"));
.
.
.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
As for cleaning up the loaded resource DLL, the destructor will handle it as shown below.
CResourceManager::~CResourceManager()
{
if (m_hExternalResource != NULL)
FreeLibrary(m_hExternalResource);
m_hExternalResource = NULL;
m_hApplicationResource = NULL;
}
Points of interest
I admit that this class do fall short in terms of the other resource problems that could be addressed, but feel free to extend it if you want. :)