Nobody wrote:
Lets say I have a class that is only available if a specific DLL (a.dll) is
present. I can't link to that DLL through lib files or my app will fail on
any machine that doesn't have a.dll.
So I do LoadLibrary()'s and keep function pointers in my wrapper class...
Which is a better style?
DWORD CClass::SomeFunc()
{
if (m_pfn != NULL)
return (*m_pfn)(parm1, parm2);
return (DWORD) -1;
}
or
DWORD CClass::SomeFunc()
{
ASSERT(m_pfn != NULL);
return (*m_pfn)(parm1, parm2);
}
I guess the first version fails "more graciously" if the developer calls it
incorrectly, but the 2nd version isn't returning "made up" return codes and
is more "in your face" during development.
I guess later is better - because not every function returns DWORD that
can be interpreted. Your code has to verify the presence of .dll abefore
calling methods anyway, so you are just duplicating this there.
BTW, in our system we have introduced nice related tool to automatize
generating such wrapper objects. To create such object, we provide
".dli" file with content like (actual example is for Lotus Notes client
..dll)
FN(WORD, OSLoadString, (HMODULE hModule, STATUS StringCode, char
*retBuffer, WORD BufferLength))
FN(WORD, OSTranslate, (WORD TranslateMode, char far *In, WORD
InLength, char far *Out, WORD OutLength))
FN(STATUS, NotesInitExtended, (int argc, char **argv))
FN(STATUS, OSPathNetConstruct, (char *PortName, char *ServerName, char
far *FileName, char *retPathName))
FN(STATUS, NSFDbOpen, (char far *PathName, DBHANDLE far *rethDB))
FN(STATUS, NSFDbClose, (DBHANDLE hDB))
............
- basically, this is somewhat "reparsed" header file for .dll. Then place
#define DLLFILENAME "nnotes.dll"
#define DLIMODULE NOTES
#define DLIHEADER <notes/notes.dli>
#define DLLCALL LNPUBLIC
#include <Core/dli_header.h>
to common header file and
#define DLLFILENAME "nnotes.dll"
#define DLIMODULE NOTES
#define DLIHEADER <notes/notes.dli>
#define DLLCALL LNPUBLIC
#include <Core/dli_header.h>
to some .cpp file.
This creates global function NOTES() returning object instance that has
all .dll functions described in .dli file defined as its methods.
Moreover, it has operator bool that can be used to test whether .dll is
present:
char h[256];
if(NOTES())
NOTES().OSLoadString(GetModuleHandle(NULL), ERR(nError), h, 255);
(
http://upp.sf.net)
Mirek