| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors |
| 3 |
* SPDX-License-Identifier: GPL-2.0-or-later OR CPOL-1.02 |
| 4 |
* |
| 5 |
* Parts of this file are based on: |
| 6 |
* https://www.codeproject.com/articles/570751/devmsi-an-example-cplusplus-msi-wix-deferred-custo |
| 7 |
* |
| 8 |
* Licensed under the The Code Project Open License (CPOL): |
| 9 |
* https://www.codeproject.com/info/cpol10.aspx |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "NCMsiHelper.h" |
| 13 |
|
| 14 |
/** |
| 15 |
* Sets up logging for MSIs and then calls the appropriate custom action with argc/argv parameters. |
| 16 |
* |
| 17 |
* MSI deferred custom action dlls have to handle parameters (properties) a little differently, |
| 18 |
* since the deferred action may not have an active session when it begins. Since the easiest |
| 19 |
* way to pass parameter(s) is to put them all into a CustomActionData property and then retrieve it, |
| 20 |
* the easiest thing to do on this ( C/C++ ) end is to pull the parameter and then split it into |
| 21 |
* a list of parameter(s) that we need. |
| 22 |
* |
| 23 |
* For this implementation, it made sense to treat the single string provided in CustomActionData |
| 24 |
* as if it were a command line, and then parse it out just as if it were a command line. Obviously, |
| 25 |
* the "program name" isn't going to be the first argument unless the MSI writer is pedantic, but |
| 26 |
* otherwise it seems to be a good way to do it. |
| 27 |
* |
| 28 |
* Since all entry points need to do this same work, it was easiest to have a single function that |
| 29 |
* would do the setup, pull the CustomActionData parameter, split it into an argc/argv style of |
| 30 |
* argument list, and then pass that argument list into a function that actually does something |
| 31 |
* interesting. |
| 32 |
* |
| 33 |
* @param hInstall The hInstall parameter provided by MSI/WiX. |
| 34 |
* @param func The function to be called with argc/argv parameters. |
| 35 |
* @param actionName The text description of the function. It will be put in the log. |
| 36 |
* @return Returns ERROR_SUCCESS or ERROR_INSTALL_FAILURE. |
| 37 |
*/ |
| 38 |
UINT CustomActionArgcArgv(MSIHANDLE hInstall, CUSTOM_ACTION_ARGC_ARGV func, LPCSTR actionName) |
| 39 |
{ |
| 40 |
LPWSTR pszCustomActionData = nullptr, *argv = nullptr; |
| 41 |
|
| 42 |
HRESULT hr = WcaInitialize(hInstall, actionName); |
| 43 |
ExitOnFailure(hr, "Failed to initialize"); |
| 44 |
|
| 45 |
WcaLog(LOGMSG_STANDARD, "Initialized."); |
| 46 |
|
| 47 |
// Retrieve our custom action property. This is one of |
| 48 |
// only three properties we can request on a Deferred |
| 49 |
// Custom Action. So, we assume the caller puts all |
| 50 |
// parameters in this one property. |
| 51 |
hr = WcaGetProperty(L"CustomActionData", &pszCustomActionData); |
| 52 |
ExitOnFailure(hr, "Failed to get Custom Action Data."); |
| 53 |
WcaLog(LOGMSG_STANDARD, "Custom Action Data = '%ls'.", pszCustomActionData); |
| 54 |
|
| 55 |
// Convert the string retrieved into a standard argc/arg layout |
| 56 |
// (ignoring the fact that the first parameter is whatever was |
| 57 |
// passed, not necessarily the application name/path). |
| 58 |
int argc = 0; |
| 59 |
argv = CommandLineToArgvW(pszCustomActionData, &argc); |
| 60 |
if (argv) { |
| 61 |
hr = HRESULT_FROM_WIN32(GetLastError()); |
| 62 |
ExitOnFailure(hr, "Failed to convert Custom Action Data to argc/argv."); |
| 63 |
} |
| 64 |
|
| 65 |
hr = (func)(argc, argv); |
| 66 |
ExitOnFailure(hr, "Custom action failed"); |
| 67 |
|
| 68 |
LExit: |
| 69 |
// Resource freeing here! |
| 70 |
ReleaseStr(pszCustomActionData); |
| 71 |
if (argv) |
| 72 |
LocalFree(argv); |
| 73 |
|
| 74 |
return WcaFinalize(SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE); |
| 75 |
} |
| 76 |
|
| 77 |
UINT __stdcall ExecNsisUninstaller(MSIHANDLE hInstall) |
| 78 |
{ |
| 79 |
return CustomActionArgcArgv(hInstall, DoExecNsisUninstaller, "ExecNsisUninstaller"); |
| 80 |
} |
| 81 |
|
| 82 |
UINT __stdcall RemoveNavigationPaneEntries(MSIHANDLE hInstall) |
| 83 |
{ |
| 84 |
return CustomActionArgcArgv(hInstall, DoRemoveNavigationPaneEntries, "RemoveNavigationPaneEntries"); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* DllMain - Initialize and cleanup WiX custom action utils. |
| 89 |
*/ |
| 90 |
extern "C" BOOL WINAPI DllMain( |
| 91 |
__in HINSTANCE hInst, |
| 92 |
__in ULONG ulReason, |
| 93 |
__in LPVOID |
| 94 |
) |
| 95 |
{ |
| 96 |
switch(ulReason) |
| 97 |
{ |
| 98 |
case DLL_PROCESS_ATTACH: |
| 99 |
WcaGlobalInitialize(hInst); |
| 100 |
break; |
| 101 |
|
| 102 |
case DLL_PROCESS_DETACH: |
| 103 |
WcaGlobalFinalize(); |
| 104 |
break; |
| 105 |
} |
| 106 |
|
| 107 |
return TRUE; |
| 108 |
} |