diff options
Diffstat (limited to 'src/dutil/apputil.cpp')
| -rw-r--r-- | src/dutil/apputil.cpp | 109 |
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 | |||
| 5 | const DWORD PRIVATE_LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800; | ||
| 6 | typedef BOOL(WINAPI *LPFN_SETDEFAULTDLLDIRECTORIES)(DWORD); | ||
| 7 | typedef BOOL(WINAPI *LPFN_SETDLLDIRECTORYW)(LPCWSTR); | ||
| 8 | |||
| 9 | extern "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 | /******************************************************************** | ||
| 19 | AppInitialize - initializes the standard safety precautions for an | ||
| 20 | installation application. | ||
| 21 | |||
| 22 | ********************************************************************/ | ||
| 23 | extern "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 | |||
| 73 | extern "C" void DAPI AppInitializeUnsafe() | ||
| 74 | { | ||
| 75 | ::HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); | ||
| 76 | } | ||
| 77 | |||
| 78 | extern "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 | |||
| 105 | LExit: | ||
| 106 | ReleaseStr(sczCommandLine); | ||
| 107 | |||
| 108 | return hr; | ||
| 109 | } | ||
