aboutsummaryrefslogtreecommitdiff
path: root/src/libs/dutil/WixToolset.DUtil/apputil.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs/dutil/WixToolset.DUtil/apputil.cpp')
-rw-r--r--src/libs/dutil/WixToolset.DUtil/apputil.cpp124
1 files changed, 124 insertions, 0 deletions
diff --git a/src/libs/dutil/WixToolset.DUtil/apputil.cpp b/src/libs/dutil/WixToolset.DUtil/apputil.cpp
new file mode 100644
index 00000000..589a09dd
--- /dev/null
+++ b/src/libs/dutil/WixToolset.DUtil/apputil.cpp
@@ -0,0 +1,124 @@
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 AppExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__)
12#define AppExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_APPUTIL, p, x, e, s, __VA_ARGS__)
13#define AppExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_APPUTIL, p, x, s, __VA_ARGS__)
14#define AppExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_APPUTIL, p, x, e, s, __VA_ARGS__)
15#define AppExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_APPUTIL, p, x, s, __VA_ARGS__)
16#define AppExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_APPUTIL, e, x, s, __VA_ARGS__)
17#define AppExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_APPUTIL, g, x, s, __VA_ARGS__)
18
19const DWORD PRIVATE_LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800;
20typedef BOOL(WINAPI *LPFN_SETDEFAULTDLLDIRECTORIES)(DWORD);
21typedef BOOL(WINAPI *LPFN_SETDLLDIRECTORYW)(LPCWSTR);
22
23extern "C" void DAPI AppFreeCommandLineArgs(
24 __in LPWSTR* argv
25 )
26{
27 // The "ignored" hack in AppParseCommandLine requires an adjustment.
28 LPWSTR* argvOriginal = argv - 1;
29 ::LocalFree(argvOriginal);
30}
31
32/********************************************************************
33AppInitialize - initializes the standard safety precautions for an
34 installation application.
35
36********************************************************************/
37extern "C" void DAPI AppInitialize(
38 __in_ecount(cSafelyLoadSystemDlls) LPCWSTR rgsczSafelyLoadSystemDlls[],
39 __in DWORD cSafelyLoadSystemDlls
40 )
41{
42 HRESULT hr = S_OK;
43 HMODULE hIgnored = NULL;
44 BOOL fSetDefaultDllDirectories = FALSE;
45
46 ::HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
47
48 // Best effort call to initialize default DLL directories to system only.
49 HMODULE hKernel32 = ::GetModuleHandleW(L"kernel32");
50 Assert(hKernel32);
51 LPFN_SETDEFAULTDLLDIRECTORIES pfnSetDefaultDllDirectories = (LPFN_SETDEFAULTDLLDIRECTORIES)::GetProcAddress(hKernel32, "SetDefaultDllDirectories");
52 if (pfnSetDefaultDllDirectories)
53 {
54 if (pfnSetDefaultDllDirectories(PRIVATE_LOAD_LIBRARY_SEARCH_SYSTEM32))
55 {
56 fSetDefaultDllDirectories = TRUE;
57 }
58 else
59 {
60 hr = HRESULT_FROM_WIN32(::GetLastError());
61 TraceError(hr, "Failed to call SetDefaultDllDirectories.");
62 }
63 }
64
65 // Only need to safely load if the default DLL directories was not
66 // able to be set.
67 if (!fSetDefaultDllDirectories)
68 {
69 // Remove current working directory from search order.
70 LPFN_SETDLLDIRECTORYW pfnSetDllDirectory = (LPFN_SETDLLDIRECTORYW)::GetProcAddress(hKernel32, "SetDllDirectoryW");
71 if (!pfnSetDllDirectory || !pfnSetDllDirectory(L""))
72 {
73 hr = HRESULT_FROM_WIN32(::GetLastError());
74 TraceError(hr, "Failed to call SetDllDirectory.");
75 }
76
77 for (DWORD i = 0; i < cSafelyLoadSystemDlls; ++i)
78 {
79 hr = LoadSystemLibrary(rgsczSafelyLoadSystemDlls[i], &hIgnored);
80 if (FAILED(hr))
81 {
82 TraceError(hr, "Failed to safety load: %ls", rgsczSafelyLoadSystemDlls[i]);
83 }
84 }
85 }
86}
87
88extern "C" void DAPI AppInitializeUnsafe()
89{
90 ::HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
91}
92
93extern "C" DAPI_(HRESULT) AppParseCommandLine(
94 __in LPCWSTR wzCommandLine,
95 __in int* pArgc,
96 __in LPWSTR** pArgv
97 )
98{
99 HRESULT hr = S_OK;
100 LPWSTR sczCommandLine = NULL;
101 LPWSTR* argv = NULL;
102 int argc = 0;
103
104 // CommandLineToArgvW tries to treat the first argument as the path to the process,
105 // which fails pretty miserably if your first argument is something like
106 // FOO="C:\Program Files\My Company". So give it something harmless to play with.
107 hr = StrAllocConcat(&sczCommandLine, L"ignored ", 0);
108 AppExitOnFailure(hr, "Failed to initialize command line.");
109
110 hr = StrAllocConcat(&sczCommandLine, wzCommandLine, 0);
111 AppExitOnFailure(hr, "Failed to copy command line.");
112
113 argv = ::CommandLineToArgvW(sczCommandLine, &argc);
114 AppExitOnNullWithLastError(argv, hr, "Failed to parse command line.");
115
116 // Skip "ignored" argument/hack.
117 *pArgv = argv + 1;
118 *pArgc = argc - 1;
119
120LExit:
121 ReleaseStr(sczCommandLine);
122
123 return hr;
124}