win32_load_time example
commit
41b9b4b3bf
|
@ -0,0 +1 @@
|
|||
build/*
|
|
@ -0,0 +1,26 @@
|
|||
@echo off
|
||||
|
||||
SET src=%cd%
|
||||
|
||||
cd ..
|
||||
if not exist "build\" mkdir build
|
||||
cd build
|
||||
|
||||
REM: ## The arrangement of this build ##
|
||||
REM: The "base layer" must be built and shared by all dynamic components.
|
||||
REM: In this example the the base layer is linked via load-time linking with the other modules.
|
||||
REM: The "plugin" is loaded at run-time.
|
||||
|
||||
|
||||
|
||||
REM: Build the base layer as a .dll (also produces load-time linking data in win32_base.lib)
|
||||
cl /nologo /Zi /LD %src%\win32_base.c
|
||||
|
||||
|
||||
REM: Build the executable; provide the base layer's load-time linking data via win32_base.lib
|
||||
cl /nologo /Zi %src%\win32_main.c win32_base.lib
|
||||
|
||||
|
||||
REM: Build the plugin as a .dll; provide the base layer's load-time linking data via win32_base.lib
|
||||
cl /nologo /Zi /LD %src%\win32_plugin.c win32_base.lib
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
// declare base_func; mark it as an exported symbol
|
||||
__declspec(dllexport) void base_func(void);
|
||||
|
||||
// define base_func
|
||||
#include <stdio.h>
|
||||
int x = 0;
|
||||
__declspec(dllexport) void
|
||||
base_func(void){
|
||||
printf("x = %d\n", x);
|
||||
x += 1;
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
// 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();
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
// declare base_func; mark it as a load-time imported symbol
|
||||
__declspec(dllimport) void base_func(void);
|
||||
|
||||
// declare plugin_func; mark it as an exported symbol
|
||||
__declspec(dllexport) void plugin_func(void);
|
||||
|
||||
// define plugin_func
|
||||
#include <stdio.h>
|
||||
__declspec(dllexport) void
|
||||
plugin_func(void){
|
||||
printf("provided by plugin: {\n");
|
||||
printf(" ");
|
||||
base_func();
|
||||
printf(" ");
|
||||
base_func();
|
||||
printf("}\n");
|
||||
}
|
Loading…
Reference in New Issue