1 | /* $Id: nocrt-startup-exe-win.cpp 93115 2022-01-01 11:31:46Z 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 |
|
---|
44 | /*********************************************************************************************************************************
|
---|
45 | * Global Variables *
|
---|
46 | *********************************************************************************************************************************/
|
---|
47 | RT_C_DECLS_BEGIN
|
---|
48 | DECL_HIDDEN_DATA(char) g_szrtProcExePath[RTPATH_MAX] = "Unknown.exe";
|
---|
49 | DECL_HIDDEN_DATA(size_t) g_cchrtProcExePath = 11;
|
---|
50 | DECL_HIDDEN_DATA(size_t) g_cchrtProcExeDir = 0;
|
---|
51 | DECL_HIDDEN_DATA(size_t) g_offrtProcName = 0;
|
---|
52 | RT_C_DECLS_END
|
---|
53 |
|
---|
54 |
|
---|
55 | /*********************************************************************************************************************************
|
---|
56 | * External Symbols *
|
---|
57 | *********************************************************************************************************************************/
|
---|
58 | extern DECLHIDDEN(void) InitStdHandles(PRTL_USER_PROCESS_PARAMETERS pParams); /* nocrt-streams-win.cpp */ /** @todo put in header */
|
---|
59 |
|
---|
60 | extern int main(int argc, char **argv, char **envp); /* in program */
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | DECL_NO_INLINE(static, void) initProcExecPath(void)
|
---|
65 | {
|
---|
66 | WCHAR wszPath[RTPATH_MAX];
|
---|
67 | UINT cwcPath = GetModuleFileNameW(NULL, wszPath, RT_ELEMENTS(wszPath));
|
---|
68 | if (cwcPath)
|
---|
69 | {
|
---|
70 | char *pszDst = g_szrtProcExePath;
|
---|
71 | int rc = RTUtf16ToUtf8Ex(wszPath, cwcPath, &pszDst, sizeof(g_szrtProcExePath), &g_cchrtProcExePath);
|
---|
72 | if (RT_SUCCESS(rc))
|
---|
73 | {
|
---|
74 | g_cchrtProcExeDir = g_offrtProcName = RTPathFilename(pszDst) - g_szrtProcExePath;
|
---|
75 | while ( g_cchrtProcExeDir >= 2
|
---|
76 | && RTPATH_IS_SLASH(g_szrtProcExePath[g_cchrtProcExeDir - 1])
|
---|
77 | && g_szrtProcExePath[g_cchrtProcExeDir - 2] != ':')
|
---|
78 | g_cchrtProcExeDir--;
|
---|
79 | }
|
---|
80 | else
|
---|
81 | RTMsgErrorExitFailure("initProcExecPath: RTUtf16ToUtf8Ex failed: %Rrc\n", rc);
|
---|
82 | }
|
---|
83 | else
|
---|
84 | RTMsgErrorExitFailure("initProcExecPath: GetModuleFileNameW failed: %Rhrc\n", GetLastError());
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | void CustomMainEntrypoint(PPEB pPeb)
|
---|
89 | {
|
---|
90 | /*
|
---|
91 | * Initialize stuff.
|
---|
92 | */
|
---|
93 | InitStdHandles(pPeb->ProcessParameters);
|
---|
94 | initProcExecPath();
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Get and convert the command line to argc/argv format.
|
---|
98 | */
|
---|
99 | RTEXITCODE rcExit;
|
---|
100 | UNICODE_STRING const *pCmdLine = pPeb->ProcessParameters ? &pPeb->ProcessParameters->CommandLine : NULL;
|
---|
101 | if (pCmdLine)
|
---|
102 | {
|
---|
103 | char *pszCmdLine = NULL;
|
---|
104 | int rc = RTUtf16ToUtf8Ex(pCmdLine->Buffer, pCmdLine->Length / sizeof(WCHAR), &pszCmdLine, 0, NULL);
|
---|
105 | if (RT_SUCCESS(rc))
|
---|
106 | {
|
---|
107 | char **papszArgv;
|
---|
108 | int cArgs = 0;
|
---|
109 | rc = RTGetOptArgvFromString(&papszArgv, &cArgs, pszCmdLine,
|
---|
110 | RTGETOPTARGV_CNV_MODIFY_INPUT | RTGETOPTARGV_CNV_QUOTE_MS_CRT, NULL);
|
---|
111 | if (RT_SUCCESS(rc))
|
---|
112 | {
|
---|
113 | /*
|
---|
114 | * Call the main function.
|
---|
115 | */
|
---|
116 | rcExit = (RTEXITCODE)main(cArgs, papszArgv, NULL /*envp*/);
|
---|
117 | }
|
---|
118 | else
|
---|
119 | rcExit = RTMsgErrorExitFailure("Error parsing command line: %Rrc\n", rc);
|
---|
120 | }
|
---|
121 | else
|
---|
122 | rcExit = RTMsgErrorExitFailure("Failed to convert command line to UTF-8: %Rrc\n", rc);
|
---|
123 | }
|
---|
124 | else
|
---|
125 | rcExit = RTMsgErrorExitFailure("No command line\n");
|
---|
126 |
|
---|
127 | for (;;)
|
---|
128 | NtTerminateProcess(NtCurrentProcess(), rcExit);
|
---|
129 | }
|
---|
130 |
|
---|