ReactOS 0.4.16-dev-814-g656a5dc
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}
#define __cdecl
Definition: accygwin.h:79
static bool __scrt_initialize()
static int call_main()
int call_main< decltype(main)>()
#define FAST_FAIL_FATAL_APP_EXIT
int call_main< decltype(wmain)>()
_ACRTIMP wchar_t **__cdecl _get_initial_wide_environment(void)
_ACRTIMP void __cdecl _initterm(_In_reads_(_Last - _First) _In_ _PVFV *_First, _In_ _PVFV *_Last)
_ACRTIMP char **__cdecl _get_initial_narrow_environment(void)
_ACRTIMP int __cdecl _initterm_e(_In_reads_(_Last - _First) _PIFV *_First, _In_ _PIFV *_Last)
_ACRTIMP errno_t __cdecl _configure_wide_argv(_In_ _crt_argv_mode mode)
_ACRTIMP errno_t __cdecl _configure_narrow_argv(_In_ _crt_argv_mode mode)
#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
_CRTIMP char ***__cdecl __p___argv()
Definition: getargs.c:483
_CRTIMP wchar_t ***__cdecl __p___wargv()
Definition: getargs.c:491
_CRTIMP int *__cdecl __p___argc()
Definition: getargs.c:475
#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:172
#define __except
Definition: pseh2_64.h:173
#define __endtry
Definition: pseh2_64.h:175
int wmain()
#define GetExceptionCode()
Definition: exception.h:68
#define exit(n)
Definition: config.h:202
@ _crt_argv_unexpanded_arguments
__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