VirtualBox

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

Last change on this file since 95870 was 95870, checked in by vboxsync, 2 years ago

IPRT/nocrt: Split up the startup code and added the basic DLL startup code. bugref:10261

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1/* $Id: nocrt-startup-exe-win.cpp 95870 2022-07-27 02:38:01Z 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 Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * The contents of this file may alternatively be used under the terms
20 * of the Common Development and Distribution License Version 1.0
21 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
22 * VirtualBox OSE distribution, in which case the provisions of the
23 * CDDL are applicable instead of those of the GPL.
24 *
25 * You may elect to license modified versions of this file under the
26 * terms and conditions of either the GPL or the CDDL or both.
27 */
28
29
30/*********************************************************************************************************************************
31* Header Files *
32*********************************************************************************************************************************/
33#include "internal/iprt.h"
34#include "internal/process.h"
35
36#include <iprt/nt/nt-and-windows.h>
37#include <iprt/getopt.h>
38#include <iprt/message.h>
39#include <iprt/path.h>
40#include <iprt/string.h>
41#include <iprt/utf16.h>
42
43#include "internal/compiler-vcc.h"
44
45
46/*********************************************************************************************************************************
47* External Symbols *
48*********************************************************************************************************************************/
49extern DECLHIDDEN(void) InitStdHandles(PRTL_USER_PROCESS_PARAMETERS pParams); /* nocrt-streams-win.cpp */ /** @todo put in header */
50
51extern int main(int argc, char **argv, char **envp); /* in program */
52
53
54static int rtTerminateProcess(int32_t rcExit, bool fDoAtExit)
55{
56#ifdef IPRT_NO_CRT
57 /*
58 * Run atexit callback in reverse order.
59 */
60 if (fDoAtExit)
61 {
62 rtVccTermRunAtExit();
63 rtVccInitializersRunTerm();
64 }
65#else
66 RT_NOREF(fDoAtExit);
67#endif
68
69 /*
70 * Terminate.
71 */
72 for (;;)
73 NtTerminateProcess(NtCurrentProcess(), rcExit);
74}
75
76
77DECLASM(void) CustomMainEntrypoint(PPEB pPeb)
78{
79 /*
80 * Initialize stuff.
81 */
82 rtVccInitSecurityCookie();
83 InitStdHandles(pPeb->ProcessParameters);
84 rtVccWinInitProcExecPath();
85
86 RTEXITCODE rcExit;
87#ifdef IPRT_NO_CRT
88 AssertCompile(sizeof(rcExit) == sizeof(int));
89 rcExit = (RTEXITCODE)rtVccInitializersRunInit();
90 if (rcExit == RTEXITCODE_SUCCESS)
91#endif
92 {
93 /*
94 * Get and convert the command line to argc/argv format.
95 */
96 UNICODE_STRING const *pCmdLine = pPeb->ProcessParameters ? &pPeb->ProcessParameters->CommandLine : NULL;
97 if (pCmdLine)
98 {
99 char *pszCmdLine = NULL;
100 int rc = RTUtf16ToUtf8Ex(pCmdLine->Buffer, pCmdLine->Length / sizeof(WCHAR), &pszCmdLine, 0, NULL);
101 if (RT_SUCCESS(rc))
102 {
103 char **papszArgv;
104 int cArgs = 0;
105 rc = RTGetOptArgvFromString(&papszArgv, &cArgs, pszCmdLine,
106 RTGETOPTARGV_CNV_MODIFY_INPUT | RTGETOPTARGV_CNV_QUOTE_MS_CRT, NULL);
107 if (RT_SUCCESS(rc))
108 {
109 /*
110 * Call the main function.
111 */
112 AssertCompile(sizeof(rcExit) == sizeof(int));
113 rcExit = (RTEXITCODE)main(cArgs, papszArgv, NULL /*envp*/);
114 }
115 else
116 rcExit = RTMsgErrorExitFailure("Error parsing command line: %Rrc\n", rc);
117 }
118 else
119 rcExit = RTMsgErrorExitFailure("Failed to convert command line to UTF-8: %Rrc\n", rc);
120 }
121 else
122 rcExit = RTMsgErrorExitFailure("No command line\n");
123 rtTerminateProcess(rcExit, true /*fDoAtExit*/);
124 }
125#ifdef IPRT_NO_CRT
126 else
127 {
128 RTMsgError("A C static initializor failed (%d)\n", rcExit);
129 rtTerminateProcess(rcExit, false /*fDoAtExit*/);
130 }
131#endif
132}
133
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