Reputation: 289
HINSTANCE hDll = LoadLibrary(_T("Test1.dll"));
if (NULL != hDll)
{
typedef CTest1Dlg*(*pDllFun)(int val);
pDllFun pDlg = (pDllFun)GetProcAddress(hDll, "ShowDlg");
if (NULL != pDlg)
{
pDlg(val);
}
}
Mainly: typedef CTest1Dlg*(*pDllFun)(int val);
The only difference in this line of code is that CTest1Dlg
is different, it will become CTest2Dlg
or other, but their parameters are the same
Upvotes: 0
Views: 75
Reputation: 66371
This suits a function template.
The most straightforward parametrization would probably be
template<typename Dlg>
void testDllFunction(LPCTSTR dll, const char* name, int val)
{
HINSTANCE hDll = LoadLibrary(dll);
if (hDll != NULL)
{
typedef Dlg*(*pDllFun)(int);
pDllFun pDlg = reinterpret_cast<pDllFun>(GetProcAddress(hDll, name));
if (pDlg != NULL)
{
pDlg(val);
}
}
}
// ...
testDllFunction<CTest1Dlg>(_T("Test1.dll"), "ShowDlg"));
But it's probably a good idea to separate the loading of the function from the calling of it.
Here's one suggestion for that:
template<typename Dlg>
using DlgFunction = Dlg*(*)();
template<typename Dlg>
bool loadFunction(LPCTSTR dll, const char* name, DlgFunction<Dlg>& fun)
{
HINSTANCE hDll = LoadLibrary(dll);
if (!hDll)
return false;
fun = reinterpret_cast<DlgFunction<Dlg>>(GetProcAddress(hDll, name));
return fun != NULL
}
// ...
DlgFunction<CTest1Dlg> test1;
if (loadFunction(_T("Test1.dll"), "ShowDlg", test1))
test1(12);
DlgFunction<CTest2Dlg> test2;
if (loadFunction(_T("Test2.dll"), "Kittens", test2))
test2(34);
Upvotes: 3