mr4th-dynamic-linking/win32_linking/win32_main.c

39 lines
1.1 KiB
C
Raw Normal View History

2024-04-23 18:50:47 +00:00
// declare base_func; mark it as a load-time imported symbol
__declspec(dllimport) void base_func(void);
// setup plugin_func as a function pointer so that it can be linked at run time
typedef void PLUGIN_Func(void);
PLUGIN_Func *plugin_func = 0;
// need to include for LoadLibraryA/LoadLibraryW and GetProcAddress
#include <Windows.h>
// helper macro for GetProcAddress that handles the casting of function pointers
#define GET_PROC(var,mod,name) *((FARPROC*)(&(var))) = GetProcAddress((mod),(name))
int main(){
// functions linked by load-time linking can be called directly without further setup
base_func();
// to call a function with run-time linking, we must manually load and link it
HMODULE module = LoadLibraryA("win32_plugin.dll");
if (module != 0){
GET_PROC(plugin_func, module, "plugin_func");
}
// calls to plugin_func only work after the "plugin" loaded successfully
if (plugin_func != 0){
plugin_func();
}
// demonstration: the data structures contained in the 'base' are not duplicated in each binary
base_func();
}