I’m trying to load a hypothetical plugin with the following header:
#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
class DLLIMPORT DllClass
{
public:
virtual ~DllClass(void);
static DllClass* getPCFilter();
virtual int Process(int a, int b);
protected:
DllClass();
};
#endif /* _DLL_H_ */
And my host code does:
HINSTANCE hinstDLL;
hinstDLL = LoadLibrary(L"PCFilter.dll");
if(hinstDLL)
{
typedef DllClass*(*Factory)();
Factory fun1;
fun1 = (Factory)GetProcAddress(hinstDLL, "DllClass::getPCFilter");
The dll opens but the GetProcAddress is not finding the static factory method. Am I not supposed to be able to do it this way?
I’ve tried getting rid of the static method and instead, after the class declaration, doing the following:
extern "C" DLLIMPORT void* getPCFilterInstance()
{
return (void*)new DllClass();
}
But then, when compiling the host source, the linker complains:
In function `getPCFilterInstance'::
[Linker Error] undefined reference to `_imp___ZN8DllClassC1Ev'
This I can solve by linking to the .a lib. But isn’t the idea of the DLL to not need to link at compile time?
You can use
extern "C"statement to export your function with the same name as you’ve declared it in the code – otherwise it will get mangled by the c++ compiler