VirtualBox

source: vbox/trunk/src/VBox/Installer/win/StubBld/VBoxStubBld.cpp@ 96407

最後變更 在這個檔案從96407是 96407,由 vboxsync 提交於 3 年 前

scm copyright and license note update

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 9.0 KB
 
1/* $Id: VBoxStubBld.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * VBoxStubBld - VirtualBox's Windows installer stub builder.
4 */
5
6/*
7 * Copyright (C) 2009-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.alldomusa.eu.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#include <iprt/win/windows.h>
33#include <shellapi.h>
34#include <strsafe.h>
35
36#include <VBox/version.h>
37#include <iprt/types.h>
38
39#include "VBoxStubBld.h"
40
41HRESULT GetFile (const char* pszFilePath,
42 HANDLE* phFile,
43 DWORD* pdwFileSize)
44{
45 HRESULT hr = S_OK;
46 *phFile = CreateFile(pszFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
47 if (INVALID_HANDLE_VALUE == *phFile)
48 hr = HRESULT_FROM_WIN32(GetLastError());
49 else
50 {
51 *pdwFileSize = ::GetFileSize(*phFile, NULL);
52 if (!*pdwFileSize)
53 hr = HRESULT_FROM_WIN32(GetLastError());
54 }
55 return hr;
56}
57
58HRESULT UpdateResource(HANDLE hFile,
59 DWORD dwFileSize,
60 HANDLE hResourceUpdate,
61 const char *pszResourceType,
62 const char *pszResourceId)
63{
64 HRESULT hr = S_OK;
65
66 HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
67 PVOID pvFile = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, dwFileSize);
68 if (!UpdateResourceA(hResourceUpdate, pszResourceType, pszResourceId,
69 MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), pvFile, dwFileSize))
70 hr = HRESULT_FROM_WIN32(GetLastError());
71
72 if (pvFile)
73 UnmapViewOfFile(pvFile);
74
75 if (hMap)
76 CloseHandle(hMap);
77
78 return hr;
79}
80
81static HRESULT IntegrateFile(HANDLE hResourceUpdate, const char *pszResourceType,
82 const char *pszResourceId, const char *pszFilePath)
83{
84 HANDLE hFile = INVALID_HANDLE_VALUE;
85 DWORD dwFileSize = 0;
86 HRESULT hr = GetFile(pszFilePath, &hFile, &dwFileSize);
87 if (SUCCEEDED(hr))
88 {
89 hr = UpdateResource(hFile, dwFileSize, hResourceUpdate, pszResourceType, pszResourceId);
90 if (FAILED(hr))
91 printf("ERROR: Error updating resource for file %s!", pszFilePath);
92 }
93 else
94 hr = HRESULT_FROM_WIN32(GetLastError());
95
96 if (hFile != INVALID_HANDLE_VALUE)
97 CloseHandle(hFile);
98 return hr;
99}
100
101static char *MyPathFilename(const char *pszPath)
102{
103 const char *pszName = pszPath;
104 for (const char *psz = pszPath;; psz++)
105 {
106 switch (*psz)
107 {
108 /* handle separators. */
109 case ':':
110 pszName = psz + 1;
111 break;
112
113 case '\\':
114 case '/':
115 pszName = psz + 1;
116 break;
117
118 /* the end */
119 case '\0':
120 if (*pszName)
121 return (char *)(void *)pszName;
122 return NULL;
123 }
124 }
125
126 /* will never get here */
127}
128
129
130int main(int argc, char* argv[])
131{
132 HRESULT hr = S_OK;
133 int rcExit = RTEXITCODE_SUCCESS;
134
135 char szSetupStub[_MAX_PATH] = {"VBoxStub.exe"};
136 char szOutput[_MAX_PATH] = {"VirtualBox-MultiArch.exe"};
137 HANDLE hUpdate = NULL;
138
139 do /* goto avoidance "loop" */
140 {
141 printf(VBOX_PRODUCT " Stub Builder v%d.%d.%d.%d\n",
142 VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
143
144 if (argc < 2)
145 printf("WARNING: No parameters given! Using default values!\n");
146
147 VBOXSTUBBUILDPKG stbBuildPkg[VBOXSTUB_MAX_PACKAGES] = {{{0}}};
148 VBOXSTUBPKG stbPkg[VBOXSTUB_MAX_PACKAGES] = {{0}};
149 VBOXSTUBPKGHEADER stbHeader =
150 {
151 "vbox$tub", /* File magic. */
152 1, /* Version. */
153 0 /* No files yet. */
154 };
155
156 for (int i=1; i<argc; i++)
157 {
158 if (!stricmp(argv[i], "-out") && argc > i+1)
159 {
160 hr = StringCchCopy(szOutput, _MAX_PATH, argv[i+1]);
161 i++;
162 }
163
164 else if (!stricmp(argv[i], "-stub") && argc > i+1)
165 {
166 hr = StringCchCopy(szSetupStub, _MAX_PATH, argv[i+1]);
167 i++;
168 }
169
170 else if (!stricmp(argv[i], "-target-all") && argc > i+1)
171 {
172 hr = StringCchCopy(stbBuildPkg[stbHeader.byCntPkgs].szSourcePath, _MAX_PATH, argv[i+1]);
173 stbBuildPkg[stbHeader.byCntPkgs].byArch = VBOXSTUBPKGARCH_ALL;
174 stbHeader.byCntPkgs++;
175 i++;
176 }
177
178 else if (!stricmp(argv[i], "-target-x86") && argc > i+1)
179 {
180 hr = StringCchCopy(stbBuildPkg[stbHeader.byCntPkgs].szSourcePath, _MAX_PATH, argv[i+1]);
181 stbBuildPkg[stbHeader.byCntPkgs].byArch = VBOXSTUBPKGARCH_X86;
182 stbHeader.byCntPkgs++;
183 i++;
184 }
185
186 else if (!stricmp(argv[i], "-target-amd64") && argc > i+1)
187 {
188 hr = StringCchCopy(stbBuildPkg[stbHeader.byCntPkgs].szSourcePath, _MAX_PATH, argv[i+1]);
189 stbBuildPkg[stbHeader.byCntPkgs].byArch = VBOXSTUBPKGARCH_AMD64;
190 stbHeader.byCntPkgs++;
191 i++;
192 }
193 else
194 {
195 printf("ERROR: Invalid parameter: %s\n", argv[i]);
196 hr = E_INVALIDARG;
197 break;
198 }
199 if (FAILED(hr))
200 {
201 printf("ERROR: StringCchCopy failed: %#lx\n", hr);
202 break;
203 }
204 }
205 if (FAILED(hr))
206 break;
207
208 if (stbHeader.byCntPkgs <= 0)
209 {
210 printf("ERROR: No packages defined! Exiting.\n");
211 break;
212 }
213
214 printf("Stub: %s\n", szSetupStub);
215 printf("Output: %s\n", szOutput);
216 printf("# Packages: %u\n", stbHeader.byCntPkgs);
217
218 if (!CopyFile(szSetupStub, szOutput, FALSE))
219 {
220 hr = HRESULT_FROM_WIN32(GetLastError());
221 printf("ERROR: Could not create stub loader: %#lx\n", hr);
222 break;
223 }
224
225 hUpdate = BeginUpdateResource(szOutput, FALSE);
226
227 PVBOXSTUBPKG pPackage = stbPkg;
228 char szHeaderName[_MAX_PATH] = {0};
229
230 for (BYTE i = 0; i < stbHeader.byCntPkgs; i++)
231 {
232 printf("Integrating (Platform %d): %s\n", stbBuildPkg[i].byArch, stbBuildPkg[i].szSourcePath);
233
234 /* Construct resource name. */
235 hr = StringCchPrintf(pPackage->szResourceName, _MAX_PATH, "BIN_%02d", i);
236 pPackage->byArch = stbBuildPkg[i].byArch;
237
238 /* Construct final name used when extracting. */
239 hr = StringCchCopy(pPackage->szFileName, _MAX_PATH, MyPathFilename(stbBuildPkg[i].szSourcePath));
240
241 /* Integrate header into binary. */
242 hr = StringCchPrintf(szHeaderName, _MAX_PATH, "HDR_%02d", i);
243 hr = UpdateResource(hUpdate, RT_RCDATA, szHeaderName, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), pPackage, sizeof(VBOXSTUBPKG));
244
245 /* Integrate file into binary. */
246 hr = IntegrateFile(hUpdate, RT_RCDATA, pPackage->szResourceName, stbBuildPkg[i].szSourcePath);
247 if (FAILED(hr))
248 {
249 printf("ERROR: Could not integrate binary %s (%s): %#lx\n",
250 pPackage->szResourceName, pPackage->szFileName, hr);
251 rcExit = RTEXITCODE_FAILURE;
252 }
253
254 pPackage++;
255 }
256
257 if (FAILED(hr))
258 break;
259
260 if (!UpdateResource(hUpdate, RT_RCDATA, "MANIFEST", MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), &stbHeader, sizeof(VBOXSTUBPKGHEADER)))
261 {
262 hr = HRESULT_FROM_WIN32(GetLastError());
263 break;
264 }
265
266 if (!EndUpdateResource(hUpdate, FALSE))
267 {
268 hr = HRESULT_FROM_WIN32(GetLastError());
269 break;
270 }
271
272 printf("Integration done!\n");
273
274 } while (0);
275
276 hUpdate = NULL;
277
278 if (FAILED(hr))
279 {
280 printf("ERROR: Building failed! Last error: %lu\n", GetLastError());
281 rcExit = RTEXITCODE_FAILURE;
282 }
283 return rcExit;
284}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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