VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/include/NoCrtOutput.h@ 96686

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

Add/NT/Inst: Merged the NT4 video driver helper executable VBoxGuestDrvInst.exe into VBoxDrvInst.exe, it only had one function and one structure we need to be careful with. bugref:10261

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.1 KB
Line 
1/* $Id: NoCrtOutput.h 96605 2022-09-05 19:08:21Z vboxsync $ */
2/** @file
3 * NoCrtOutput - ErrorMsgXxx and PrintXxx functions for small EXEs.
4 */
5
6/*
7 * Copyright (C) 2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.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#ifndef GA_INCLUDED_WINNT_NoCrtOutput_h
29#define GA_INCLUDED_WINNT_NoCrtOutput_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34#include <iprt/win/windows.h>
35#include <iprt/string.h>
36#include <iprt/utf16.h>
37
38
39/** @name Output helpers
40 *
41 * The general ASSUMPTION here is that all strings are restricted to 7-bit
42 * ASCII, with the exception of wchar_t ones.
43 *
44 * @note We don't use printf, RTPrintf or similar not for masochistic reasons
45 * but to keep the binary small and make it easier to switch between CRT
46 * and IPRT w/ no-CRT.
47 *
48 * @{
49 */
50
51DECLINLINE(void) OutputWStr(HANDLE hDst, const wchar_t *pwszStr)
52{
53 DWORD cbIgn;
54 if (GetConsoleMode(hDst, &cbIgn))
55 WriteConsoleW(hDst, pwszStr, (DWORD)RTUtf16Len(pwszStr), &cbIgn, NULL);
56 else
57 {
58 char *pszTmp;
59 int rc = RTUtf16ToUtf8(pwszStr, &pszTmp);
60 if (RT_SUCCESS(rc))
61 {
62 char *pszInCodepage;
63 rc = RTStrUtf8ToCurrentCP(&pszInCodepage, pszTmp);
64 if (RT_SUCCESS(rc))
65 {
66 WriteFile(hDst, pszInCodepage, (DWORD)strlen(pszInCodepage), &cbIgn, NULL);
67 RTStrFree(pszInCodepage);
68 }
69 else
70 WriteFile(hDst, RT_STR_TUPLE("<RTStrUtf8ToCurrentCP error>"), &cbIgn, NULL);
71 RTStrFree(pszTmp);
72 }
73 else
74 WriteFile(hDst, RT_STR_TUPLE("<RTUtf16ToUtf8 error>"), &cbIgn, NULL);
75 }
76}
77
78
79DECLINLINE(void) ErrorMsgBegin(const char *pszMsg)
80{
81 HANDLE const hStdErr = GetStdHandle(STD_ERROR_HANDLE);
82 DWORD cbIgn;
83 WriteFile(hStdErr, RT_STR_TUPLE("error: "), &cbIgn, NULL);
84 WriteFile(hStdErr, pszMsg, (DWORD)strlen(pszMsg), &cbIgn, NULL);
85}
86
87
88DECLINLINE(void) ErrorMsgStr(const char *pszMsg)
89{
90 HANDLE const hStdErr = GetStdHandle(STD_ERROR_HANDLE);
91 DWORD cbIgn;
92 WriteFile(hStdErr, pszMsg, (DWORD)strlen(pszMsg), &cbIgn, NULL);
93}
94
95
96DECLINLINE(void) ErrorMsgWStr(const wchar_t *pwszStr)
97{
98 OutputWStr(GetStdHandle(STD_ERROR_HANDLE), pwszStr);
99}
100
101
102DECLINLINE(int) ErrorMsgEnd(const char *pszMsg)
103{
104 HANDLE const hStdErr = GetStdHandle(STD_ERROR_HANDLE);
105 DWORD cbIgn;
106 if (pszMsg)
107 WriteFile(hStdErr, pszMsg, (DWORD)strlen(pszMsg), &cbIgn, NULL);
108 WriteFile(hStdErr, RT_STR_TUPLE("\r\n"), &cbIgn, NULL);
109#ifdef EXIT_FAIL /* VBoxDrvInst.cpp speciality */
110 return EXIT_FAIL;
111#else
112 return RTEXITCODE_FAILURE;
113#endif
114}
115
116
117DECLINLINE(void) ErrorMsgU64(uint64_t uValue, bool fSigned = false)
118{
119 char szVal[128];
120 RTStrFormatU64(szVal, sizeof(szVal), uValue, 10, 0, 0, fSigned ? RTSTR_F_VALSIGNED : 0);
121 ErrorMsgStr(szVal);
122}
123
124
125DECLINLINE(int) ErrorMsg(const char *pszMsg)
126{
127 ErrorMsgBegin(pszMsg);
128 return ErrorMsgEnd(NULL);
129}
130
131
132DECLINLINE(int) ErrorMsgSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3)
133{
134 ErrorMsgBegin(pszMsg1);
135 ErrorMsgWStr(pwszMsg2);
136 return ErrorMsgEnd(pszMsg3);
137}
138
139
140DECLINLINE(int) ErrorMsgSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
141 const wchar_t *pwszMsg4, const char *pszMsg5)
142{
143 ErrorMsgBegin(pszMsg1);
144 ErrorMsgWStr(pwszMsg2);
145 ErrorMsgStr(pszMsg3);
146 ErrorMsgWStr(pwszMsg4);
147 return ErrorMsgEnd(pszMsg5);
148}
149
150
151DECLINLINE(int) ErrorMsgSUSUS(const char *pszMsg1, uint64_t uValue1, const char *pszMsg2, uint64_t uValue2, const char *pszMsg3)
152{
153 ErrorMsgBegin(pszMsg1);
154 ErrorMsgU64(uValue1);
155 ErrorMsgStr(pszMsg2);
156 ErrorMsgU64(uValue2);
157 return ErrorMsgEnd(pszMsg3);
158}
159
160
161DECLINLINE(void) ErrorMsgErrVal(uint32_t uErrVal, bool fSigned)
162{
163 char szVal[128];
164 ssize_t cchVal = RTStrFormatU32(szVal, sizeof(szVal) - 1, uErrVal, 10, 0, 0, fSigned ? RTSTR_F_VALSIGNED : 0);
165 szVal[cchVal++] = '/';
166 szVal[cchVal] = '\0';
167 ErrorMsgStr(szVal);
168
169 RTStrFormatU32(szVal, sizeof(szVal) - 1, uErrVal, 16, 0, 0, RTSTR_F_SPECIAL);
170 ErrorMsgStr(szVal);
171}
172
173
174DECLINLINE(int) ErrorMsgErr(const char *pszMsg, uint32_t uErrVal, const char *pszErrIntro, bool fSigned)
175{
176 ErrorMsgBegin(pszMsg);
177 ErrorMsgStr(pszErrIntro);
178 ErrorMsgErrVal(uErrVal, fSigned);
179 return ErrorMsgEnd(")");
180}
181
182
183DECLINLINE(int) ErrorMsgRc(int rcExit, const char *pszMsg)
184{
185 ErrorMsgBegin(pszMsg);
186 ErrorMsgEnd(NULL);
187 return rcExit;
188}
189
190
191DECLINLINE(int) ErrorMsgRcSUS(int rcExit, const char *pszMsg1, uint64_t uValue, const char *pszMsg2)
192{
193 ErrorMsgBegin(pszMsg1);
194 ErrorMsgU64(uValue);
195 ErrorMsgEnd(pszMsg2);
196 return rcExit;
197}
198
199
200DECLINLINE(int) ErrorMsgVBoxErr(const char *pszMsg, int rc)
201{
202 return ErrorMsgErr(pszMsg, rc, " (", true);
203}
204
205
206DECLINLINE(int) ErrorMsgLastErr(const char *pszMsg)
207{
208 return ErrorMsgErr(pszMsg, GetLastError(), " (last error ", false);
209}
210
211
212DECLINLINE(int) ErrorMsgLastErrSUR(const char *pszMsg1, uint64_t uValue)
213{
214 DWORD dwErr = GetLastError();
215 ErrorMsgBegin(pszMsg1);
216 ErrorMsgU64(uValue);
217 ErrorMsgStr(" (last error ");
218 ErrorMsgErrVal(dwErr, false);
219 return ErrorMsgEnd(")");
220}
221
222
223DECLINLINE(int) ErrorMsgLastErrSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3)
224{
225 DWORD dwErr = GetLastError();
226 ErrorMsgBegin(pszMsg1);
227 ErrorMsgWStr(pwszMsg2);
228 ErrorMsgStr(pszMsg3);
229 ErrorMsgStr(" (last error ");
230 ErrorMsgErrVal(dwErr, false);
231 return ErrorMsgEnd(")");
232}
233
234
235DECLINLINE(int) ErrorMsgLastErrSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
236 const wchar_t *pwszMsg4, const char *pszMsg5)
237{
238 DWORD dwErr = GetLastError();
239 ErrorMsgBegin(pszMsg1);
240 ErrorMsgWStr(pwszMsg2);
241 ErrorMsgStr(pszMsg3);
242 ErrorMsgWStr(pwszMsg4);
243 ErrorMsgStr(pszMsg5);
244 ErrorMsgStr(" (last error ");
245 ErrorMsgErrVal(dwErr, false);
246 return ErrorMsgEnd(")");
247}
248
249
250DECLINLINE(int) ErrorMsgLastErrSWSRSUS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const char *pszMsg4,
251 uint64_t uValue, const char *pszMsg5)
252{
253 DWORD dwErr = GetLastError();
254 ErrorMsgBegin(pszMsg1);
255 ErrorMsgWStr(pwszMsg2);
256 ErrorMsgStr(pszMsg3);
257 ErrorMsgStr(" (last error ");
258 ErrorMsgErrVal(dwErr, false);
259 ErrorMsgStr(")");
260 ErrorMsgStr(pszMsg4);
261 ErrorMsgU64(uValue);
262 return ErrorMsgEnd(pszMsg5);
263}
264
265
266DECLINLINE(int) ErrorMsgLastErrSSS(const char *pszMsg1, const char *pszMsg2, const char *pszMsg3)
267{
268 DWORD dwErr = GetLastError();
269 ErrorMsgBegin(pszMsg1);
270 ErrorMsgStr(pszMsg2);
271 ErrorMsgStr(pszMsg3);
272 ErrorMsgStr(" (last error ");
273 ErrorMsgErrVal(dwErr, false);
274 return ErrorMsgEnd(")");
275}
276
277
278DECLINLINE(int) ErrorMsgRcLastErr(int rcExit, const char *pszMsg)
279{
280 ErrorMsgErr(pszMsg, GetLastError(), " (last error ", false);
281 return rcExit;
282}
283
284
285DECLINLINE(int) ErrorMsgRcLastErrSUR(int rcExit, const char *pszMsg1, uint64_t uValue)
286{
287 ErrorMsgLastErrSUR(pszMsg1, uValue);
288 return rcExit;
289}
290
291
292static int ErrorMsgRcLastErrSWSR(int rcExit, const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3)
293{
294 DWORD dwErr = GetLastError();
295 ErrorMsgBegin(pszMsg1);
296 ErrorMsgWStr(pwszMsg2);
297 ErrorMsgStr(pszMsg3);
298 ErrorMsgStr(" (last error ");
299 ErrorMsgErrVal(dwErr, false);
300 ErrorMsgEnd(")");
301 return rcExit;
302}
303
304
305
306DECLINLINE(int) ErrorMsgLStatus(const char *pszMsg, LSTATUS lrc)
307{
308 return ErrorMsgErr(pszMsg, (DWORD)lrc, " (", true);
309}
310
311
312DECLINLINE(int) ErrorMsgLStatusSWSRS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
313 LSTATUS lrc, const char *pszMsg4)
314{
315 ErrorMsgBegin(pszMsg1);
316 ErrorMsgWStr(pwszMsg2);
317 ErrorMsgStr(pszMsg3);
318 ErrorMsgErrVal((DWORD)lrc, true);
319 return ErrorMsgEnd(pszMsg4);
320}
321
322
323DECLINLINE(int) ErrorMsgLStatusSWSWSRS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const wchar_t *pwszMsg4,
324 const char *pszMsg5, LSTATUS lrc, const char *pszMsg6)
325{
326 ErrorMsgBegin(pszMsg1);
327 ErrorMsgWStr(pwszMsg2);
328 ErrorMsgStr(pszMsg3);
329 ErrorMsgWStr(pwszMsg4);
330 ErrorMsgStr(pszMsg5);
331 ErrorMsgErrVal((DWORD)lrc, true);
332 return ErrorMsgEnd(pszMsg6);
333}
334
335
336DECLINLINE(int) ErrorMsgLStatusSWSWSWSRS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
337 const wchar_t *pwszMsg4, const char *pszMsg5, const wchar_t *pwszMsg6,
338 const char *pszMsg7, LSTATUS lrc, const char *pszMsg8)
339{
340 ErrorMsgBegin(pszMsg1);
341 ErrorMsgWStr(pwszMsg2);
342 ErrorMsgStr(pszMsg3);
343 ErrorMsgWStr(pwszMsg4);
344 ErrorMsgStr(pszMsg5);
345 ErrorMsgWStr(pwszMsg6);
346 ErrorMsgStr(pszMsg7);
347 ErrorMsgErrVal((DWORD)lrc, true);
348 return ErrorMsgEnd(pszMsg8);
349}
350
351
352DECLINLINE(int) ErrorMsgLStatusSWSWSWSWSRS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
353 const wchar_t *pwszMsg4, const char *pszMsg5, const wchar_t *pwszMsg6,
354 const char *pszMsg7, const wchar_t *pwszMsg8, const char *pszMsg9, LSTATUS lrc,
355 const char *pszMsg10)
356{
357 ErrorMsgBegin(pszMsg1);
358 ErrorMsgWStr(pwszMsg2);
359 ErrorMsgStr(pszMsg3);
360 ErrorMsgWStr(pwszMsg4);
361 ErrorMsgStr(pszMsg5);
362 ErrorMsgWStr(pwszMsg6);
363 ErrorMsgStr(pszMsg7);
364 ErrorMsgWStr(pwszMsg8);
365 ErrorMsgStr(pszMsg9);
366 ErrorMsgErrVal((DWORD)lrc, true);
367 return ErrorMsgEnd(pszMsg10);
368}
369
370
371DECLINLINE(int) ErrorBadArg(const char *pszName, wchar_t const *pwszArg, const char *pszValues = NULL)
372{
373 ErrorMsgBegin("Bad argument '");
374 ErrorMsgStr(pszName);
375 ErrorMsgStr("': ");
376 ErrorMsgWStr(pwszArg);
377 if (pszValues)
378 ErrorMsgStr(", expected: ");
379 return ErrorMsgEnd(pszValues);
380}
381
382
383/** Simple fputs(stdout) replacement. */
384DECLINLINE(void) PrintStr(const char *pszMsg)
385{
386 HANDLE const hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
387 DWORD cbIgn;
388 WriteFile(hStdOut, pszMsg, (DWORD)strlen(pszMsg), &cbIgn, NULL);
389}
390
391
392/** Simple fputs(stdout) replacement. */
393DECLINLINE(void) PrintWStr(const wchar_t *pwszStr)
394{
395 OutputWStr(GetStdHandle(STD_OUTPUT_HANDLE), pwszStr);
396}
397
398
399DECLINLINE(void) PrintX64(uint64_t uValue)
400{
401 char szVal[128];
402 RTStrFormatU64(szVal, sizeof(szVal), uValue, 16, 0, 0, RTSTR_F_64BIT | RTSTR_F_SPECIAL);
403 PrintStr(szVal);
404}
405
406
407DECLINLINE(void) PrintSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3)
408{
409 PrintStr(pszMsg1);
410 PrintWStr(pwszMsg2);
411 PrintStr(pszMsg3);
412}
413
414
415DECLINLINE(void) PrintSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
416 const wchar_t *pwszMsg4, const char *pszMsg5)
417{
418 PrintStr(pszMsg1);
419 PrintWStr(pwszMsg2);
420 PrintStr(pszMsg3);
421 PrintWStr(pwszMsg4);
422 PrintStr(pszMsg5);
423}
424
425
426DECLINLINE(void) PrintSWSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const wchar_t *pwszMsg4,
427 const char *pszMsg5, const wchar_t *pwszMsg6, const char *pszMsg7)
428{
429 PrintStr(pszMsg1);
430 PrintWStr(pwszMsg2);
431 PrintStr(pszMsg3);
432 PrintWStr(pwszMsg4);
433 PrintStr(pszMsg5);
434 PrintWStr(pwszMsg6);
435 PrintStr(pszMsg7);
436}
437
438
439DECLINLINE(void) PrintSWSWSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const wchar_t *pwszMsg4,
440 const char *pszMsg5, const wchar_t *pwszMsg6, const char *pszMsg7, const wchar_t *pwszMsg8,
441 const char *pszMsg9)
442{
443 PrintStr(pszMsg1);
444 PrintWStr(pwszMsg2);
445 PrintStr(pszMsg3);
446 PrintWStr(pwszMsg4);
447 PrintStr(pszMsg5);
448 PrintWStr(pwszMsg6);
449 PrintStr(pszMsg7);
450 PrintWStr(pwszMsg8);
451 PrintStr(pszMsg9);
452}
453
454
455DECLINLINE(void) PrintSXS(const char *pszMsg1, uint64_t uValue, const char *pszMsg2)
456{
457 PrintStr(pszMsg1);
458 PrintX64(uValue);
459 PrintStr(pszMsg2);
460}
461
462
463DECLINLINE(void) PrintSWSWSWSXS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const wchar_t *pwszMsg4,
464 const char *pszMsg5, const wchar_t *pwszMsg6, const char *pszMsg7, uint64_t uValue,
465 const char *pszMsg8)
466{
467 PrintStr(pszMsg1);
468 PrintWStr(pwszMsg2);
469 PrintStr(pszMsg3);
470 PrintWStr(pwszMsg4);
471 PrintStr(pszMsg5);
472 PrintWStr(pwszMsg6);
473 PrintStr(pszMsg7);
474 PrintX64(uValue);
475 PrintStr(pszMsg8);
476}
477
478/** @} */
479
480#endif /* !GA_INCLUDED_WINNT_NoCrtOutput_h */
481
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