Possible Duplicate:
Why do I get “unresolved external symbol” errors when using templates?
My solution is split into 3 projects. The first being an MFC (GUI) app, the second being a static lib, and the third being a DLL. The DLL depends on the static lib, and the GUI depends on both. The idea here was to write an SDK (static lib) complete with a demo plugin (DLL) which the MFC (GUI) app uses.
That being said, the SDK contains template functions to allow plugin developers to choose a prototype function to hook. But there seems to be an implementation problem with the dll vs. the lib...
SdkLib.h:
#define MY_EXPORTS
#include "SdkHookMgr.h"
// Prototypes
typedef HMODULE (WINAPI *HookLoadLibraryA)( LPCSTR lpFileName );
typedef HMODULE (WINAPI *HookLoadLibraryW)( LPWSTR lpFileName );
SdkHookMgr.h:
#ifdef MY_EXPORTS
#define MY_API __declspec(dllexport) WINAPI
#else
#define MY_API WINAPI
#endif
template <typename HookFunction>
BOOL MY_API Hook(HookFunction Target);
SdkHookMgr.cpp:
#include "SdkHookMgr.h"
template <typename HookFunction>
BOOL MY_API Hook(HookFunction Target)
{
switch( typeid(Target) )
{
case HookLoadLibraryA:
// Use private SDK functions to hook LoadLibraryA
break;
case HookLoadLibraryW:
// Use private SDK functions to hook LoadLibraryW
break;
//...
default:
return FALSE;
}
return TRUE;
}
demodll.cpp:
#include <SdkLib.h>
HMODULE WINAPI Hooked_LoadLibraryA ( LPCSTR lpFileName )
{
//...
}
//...
Hook<HookLoadLibraryA>(Hooked_LoadLibraryA);
The issue here is that the lib compiles just fine, but the dll shows:
demodll.obj : error LNK2019: unresolved external symbol "int __stdcall Hook<struct HINSTANCE__ * (__stdcall*)(char const *)>(struct HINSTANCE__ * (__stdcall*)(char const *))"
I suppose what's happening is the lib sees the implementation in SdkHookMgr.cpp
, but the dll doesn't (even though it links with sdklib.lib). I really enjoyed the idea of limiting developers to using prototypes and functions with identical signatures when calling the Hook()
function, and figured templates was the best way to go... But how can I get around this loophole without having to include the implementation of Hook()
within SdkHookMgr.h
? The point of building a static lib was to avoid plugin developers from poking at the SDK's source code in the first place...