From 78b5daf01555d86293c1012ed3de704752880a6a Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Fri, 24 Jun 2022 12:26:27 -0500 Subject: Move LoadSystemLibrary and LoadSystemLibraryWithPath into apputil. --- src/libs/dutil/WixToolset.DUtil/app2util.cpp | 60 ++++++++++++++++ src/libs/dutil/WixToolset.DUtil/apputil.cpp | 82 +++++++++++----------- src/libs/dutil/WixToolset.DUtil/dutil.cpp | 61 ---------------- src/libs/dutil/WixToolset.DUtil/dutil.vcxproj | 1 + .../dutil/WixToolset.DUtil/dutil.vcxproj.filters | 3 + src/libs/dutil/WixToolset.DUtil/inc/apputil.h | 5 ++ src/libs/dutil/WixToolset.DUtil/inc/dutil.h | 30 +++++++- 7 files changed, 137 insertions(+), 105 deletions(-) create mode 100644 src/libs/dutil/WixToolset.DUtil/app2util.cpp (limited to 'src') diff --git a/src/libs/dutil/WixToolset.DUtil/app2util.cpp b/src/libs/dutil/WixToolset.DUtil/app2util.cpp new file mode 100644 index 00000000..d2e21e39 --- /dev/null +++ b/src/libs/dutil/WixToolset.DUtil/app2util.cpp @@ -0,0 +1,60 @@ +// 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. + +#include "precomp.h" + +// Exit macros +#define AppExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__) +#define AppExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__) +#define AppExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__) +#define AppExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__) +#define AppExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__) +#define AppExitWithRootFailure(x, e, s, ...) ExitWithRootFailureSource(DUTIL_SOURCE_APPUTIL, x, e, s, __VA_ARGS__) +#define AppExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__) +#define AppExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_APPUTIL, p, x, e, s, __VA_ARGS__) +#define AppExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_APPUTIL, p, x, s, __VA_ARGS__) +#define AppExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_APPUTIL, p, x, e, s, __VA_ARGS__) +#define AppExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_APPUTIL, p, x, s, __VA_ARGS__) +#define AppExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_APPUTIL, e, x, s, __VA_ARGS__) +#define AppExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_APPUTIL, g, x, s, __VA_ARGS__) + +DAPI_(void) AppFreeCommandLineArgs( + __in LPWSTR* argv + ) +{ + // The "ignored" hack in AppParseCommandLine requires an adjustment. + LPWSTR* argvOriginal = argv - 1; + ::LocalFree(argvOriginal); +} + +DAPI_(HRESULT) AppParseCommandLine( + __in LPCWSTR wzCommandLine, + __in int* pArgc, + __in LPWSTR** pArgv + ) +{ + HRESULT hr = S_OK; + LPWSTR sczCommandLine = NULL; + LPWSTR* argv = NULL; + int argc = 0; + + // CommandLineToArgvW tries to treat the first argument as the path to the process, + // which fails pretty miserably if your first argument is something like + // FOO="C:\Program Files\My Company". So give it something harmless to play with. + hr = StrAllocConcat(&sczCommandLine, L"ignored ", 0); + AppExitOnFailure(hr, "Failed to initialize command line."); + + hr = StrAllocConcat(&sczCommandLine, wzCommandLine, 0); + AppExitOnFailure(hr, "Failed to copy command line."); + + argv = ::CommandLineToArgvW(sczCommandLine, &argc); + AppExitOnNullWithLastError(argv, hr, "Failed to parse command line."); + + // Skip "ignored" argument/hack. + *pArgv = argv + 1; + *pArgc = argc - 1; + +LExit: + ReleaseStr(sczCommandLine); + + return hr; +} diff --git a/src/libs/dutil/WixToolset.DUtil/apputil.cpp b/src/libs/dutil/WixToolset.DUtil/apputil.cpp index 7e0bbc7b..147d1d1e 100644 --- a/src/libs/dutil/WixToolset.DUtil/apputil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/apputil.cpp @@ -8,6 +8,7 @@ #define AppExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__) #define AppExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__) #define AppExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__) +#define AppExitWithRootFailure(x, e, s, ...) ExitWithRootFailureSource(DUTIL_SOURCE_APPUTIL, x, e, s, __VA_ARGS__) #define AppExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_APPUTIL, x, s, __VA_ARGS__) #define AppExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_APPUTIL, p, x, e, s, __VA_ARGS__) #define AppExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_APPUTIL, p, x, s, __VA_ARGS__) @@ -31,20 +32,50 @@ static HRESULT EscapeCommandLineArgument( __out_z LPWSTR* psczEscaped ); -DAPI_(void) AppFreeCommandLineArgs( - __in LPWSTR* argv +DAPI_(HRESULT) LoadSystemLibrary( + __in_z LPCWSTR wzModuleName, + __out HMODULE* phModule ) { - // The "ignored" hack in AppParseCommandLine requires an adjustment. - LPWSTR* argvOriginal = argv - 1; - ::LocalFree(argvOriginal); + HRESULT hr = LoadSystemLibraryWithPath(wzModuleName, phModule, NULL); + return hr; } -/******************************************************************** -AppInitialize - initializes the standard safety precautions for an - installation application. +DAPI_(HRESULT) LoadSystemLibraryWithPath( + __in_z LPCWSTR wzModuleName, + __out HMODULE* phModule, + __deref_out_z_opt LPWSTR* psczPath + ) +{ + HRESULT hr = S_OK; + DWORD cch = 0; + WCHAR wzPath[MAX_PATH] = { }; + + cch = ::GetSystemDirectoryW(wzPath, MAX_PATH); + AppExitOnNullWithLastError(cch, hr, "Failed to get the Windows system directory."); + + if (L'\\' != wzPath[cch - 1]) + { + hr = ::StringCchCatNW(wzPath, MAX_PATH, L"\\", 1); + AppExitOnRootFailure(hr, "Failed to terminate the string with a backslash."); + } + + hr = ::StringCchCatW(wzPath, MAX_PATH, wzModuleName); + AppExitOnRootFailure(hr, "Failed to create the fully-qualified path to %ls.", wzModuleName); + + *phModule = ::LoadLibraryW(wzPath); + AppExitOnNullWithLastError(*phModule, hr, "Failed to load the library %ls.", wzModuleName); + + if (psczPath) + { + hr = StrAllocString(psczPath, wzPath, MAX_PATH); + AppExitOnFailure(hr, "Failed to copy the path to library."); + } + +LExit: + return hr; +} -********************************************************************/ DAPI_(void) AppInitialize( __in_ecount(cSafelyLoadSystemDlls) LPCWSTR rgsczSafelyLoadSystemDlls[], __in DWORD cSafelyLoadSystemDlls @@ -101,39 +132,6 @@ DAPI_(void) AppInitializeUnsafe() ::HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); } -DAPI_(HRESULT) AppParseCommandLine( - __in LPCWSTR wzCommandLine, - __in int* pArgc, - __in LPWSTR** pArgv - ) -{ - HRESULT hr = S_OK; - LPWSTR sczCommandLine = NULL; - LPWSTR* argv = NULL; - int argc = 0; - - // CommandLineToArgvW tries to treat the first argument as the path to the process, - // which fails pretty miserably if your first argument is something like - // FOO="C:\Program Files\My Company". So give it something harmless to play with. - hr = StrAllocConcat(&sczCommandLine, L"ignored ", 0); - AppExitOnFailure(hr, "Failed to initialize command line."); - - hr = StrAllocConcat(&sczCommandLine, wzCommandLine, 0); - AppExitOnFailure(hr, "Failed to copy command line."); - - argv = ::CommandLineToArgvW(sczCommandLine, &argc); - AppExitOnNullWithLastError(argv, hr, "Failed to parse command line."); - - // Skip "ignored" argument/hack. - *pArgv = argv + 1; - *pArgc = argc - 1; - -LExit: - ReleaseStr(sczCommandLine); - - return hr; -} - DAPI_(HRESULT) AppAppendCommandLineArgument( __deref_inout_z LPWSTR* psczCommandLine, __in_z LPCWSTR wzArgument diff --git a/src/libs/dutil/WixToolset.DUtil/dutil.cpp b/src/libs/dutil/WixToolset.DUtil/dutil.cpp index dd4fa8bf..83b53f64 100644 --- a/src/libs/dutil/WixToolset.DUtil/dutil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/dutil.cpp @@ -486,64 +486,3 @@ extern "C" void DAPI Dutil_RootFailure( TraceError(hrError, "Root failure at %s:%d", szFile, iLine); } - -/******************************************************************* - LoadSystemLibrary - Fully qualifies the path to a module in the - Windows system directory and loads it. - - Returns - E_MODNOTFOUND - The module could not be found. - * - Another error occured. -********************************************************************/ -extern "C" HRESULT DAPI LoadSystemLibrary( - __in_z LPCWSTR wzModuleName, - __out HMODULE *phModule - ) -{ - HRESULT hr = LoadSystemLibraryWithPath(wzModuleName, phModule, NULL); - return hr; -} - -/******************************************************************* - LoadSystemLibraryWithPath - Fully qualifies the path to a module in - the Windows system directory and loads it - and returns the path - - Returns - E_MODNOTFOUND - The module could not be found. - * - Another error occured. -********************************************************************/ -extern "C" HRESULT DAPI LoadSystemLibraryWithPath( - __in_z LPCWSTR wzModuleName, - __out HMODULE *phModule, - __deref_out_z_opt LPWSTR* psczPath - ) -{ - HRESULT hr = S_OK; - DWORD cch = 0; - WCHAR wzPath[MAX_PATH] = { }; - - cch = ::GetSystemDirectoryW(wzPath, MAX_PATH); - DExitOnNullWithLastError(cch, hr, "Failed to get the Windows system directory."); - - if (L'\\' != wzPath[cch - 1]) - { - hr = ::StringCchCatNW(wzPath, MAX_PATH, L"\\", 1); - DExitOnRootFailure(hr, "Failed to terminate the string with a backslash."); - } - - hr = ::StringCchCatW(wzPath, MAX_PATH, wzModuleName); - DExitOnRootFailure(hr, "Failed to create the fully-qualified path to %ls.", wzModuleName); - - *phModule = ::LoadLibraryW(wzPath); - DExitOnNullWithLastError(*phModule, hr, "Failed to load the library %ls.", wzModuleName); - - if (psczPath) - { - hr = StrAllocString(psczPath, wzPath, MAX_PATH); - DExitOnFailure(hr, "Failed to copy the path to library."); - } - -LExit: - return hr; -} diff --git a/src/libs/dutil/WixToolset.DUtil/dutil.vcxproj b/src/libs/dutil/WixToolset.DUtil/dutil.vcxproj index 3100768d..3e3c42b7 100644 --- a/src/libs/dutil/WixToolset.DUtil/dutil.vcxproj +++ b/src/libs/dutil/WixToolset.DUtil/dutil.vcxproj @@ -46,6 +46,7 @@ + diff --git a/src/libs/dutil/WixToolset.DUtil/dutil.vcxproj.filters b/src/libs/dutil/WixToolset.DUtil/dutil.vcxproj.filters index c966c965..07698f9e 100644 --- a/src/libs/dutil/WixToolset.DUtil/dutil.vcxproj.filters +++ b/src/libs/dutil/WixToolset.DUtil/dutil.vcxproj.filters @@ -21,6 +21,9 @@ Source Files + + Source Files + Source Files diff --git a/src/libs/dutil/WixToolset.DUtil/inc/apputil.h b/src/libs/dutil/WixToolset.DUtil/inc/apputil.h index 11280102..95a98e73 100644 --- a/src/libs/dutil/WixToolset.DUtil/inc/apputil.h +++ b/src/libs/dutil/WixToolset.DUtil/inc/apputil.h @@ -16,6 +16,11 @@ void DAPI AppFreeCommandLineArgs( __in LPWSTR* argv ); +/******************************************************************** +AppInitialize - initializes the standard safety precautions for an + installation application. + +********************************************************************/ void DAPI AppInitialize( __in_ecount(cSafelyLoadSystemDlls) LPCWSTR rgsczSafelyLoadSystemDlls[], __in DWORD cSafelyLoadSystemDlls diff --git a/src/libs/dutil/WixToolset.DUtil/inc/dutil.h b/src/libs/dutil/WixToolset.DUtil/inc/dutil.h index 618349da..4a29b66b 100644 --- a/src/libs/dutil/WixToolset.DUtil/inc/dutil.h +++ b/src/libs/dutil/WixToolset.DUtil/inc/dutil.h @@ -205,8 +205,34 @@ extern "C" { #endif // other functions -HRESULT DAPI LoadSystemLibrary(__in_z LPCWSTR wzModuleName, __out HMODULE *phModule); -HRESULT DAPI LoadSystemLibraryWithPath(__in_z LPCWSTR wzModuleName, __out HMODULE *phModule, __deref_out_z_opt LPWSTR* psczPath); + +/******************************************************************* + LoadSystemLibrary - Fully qualifies the path to a module in the + Windows system directory and loads it. + + Returns + E_MODNOTFOUND - The module could not be found. + * - Another error occured. +********************************************************************/ +HRESULT DAPI LoadSystemLibrary( + __in_z LPCWSTR wzModuleName, + __out HMODULE* phModule + ); + +/******************************************************************* + LoadSystemLibraryWithPath - Fully qualifies the path to a module in + the Windows system directory and loads it + and returns the path + + Returns + E_MODNOTFOUND - The module could not be found. + * - Another error occured. +********************************************************************/ +HRESULT DAPI LoadSystemLibraryWithPath( + __in_z LPCWSTR wzModuleName, + __out HMODULE* phModule, + __deref_out_z_opt LPWSTR* psczPath + ); #ifdef __cplusplus } -- cgit v1.2.3-55-g6feb