aboutsummaryrefslogtreecommitdiff
path: root/src/dutil/apputil.cpp
diff options
context:
space:
mode:
authorRob Mensching <rob@firegiant.com>2017-09-03 11:22:38 -0700
committerRob Mensching <rob@firegiant.com>2017-09-03 13:33:33 -0700
commit5d8375007754101ff2889d0e79486c8f9b7cf5ab (patch)
treea76d6fb6a38dd9f04a93ffcfd9d64e76779b3414 /src/dutil/apputil.cpp
parent8e8da6dbc051ec884b5d439bb4f44dc027d05bbf (diff)
downloadwix-5d8375007754101ff2889d0e79486c8f9b7cf5ab.tar.gz
wix-5d8375007754101ff2889d0e79486c8f9b7cf5ab.tar.bz2
wix-5d8375007754101ff2889d0e79486c8f9b7cf5ab.zip
Initial commit
Diffstat (limited to 'src/dutil/apputil.cpp')
-rw-r--r--src/dutil/apputil.cpp109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/dutil/apputil.cpp b/src/dutil/apputil.cpp
new file mode 100644
index 00000000..8562a47a
--- /dev/null
+++ b/src/dutil/apputil.cpp
@@ -0,0 +1,109 @@
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
5const DWORD PRIVATE_LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800;
6typedef BOOL(WINAPI *LPFN_SETDEFAULTDLLDIRECTORIES)(DWORD);
7typedef BOOL(WINAPI *LPFN_SETDLLDIRECTORYW)(LPCWSTR);
8
9extern "C" void DAPI AppFreeCommandLineArgs(
10 __in LPWSTR* argv
11 )
12{
13 // The "ignored" hack in AppParseCommandLine requires an adjustment.
14 LPWSTR* argvOriginal = argv - 1;
15 ::LocalFree(argvOriginal);
16}
17
18/********************************************************************
19AppInitialize - initializes the standard safety precautions for an
20 installation application.
21
22********************************************************************/
23extern "C" void DAPI AppInitialize(
24 __in_ecount(cSafelyLoadSystemDlls) LPCWSTR rgsczSafelyLoadSystemDlls[],
25 __in DWORD cSafelyLoadSystemDlls
26 )
27{
28 HRESULT hr = S_OK;
29 HMODULE hIgnored = NULL;
30 BOOL fSetDefaultDllDirectories = FALSE;
31
32 ::HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
33
34 // Best effort call to initialize default DLL directories to system only.
35 HMODULE hKernel32 = ::GetModuleHandleW(L"kernel32");
36 LPFN_SETDEFAULTDLLDIRECTORIES pfnSetDefaultDllDirectories = (LPFN_SETDEFAULTDLLDIRECTORIES)::GetProcAddress(hKernel32, "SetDefaultDllDirectories");
37 if (pfnSetDefaultDllDirectories)
38 {
39 if (pfnSetDefaultDllDirectories(PRIVATE_LOAD_LIBRARY_SEARCH_SYSTEM32))
40 {
41 fSetDefaultDllDirectories = TRUE;
42 }
43 else
44 {
45 hr = HRESULT_FROM_WIN32(::GetLastError());
46 TraceError(hr, "Failed to call SetDefaultDllDirectories.");
47 }
48 }
49
50 // Only need to safely load if the default DLL directories was not
51 // able to be set.
52 if (!fSetDefaultDllDirectories)
53 {
54 // Remove current working directory from search order.
55 LPFN_SETDLLDIRECTORYW pfnSetDllDirectory = (LPFN_SETDLLDIRECTORYW)::GetProcAddress(hKernel32, "SetDllDirectoryW");
56 if (!pfnSetDllDirectory || !pfnSetDllDirectory(L""))
57 {
58 hr = HRESULT_FROM_WIN32(::GetLastError());
59 TraceError(hr, "Failed to call SetDllDirectory.");
60 }
61
62 for (DWORD i = 0; i < cSafelyLoadSystemDlls; ++i)
63 {
64 hr = LoadSystemLibrary(rgsczSafelyLoadSystemDlls[i], &hIgnored);
65 if (FAILED(hr))
66 {
67 TraceError(hr, "Failed to safety load: %ls", rgsczSafelyLoadSystemDlls[i]);
68 }
69 }
70 }
71}
72
73extern "C" void DAPI AppInitializeUnsafe()
74{
75 ::HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
76}
77
78extern "C" DAPI_(HRESULT) AppParseCommandLine(
79 __in LPCWSTR wzCommandLine,
80 __in int* pArgc,
81 __in LPWSTR** pArgv
82 )
83{
84 HRESULT hr = S_OK;
85 LPWSTR sczCommandLine = NULL;
86 LPWSTR* argv = NULL;
87 int argc = 0;
88
89 // CommandLineToArgvW tries to treat the first argument as the path to the process,
90 // which fails pretty miserably if your first argument is something like
91 // FOO="C:\Program Files\My Company". So give it something harmless to play with.
92 hr = StrAllocConcat(&sczCommandLine, L"ignored ", 0);
93 ExitOnFailure(hr, "Failed to initialize command line.");
94
95 hr = StrAllocConcat(&sczCommandLine, wzCommandLine, 0);
96 ExitOnFailure(hr, "Failed to copy command line.");
97
98 argv = ::CommandLineToArgvW(sczCommandLine, &argc);
99 ExitOnNullWithLastError(argv, hr, "Failed to parse command line.");
100
101 // Skip "ignored" argument/hack.
102 *pArgv = argv + 1;
103 *pArgc = argc - 1;
104
105LExit:
106 ReleaseStr(sczCommandLine);
107
108 return hr;
109}