ReactOS 0.4.16-dev-2104-gb84fa49
commonCRTStartup.hpp
Go to the documentation of this file.
1//
2// commonCRTStartup.c
3//
4// Copyright (c) 2024 Timo Kreuzer
5//
6// Implementation of common executable startup code.
7//
8// SPDX-License-Identifier: MIT
9//
10
11#include <corecrt_startup.h>
12#include <internal_shared.h>
13#include <excpt.h>
14#include <stdlib.h>
15#include <intrin.h>
16#include <pseh/pseh2.h>
17
18// Defined in winnt.h
19#define FAST_FAIL_FATAL_APP_EXIT 7
20
21extern "C" int __cdecl main(int, char**, char**);
22extern "C" int __cdecl wmain(int, wchar_t**, wchar_t**);
23
24template<typename Tmain>
25static int call_main();
26
27template<>
29{
31
33}
34
35template<>
37{
39
41}
42
43static bool __scrt_initialize()
44{
46
47 if (!__vcrt_initialize())
48 {
49 return false;
50 }
51
52 if (!__acrt_initialize())
53 {
55 return false;
56 }
57
58 if (_initterm_e(__xi_a, __xi_z) != 0)
59 {
60 return false;
61 }
62
64
65 return true;
66}
67
68template<typename Tmain>
69static __declspec(noinline) int __cdecl __commonCRTStartup()
70{
71 int exitCode;
72
73 if (!__scrt_initialize())
74 {
76 }
77
78 __try
79 {
80 exitCode = call_main<Tmain>();
81 }
83 {
84 exitCode = GetExceptionCode();
85 }
87
88 exit(exitCode);
89}
static bool __scrt_initialize()
static int call_main()
int call_main< decltype(main)>()
#define FAST_FAIL_FATAL_APP_EXIT
int call_main< decltype(wmain)>()
int *CDECL __p___argc(void)
Definition: data.c:227
wchar_t ***CDECL __p___wargv(void)
Definition: data.c:334
int CDECL _initterm_e(_INITTERM_E_FN *table, _INITTERM_E_FN *end)
Definition: data.c:654
char ***CDECL __p___argv(void)
Definition: data.c:329
void CDECL _initterm(_INITTERMFUN *start, _INITTERMFUN *end)
Definition: data.c:632
#define __cdecl
Definition: corecrt.h:121
_ACRTIMP errno_t __cdecl _configure_narrow_argv(_crt_argv_mode)
@ _crt_argv_unexpanded_arguments
_ACRTIMP errno_t __cdecl _configure_wide_argv(_crt_argv_mode)
_ACRTIMP wchar_t **__cdecl _get_initial_wide_environment(void)
_ACRTIMP char **__cdecl _get_initial_narrow_environment(void)
#define noinline
Definition: types.h:64
int main()
Definition: test.c:6
void __declspec(noinline) __cdecl _free_base(void *const block)
Definition: free_base.cpp:98
#define GetExceptionCode
Definition: excpt.h:83
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:90
__crt_bool __cdecl __acrt_initialize()
_PVFV __xc_a[]
_PVFV __xc_z[]
_PIFV __xi_z[]
_PIFV __xi_a[]
__fastfail(FAST_FAIL_INVALID_ARG)
#define __try
Definition: pseh2_64.h:188
#define __except
Definition: pseh2_64.h:189
#define __endtry
Definition: pseh2_64.h:191
int wmain()
#define exit(n)
Definition: config.h:202
__vcrt_bool __cdecl __vcrt_uninitialize(_In_ __vcrt_bool _Terminating)
Definition: __vcrt_init.c:28
int __cdecl __isa_available_init(void)
__vcrt_bool __cdecl __vcrt_initialize(void)
Definition: __vcrt_init.c:18