41 lines
		
	
	
		
			887 B
		
	
	
	
		
			C
		
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			887 B
		
	
	
	
		
			C
		
	
	
// 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"))
 | 
						|
__declspec(dllexport)
 | 
						|
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);
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
#if 0
 | 
						|
// wrapped in a macro:
 | 
						|
 | 
						|
#define BEFORE_MAIN(n) static void n(void); \
 | 
						|
__declspec(allocate(".CRT$XCU"))           \
 | 
						|
__declspec(dllexport)                      \
 | 
						|
void (*n##__)(void) = n;                   \
 | 
						|
static void n(void)
 | 
						|
 | 
						|
BEFORE_MAIN(before_main_rule){
 | 
						|
  // do work here
 | 
						|
}
 | 
						|
 | 
						|
#endif
 |