mr4th-dynamic-linking/win32_before_main/win32_before_main.c

24 lines
629 B
C
Raw Normal View History

2024-04-24 13:31:59 +00:00
// this specific section contains before-main function pointers
// this pragma ensures the compilation unit will generate this section
#pragma section(".CRT$XCU", read)
// declare the "before main" function
static void run_before_main_func(void);
// set the before-main execution function pointer
__declspec(allocate(".CRT$XCU"))
__pragma(comment(linker, "/INCLUDE:run_before_main_ptr"))
void (*run_before_main_ptr)(void) = run_before_main_func;
// define the "before main" function
int x = 0;
static void run_before_main_func(void){
x = 100;
}
// main
#include <stdio.h>
int main(){
printf("x = %d\n", x);
return(0);
}