@joebigelow / wix / commits / 78b5daf0

Move LoadSystemLibrary and LoadSystemLibraryWithPath into apputil.

Sean Hall committed Jun 24, 2022 at 12:26 UTC 78b5daf01555d86293c1012ed3de704752880a6a
7 files changed +137 -105
src/libs/dutil/WixToolset.DUtil/app2util.cpp new
+60
@@ -0,0 +1,60 @@
1 +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2 +
3 +#include "precomp.h"
4 +
5 +// Exit macros
6 +#define AppExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
7 +#define AppExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
8 +#define AppExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
9 +#define AppExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
10 +#define AppExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
11 +#define AppExitWithRootFailure(x, e, s, ...) ExitWithRootFailureSource(DUTIL_SOURCE_APPUTIL, x, e, s, __VA_ARGS__)
12 +#define AppExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
13 +#define AppExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_APPUTIL, p, x, e, s, __VA_ARGS__)
14 +#define AppExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_APPUTIL, p, x, s, __VA_ARGS__)
15 +#define AppExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_APPUTIL, p, x, e, s, __VA_ARGS__)
16 +#define AppExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_APPUTIL, p, x, s, __VA_ARGS__)
17 +#define AppExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_APPUTIL, e, x, s, __VA_ARGS__)
18 +#define AppExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_APPUTIL, g, x, s, __VA_ARGS__)
19 +
20 +DAPI_(void) AppFreeCommandLineArgs(
21 + __in LPWSTR* argv
22 + )
23 +{
24 + // The "ignored" hack in AppParseCommandLine requires an adjustment.
25 + LPWSTR* argvOriginal = argv - 1;
26 + ::LocalFree(argvOriginal);
27 +}
28 +
29 +DAPI_(HRESULT) AppParseCommandLine(
30 + __in LPCWSTR wzCommandLine,
31 + __in int* pArgc,
32 + __in LPWSTR** pArgv
33 + )
34 +{
35 + HRESULT hr = S_OK;
36 + LPWSTR sczCommandLine = NULL;
37 + LPWSTR* argv = NULL;
38 + int argc = 0;
39 +
40 + // CommandLineToArgvW tries to treat the first argument as the path to the process,
41 + // which fails pretty miserably if your first argument is something like
42 + // FOO="C:\Program Files\My Company". So give it something harmless to play with.
43 + hr = StrAllocConcat(&sczCommandLine, L"ignored ", 0);
44 + AppExitOnFailure(hr, "Failed to initialize command line.");
45 +
46 + hr = StrAllocConcat(&sczCommandLine, wzCommandLine, 0);
47 + AppExitOnFailure(hr, "Failed to copy command line.");
48 +
49 + argv = ::CommandLineToArgvW(sczCommandLine, &argc);
50 + AppExitOnNullWithLastError(argv, hr, "Failed to parse command line.");
51 +
52 + // Skip "ignored" argument/hack.
53 + *pArgv = argv + 1;
54 + *pArgc = argc - 1;
55 +
56 +LExit:
57 + ReleaseStr(sczCommandLine);
58 +
59 + return hr;
60 +}
src/libs/dutil/WixToolset.DUtil/apputil.cpp
+40 -42
@@ -8,6 +8,7 @@
8 #define AppExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
9 #define AppExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
10 #define AppExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
11 +#define AppExitWithRootFailure(x, e, s, ...) ExitWithRootFailureSource(DUTIL_SOURCE_APPUTIL, x, e, s, __VA_ARGS__)
12 #define AppExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
13 #define AppExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_APPUTIL, p, x, e, s, __VA_ARGS__)
14 #define AppExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_APPUTIL, p, x, s, __VA_ARGS__)
@@ -31,20 +32,50 @@ static HRESULT EscapeCommandLineArgument(
32 __out_z LPWSTR* psczEscaped
33 );
34
34 -DAPI_(void) AppFreeCommandLineArgs(
35 - __in LPWSTR* argv
35 +DAPI_(HRESULT) LoadSystemLibrary(
36 + __in_z LPCWSTR wzModuleName,
37 + __out HMODULE* phModule
38 )
39 {
38 - // The "ignored" hack in AppParseCommandLine requires an adjustment.
39 - LPWSTR* argvOriginal = argv - 1;
40 - ::LocalFree(argvOriginal);
40 + HRESULT hr = LoadSystemLibraryWithPath(wzModuleName, phModule, NULL);
41 + return hr;
42 }
43
43 -/********************************************************************
44 -AppInitialize - initializes the standard safety precautions for an
45 - installation application.
44 +DAPI_(HRESULT) LoadSystemLibraryWithPath(
45 + __in_z LPCWSTR wzModuleName,
46 + __out HMODULE* phModule,
47 + __deref_out_z_opt LPWSTR* psczPath
48 + )
49 +{
50 + HRESULT hr = S_OK;
51 + DWORD cch = 0;
52 + WCHAR wzPath[MAX_PATH] = { };
53 +
54 + cch = ::GetSystemDirectoryW(wzPath, MAX_PATH);
55 + AppExitOnNullWithLastError(cch, hr, "Failed to get the Windows system directory.");
56 +
57 + if (L'\\' != wzPath[cch - 1])
58 + {
59 + hr = ::StringCchCatNW(wzPath, MAX_PATH, L"\\", 1);
60 + AppExitOnRootFailure(hr, "Failed to terminate the string with a backslash.");
61 + }
62 +
63 + hr = ::StringCchCatW(wzPath, MAX_PATH, wzModuleName);
64 + AppExitOnRootFailure(hr, "Failed to create the fully-qualified path to %ls.", wzModuleName);
65 +
66 + *phModule = ::LoadLibraryW(wzPath);
67 + AppExitOnNullWithLastError(*phModule, hr, "Failed to load the library %ls.", wzModuleName);
68 +
69 + if (psczPath)
70 + {
71 + hr = StrAllocString(psczPath, wzPath, MAX_PATH);
72 + AppExitOnFailure(hr, "Failed to copy the path to library.");
73 + }
74 +
75 +LExit:
76 + return hr;
77 +}
78
47 -********************************************************************/
79 DAPI_(void) AppInitialize(
80 __in_ecount(cSafelyLoadSystemDlls) LPCWSTR rgsczSafelyLoadSystemDlls[],
81 __in DWORD cSafelyLoadSystemDlls
@@ -101,39 +132,6 @@ DAPI_(void) AppInitializeUnsafe()
132 ::HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
133 }
134
104 -DAPI_(HRESULT) AppParseCommandLine(
105 - __in LPCWSTR wzCommandLine,
106 - __in int* pArgc,
107 - __in LPWSTR** pArgv
108 - )
109 -{
110 - HRESULT hr = S_OK;
111 - LPWSTR sczCommandLine = NULL;
112 - LPWSTR* argv = NULL;
113 - int argc = 0;
114 -
115 - // CommandLineToArgvW tries to treat the first argument as the path to the process,
116 - // which fails pretty miserably if your first argument is something like
117 - // FOO="C:\Program Files\My Company". So give it something harmless to play with.
118 - hr = StrAllocConcat(&sczCommandLine, L"ignored ", 0);
119 - AppExitOnFailure(hr, "Failed to initialize command line.");
120 -
121 - hr = StrAllocConcat(&sczCommandLine, wzCommandLine, 0);
122 - AppExitOnFailure(hr, "Failed to copy command line.");
123 -
124 - argv = ::CommandLineToArgvW(sczCommandLine, &argc);
125 - AppExitOnNullWithLastError(argv, hr, "Failed to parse command line.");
126 -
127 - // Skip "ignored" argument/hack.
128 - *pArgv = argv + 1;
129 - *pArgc = argc - 1;
130 -
131 -LExit:
132 - ReleaseStr(sczCommandLine);
133 -
134 - return hr;
135 -}
136 -
135 DAPI_(HRESULT) AppAppendCommandLineArgument(
136 __deref_inout_z LPWSTR* psczCommandLine,
137 __in_z LPCWSTR wzArgument
src/libs/dutil/WixToolset.DUtil/dutil.cpp
-61
@@ -486,64 +486,3 @@ extern "C" void DAPI Dutil_RootFailure(
486
487 TraceError(hrError, "Root failure at %s:%d", szFile, iLine);
488 }
489 -
490 -/*******************************************************************
491 - LoadSystemLibrary - Fully qualifies the path to a module in the
492 - Windows system directory and loads it.
493 -
494 - Returns
495 - E_MODNOTFOUND - The module could not be found.
496 - * - Another error occured.
497 -********************************************************************/
498 -extern "C" HRESULT DAPI LoadSystemLibrary(
499 - __in_z LPCWSTR wzModuleName,
500 - __out HMODULE *phModule
501 - )
502 -{
503 - HRESULT hr = LoadSystemLibraryWithPath(wzModuleName, phModule, NULL);
504 - return hr;
505 -}
506 -
507 -/*******************************************************************
508 - LoadSystemLibraryWithPath - Fully qualifies the path to a module in
509 - the Windows system directory and loads it
510 - and returns the path
511 -
512 - Returns
513 - E_MODNOTFOUND - The module could not be found.
514 - * - Another error occured.
515 -********************************************************************/
516 -extern "C" HRESULT DAPI LoadSystemLibraryWithPath(
517 - __in_z LPCWSTR wzModuleName,
518 - __out HMODULE *phModule,
519 - __deref_out_z_opt LPWSTR* psczPath
520 - )
521 -{
522 - HRESULT hr = S_OK;
523 - DWORD cch = 0;
524 - WCHAR wzPath[MAX_PATH] = { };
525 -
526 - cch = ::GetSystemDirectoryW(wzPath, MAX_PATH);
527 - DExitOnNullWithLastError(cch, hr, "Failed to get the Windows system directory.");
528 -
529 - if (L'\\' != wzPath[cch - 1])
530 - {
531 - hr = ::StringCchCatNW(wzPath, MAX_PATH, L"\\", 1);
532 - DExitOnRootFailure(hr, "Failed to terminate the string with a backslash.");
533 - }
534 -
535 - hr = ::StringCchCatW(wzPath, MAX_PATH, wzModuleName);
536 - DExitOnRootFailure(hr, "Failed to create the fully-qualified path to %ls.", wzModuleName);
537 -
538 - *phModule = ::LoadLibraryW(wzPath);
539 - DExitOnNullWithLastError(*phModule, hr, "Failed to load the library %ls.", wzModuleName);
540 -
541 - if (psczPath)
542 - {
543 - hr = StrAllocString(psczPath, wzPath, MAX_PATH);
544 - DExitOnFailure(hr, "Failed to copy the path to library.");
545 - }
546 -
547 -LExit:
548 - return hr;
549 -}
src/libs/dutil/WixToolset.DUtil/dutil.vcxproj
+1
@@ -46,6 +46,7 @@
46 <ItemGroup>
47 <ClCompile Include="acl2util.cpp" />
48 <ClCompile Include="aclutil.cpp" />
49 + <ClCompile Include="app2util.cpp" />
50 <ClCompile Include="apputil.cpp" />
51 <ClCompile Include="apuputil.cpp" />
52 <ClCompile Include="atomutil.cpp" />
src/libs/dutil/WixToolset.DUtil/dutil.vcxproj.filters
+3
@@ -21,6 +21,9 @@
21 <ClCompile Include="aclutil.cpp">
22 <Filter>Source Files</Filter>
23 </ClCompile>
24 + <ClCompile Include="app2util.cpp">
25 + <Filter>Source Files</Filter>
26 + </ClCompile>
27 <ClCompile Include="apputil.cpp">
28 <Filter>Source Files</Filter>
29 </ClCompile>
src/libs/dutil/WixToolset.DUtil/inc/apputil.h
+5
@@ -16,6 +16,11 @@ void DAPI AppFreeCommandLineArgs(
16 __in LPWSTR* argv
17 );
18
19 +/********************************************************************
20 +AppInitialize - initializes the standard safety precautions for an
21 + installation application.
22 +
23 +********************************************************************/
24 void DAPI AppInitialize(
25 __in_ecount(cSafelyLoadSystemDlls) LPCWSTR rgsczSafelyLoadSystemDlls[],
26 __in DWORD cSafelyLoadSystemDlls
src/libs/dutil/WixToolset.DUtil/inc/dutil.h
+28 -2
@@ -205,8 +205,34 @@ extern "C" {
205 #endif
206
207 // other functions
208 -HRESULT DAPI LoadSystemLibrary(__in_z LPCWSTR wzModuleName, __out HMODULE *phModule);
209 -HRESULT DAPI LoadSystemLibraryWithPath(__in_z LPCWSTR wzModuleName, __out HMODULE *phModule, __deref_out_z_opt LPWSTR* psczPath);
208 +
209 +/*******************************************************************
210 + LoadSystemLibrary - Fully qualifies the path to a module in the
211 + Windows system directory and loads it.
212 +
213 + Returns
214 + E_MODNOTFOUND - The module could not be found.
215 + * - Another error occured.
216 +********************************************************************/
217 +HRESULT DAPI LoadSystemLibrary(
218 + __in_z LPCWSTR wzModuleName,
219 + __out HMODULE* phModule
220 + );
221 +
222 +/*******************************************************************
223 + LoadSystemLibraryWithPath - Fully qualifies the path to a module in
224 + the Windows system directory and loads it
225 + and returns the path
226 +
227 + Returns
228 + E_MODNOTFOUND - The module could not be found.
229 + * - Another error occured.
230 +********************************************************************/
231 +HRESULT DAPI LoadSystemLibraryWithPath(
232 + __in_z LPCWSTR wzModuleName,
233 + __out HMODULE* phModule,
234 + __deref_out_z_opt LPWSTR* psczPath
235 + );
236
237 #ifdef __cplusplus
238 }