VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/nocrt-startup-exe-win.cpp@ 96476

Last change on this file since 96476 was 96407, checked in by vboxsync, 3 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/* $Id: nocrt-startup-exe-win.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - No-CRT - Windows EXE startup code.
4 *
5 * @note Does not run static constructors and destructors!
6 */
7
8/*
9 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
10 *
11 * This file is part of VirtualBox base platform packages, as
12 * available from https://www.virtualbox.org.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation, in version 3 of the
17 * License.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <https://www.gnu.org/licenses>.
26 *
27 * The contents of this file may alternatively be used under the terms
28 * of the Common Development and Distribution License Version 1.0
29 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
30 * in the VirtualBox distribution, in which case the provisions of the
31 * CDDL are applicable instead of those of the GPL.
32 *
33 * You may elect to license modified versions of this file under the
34 * terms and conditions of either the GPL or the CDDL or both.
35 *
36 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
37 */
38
39
40/*********************************************************************************************************************************
41* Header Files *
42*********************************************************************************************************************************/
43#include "internal/nocrt.h"
44#include "internal/process.h"
45
46#include <iprt/nt/nt-and-windows.h>
47#include <iprt/getopt.h>
48#include <iprt/message.h>
49#include <iprt/path.h>
50#include <iprt/string.h>
51#include <iprt/utf16.h>
52
53#include "internal/compiler-vcc.h"
54
55
56/*********************************************************************************************************************************
57* External Symbols *
58*********************************************************************************************************************************/
59extern int main(int argc, char **argv, char **envp); /* in program */
60#ifndef IPRT_NO_CRT
61extern DECLHIDDEN(void) InitStdHandles(PRTL_USER_PROCESS_PARAMETERS pParams); /* nocrt-streams-win.cpp */ /** @todo put in header */
62#endif
63
64
65static int rtTerminateProcess(int32_t rcExit, bool fDoAtExit)
66{
67#ifdef IPRT_NO_CRT
68 /*
69 * Run atexit callback in reverse order.
70 */
71 if (fDoAtExit)
72 {
73 rtVccTermRunAtExit();
74 rtVccInitializersRunTerm();
75 }
76#else
77 RT_NOREF(fDoAtExit);
78#endif
79
80 /*
81 * Terminate.
82 */
83 for (;;)
84 NtTerminateProcess(NtCurrentProcess(), rcExit);
85}
86
87
88DECLASM(void) CustomMainEntrypoint(PPEB pPeb)
89{
90 /*
91 * Initialize stuff.
92 */
93#ifdef IPRT_NO_CRT
94 rtVccInitSecurityCookie();
95#else
96 InitStdHandles(pPeb->ProcessParameters);
97#endif
98 rtVccWinInitProcExecPath();
99
100 RTEXITCODE rcExit;
101#ifdef IPRT_NO_CRT
102 AssertCompile(sizeof(rcExit) == sizeof(int));
103 rcExit = (RTEXITCODE)rtVccInitializersRunInit();
104 if (rcExit == RTEXITCODE_SUCCESS)
105#endif
106 {
107 /*
108 * Get and convert the command line to argc/argv format.
109 */
110 rcExit = RTEXITCODE_INIT;
111 UNICODE_STRING const *pCmdLine = pPeb->ProcessParameters ? &pPeb->ProcessParameters->CommandLine : NULL;
112 if (pCmdLine)
113 {
114 char *pszCmdLine = NULL;
115 int rc = RTUtf16ToUtf8Ex(pCmdLine->Buffer, pCmdLine->Length / sizeof(WCHAR), &pszCmdLine, 0, NULL);
116 if (RT_SUCCESS(rc))
117 {
118 char **papszArgv;
119 int cArgs = 0;
120 rc = RTGetOptArgvFromString(&papszArgv, &cArgs, pszCmdLine,
121 RTGETOPTARGV_CNV_MODIFY_INPUT | RTGETOPTARGV_CNV_QUOTE_MS_CRT, NULL);
122 if (RT_SUCCESS(rc))
123 {
124 /*
125 * Call the main function.
126 */
127 AssertCompile(sizeof(rcExit) == sizeof(int));
128 rcExit = (RTEXITCODE)main(cArgs, papszArgv, NULL /*envp*/);
129 }
130 else
131#ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
132 RTMsgError("Error parsing command line: %Rrc\n", rc);
133#else
134 rtNoCrtFatalMsgWithRc(RT_STR_TUPLE("Error parsing command line: "), rc);
135#endif
136 }
137 else
138#ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
139 RTMsgError("Failed to convert command line to UTF-8: %Rrc\n", rc);
140#else
141 rtNoCrtFatalMsgWithRc(RT_STR_TUPLE("Failed to convert command line to UTF-8: "), rc);
142#endif
143 }
144 else
145#ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
146 RTMsgError("No command line\n");
147#else
148 rtNoCrtFatalMsg(RT_STR_TUPLE("No command line\r\n"));
149#endif
150 rtTerminateProcess(rcExit, true /*fDoAtExit*/);
151 }
152#ifdef IPRT_NO_CRT
153 else
154 {
155# ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
156 RTMsgError("A C static initializor failed (%d)\n", rcExit);
157# else
158 rtNoCrtFatalWriteBegin(RT_STR_TUPLE("A C static initializor failed ("));
159 rtNoCrtFatalWriteWinRc(rcExit);
160 rtNoCrtFatalWriteEnd(RT_STR_TUPLE("\r\n"));
161# endif
162 rtTerminateProcess(rcExit, false /*fDoAtExit*/);
163 }
164#endif
165}
166
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette