Hi.
I'm writing a proxy library (called Library A) that is just an interface to another DLL (called Library B) that may be present or not on the system. The idea is that a program would link to this library A instead of the original library B ; and Library A would handle the errors if Library B wasn't installed on the system.
So a typical proxy function would look like this:
int function(int arg1, int arg2)
{
HINSTANCE hinstLib;
UINT errormode = SetErrorMode(SEM_FAILCRITICALERRORS);
SetErrorMode(errormode | SEM_FAILCRITICALERRORS);
hinstLib = LoadLibrary(TEXT(ORIGINAL_DLL));
SetErrorMode (errormode);
if (hinstLib != NULL)
{
ProcAdd = (void *) GetProcAddress(hinstLib, TEXT("function"));
if (NULL != ProcAdd)
{
return (ProcAdd) (arg1, arg2);
}
FreeLibrary(hinstLib);
}
return ERROR;
}
Now, I would do this for all original entries in Library B. There could be a lot of calls to it. So loading / unloading the DLL so frequently is certainly going to have an impact speed-wise.
I was wondering if it would be acceptable to use a global variable hinstLib ; something like
HINSTANCE hinstLib = LoadLibrary(TEXT(ORIGINAL_DLL));
int function(int arg1, int arg2)
{
if (hinstLib != NULL)
{
ProcAdd = (void *) GetProcAddress(hinstLib, TEXT("function"));
if (NULL != ProcAdd)
{
return (ProcAdd) (arg1, arg2);
}
}
return ERROR;
}
And let Windows automatically unload the DLL when the program exits (assuming it does unload it).
Thanks in advance for your wise remarks...
Jean-Yves