summaryrefslogtreecommitdiffstats
path: root/other-licenses/nsis/Contrib/ExecInExplorer
diff options
context:
space:
mode:
Diffstat (limited to 'other-licenses/nsis/Contrib/ExecInExplorer')
-rw-r--r--other-licenses/nsis/Contrib/ExecInExplorer/ExecInExplorer.cpp212
-rw-r--r--other-licenses/nsis/Contrib/ExecInExplorer/ExecInExplorer.sln31
-rw-r--r--other-licenses/nsis/Contrib/ExecInExplorer/ExecInExplorer.vcxproj175
3 files changed, 418 insertions, 0 deletions
diff --git a/other-licenses/nsis/Contrib/ExecInExplorer/ExecInExplorer.cpp b/other-licenses/nsis/Contrib/ExecInExplorer/ExecInExplorer.cpp
new file mode 100644
index 0000000000..05e0ed5e92
--- /dev/null
+++ b/other-licenses/nsis/Contrib/ExecInExplorer/ExecInExplorer.cpp
@@ -0,0 +1,212 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// This file is an NSIS plugin which exports a function that starts a process
+// from a provided path by using the shell automation API to have explorer.exe
+// invoke ShellExecute. This roundabout method of starting a process is useful
+// because it means the new process will use the integrity level and security
+// token of the shell, so it allows starting an unelevated process from inside
+// an elevated one. The method is based on
+// https://blogs.msdn.microsoft.com/oldnewthing/20131118-00/?p=2643
+// but the code has been rewritten to remove the need for ATL or the C runtime.
+
+// Normally an NSIS installer would use the UAC plugin, which itself uses both
+// an unelevated and an elevated process, and the elevated process can invoke
+// functions in the unelevated one, so this plugin wouldn't be needed.
+// But uninstallers are often directly run elevated because that's just how
+// the Windows UI launches them, so there is no unelevated process. This
+// plugin allows starting a needed unelevated process in that situation.
+
+#include <windows.h>
+#include <shlobj.h>
+
+#pragma comment(lib, "shlwapi.lib")
+
+static IShellView*
+GetDesktopWindowShellView()
+{
+ IShellView* view = nullptr;
+ IShellWindows* shell = nullptr;
+ CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_LOCAL_SERVER,
+ IID_PPV_ARGS(&shell));
+ if (shell) {
+ VARIANT empty;
+ VariantInit(&empty);
+
+ VARIANT loc;
+ loc.vt = VT_VARIANT | VT_BYREF;
+ PIDLIST_ABSOLUTE locList;
+ SHGetFolderLocation(nullptr, CSIDL_DESKTOP, nullptr, 0, &locList);
+ loc.byref = locList;
+
+ HWND windowHandle = 0;
+ IDispatch* dispatch = nullptr;
+
+ shell->FindWindowSW(&loc, &empty, SWC_DESKTOP, (long*)&windowHandle,
+ SWFO_NEEDDISPATCH, &dispatch);
+ if (dispatch) {
+ IServiceProvider* provider = nullptr;
+ dispatch->QueryInterface(IID_PPV_ARGS(&provider));
+ if (provider) {
+ IShellBrowser* browser = nullptr;
+ provider->QueryService(SID_STopLevelBrowser, IID_PPV_ARGS(&browser));
+ if (browser) {
+ browser->QueryActiveShellView(&view);
+ browser->Release();
+ }
+ provider->Release();
+ }
+ dispatch->Release();
+ }
+ shell->Release();
+ }
+
+ return view;
+}
+
+static IShellDispatch2*
+GetApplicationFromShellView(IShellView* view)
+{
+ IShellDispatch2* shellDispatch = nullptr;
+ IDispatch* viewDisp = nullptr;
+ HRESULT hr = view->GetItemObject(SVGIO_BACKGROUND, IID_PPV_ARGS(&viewDisp));
+ if (SUCCEEDED(hr)) {
+ IShellFolderViewDual* shellViewFolder = nullptr;
+ viewDisp->QueryInterface(IID_PPV_ARGS(&shellViewFolder));
+ if (shellViewFolder) {
+ IDispatch* dispatch = nullptr;
+ shellViewFolder->get_Application(&dispatch);
+ if (dispatch) {
+ dispatch->QueryInterface(IID_PPV_ARGS(&shellDispatch));
+ dispatch->Release();
+ }
+ shellViewFolder->Release();
+ }
+ viewDisp->Release();
+ }
+ return shellDispatch;
+}
+
+static bool
+ShellExecInExplorerProcess(wchar_t* path, wchar_t* args = nullptr)
+{
+ bool rv = false;
+ if (SUCCEEDED(CoInitialize(nullptr))) {
+ IShellView *desktopView = GetDesktopWindowShellView();
+ if (desktopView) {
+ IShellDispatch2 *shellDispatch = GetApplicationFromShellView(desktopView);
+ if (shellDispatch) {
+ BSTR bstrPath = SysAllocString(path);
+ VARIANT vArgs;
+ VariantInit(&vArgs);
+ if (args) {
+ vArgs.vt = VT_BSTR;
+ vArgs.bstrVal = SysAllocString(args);
+ }
+ rv = SUCCEEDED(shellDispatch->ShellExecuteW(bstrPath, vArgs, VARIANT{},
+ VARIANT{}, VARIANT{}));
+ VariantClear(&vArgs);
+ SysFreeString(bstrPath);
+ shellDispatch->Release();
+ }
+ desktopView->Release();
+ }
+ CoUninitialize();
+ }
+ return rv;
+}
+
+struct stack_t {
+ stack_t* next;
+ TCHAR text[MAX_PATH];
+};
+
+/**
+ * Removes an element from the top of the NSIS stack
+ *
+ * @param stacktop A pointer to the top of the stack
+ * @param str The string to pop to
+ * @param len The max length
+ * @return 0 on success
+ */
+int
+popstring(stack_t **stacktop, TCHAR *str, int len)
+{
+ // Removes the element from the top of the stack and puts it in the buffer
+ stack_t *th;
+ if (!stacktop || !*stacktop) {
+ return 1;
+ }
+
+ th = (*stacktop);
+ lstrcpyn(str, th->text, len);
+ *stacktop = th->next;
+ HeapFree(GetProcessHeap(), 0, th);
+ return 0;
+}
+
+/**
+ * Adds an element to the top of the NSIS stack
+ *
+ * @param stacktop A pointer to the top of the stack
+ * @param str The string to push on the stack
+ * @param len The length of the string to push on the stack
+ * @return 0 on success
+ */
+void
+pushstring(stack_t **stacktop, const TCHAR *str, int len)
+{
+ stack_t *th;
+ if (!stacktop) {
+ return;
+ }
+ th = (stack_t*)HeapAlloc(GetProcessHeap(), 0, sizeof(stack_t) + len);
+ lstrcpyn(th->text, str, len);
+ th->next = *stacktop;
+ *stacktop = th;
+}
+
+/**
+* Starts an executable or URL from the shell process.
+*
+* @param stacktop Pointer to the top of the stack, AKA the first parameter to
+ the plugin call. Should contain the file or URL to execute.
+* @return 1 if the file/URL was executed successfully, 0 if it was not
+*/
+extern "C" void __declspec(dllexport)
+Exec(HWND, int, TCHAR *, stack_t **stacktop, void *)
+{
+ wchar_t path[MAX_PATH + 1];
+ wchar_t args[MAX_PATH + 1];
+ bool rv = false;
+ bool restoreArgString = false;
+ // We're skipping building the C runtime to keep the file size low, so we
+ // can't use a normal string initialization because that would call memset.
+ path[0] = L'\0';
+ args[0] = L'\0';
+ popstring(stacktop, path, MAX_PATH);
+ if (popstring(stacktop, args, MAX_PATH) == 0) {
+ // This stack item may not be for us, but we don't know yet.
+ restoreArgString = true;
+ }
+
+ if (lstrcmpW(args, L"/cmdargs") == 0) {
+ popstring(stacktop, args, MAX_PATH);
+ rv = ShellExecInExplorerProcess(path, args);
+ } else {
+ // If the stack wasn't empty, then we popped something that wasn't for us.
+ if (restoreArgString) {
+ pushstring(stacktop, args, lstrlenW(args));
+ }
+ rv = ShellExecInExplorerProcess(path);
+ }
+
+ pushstring(stacktop, rv ? L"1" : L"0", 2);
+}
+
+BOOL APIENTRY
+DllMain(HMODULE, DWORD, LPVOID)
+{
+ return TRUE;
+}
diff --git a/other-licenses/nsis/Contrib/ExecInExplorer/ExecInExplorer.sln b/other-licenses/nsis/Contrib/ExecInExplorer/ExecInExplorer.sln
new file mode 100644
index 0000000000..cbd2ea4bf0
--- /dev/null
+++ b/other-licenses/nsis/Contrib/ExecInExplorer/ExecInExplorer.sln
@@ -0,0 +1,31 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 15
+VisualStudioVersion = 15.0.27428.2015
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ExecInExplorer", "ExecInExplorer.vcxproj", "{B5DBA89B-37EE-425C-A375-4E04E69731FA}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|x64 = Debug|x64
+ Debug|x86 = Debug|x86
+ Release|x64 = Release|x64
+ Release|x86 = Release|x86
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {B5DBA89B-37EE-425C-A375-4E04E69731FA}.Debug|x64.ActiveCfg = Debug|x64
+ {B5DBA89B-37EE-425C-A375-4E04E69731FA}.Debug|x64.Build.0 = Debug|x64
+ {B5DBA89B-37EE-425C-A375-4E04E69731FA}.Debug|x86.ActiveCfg = Debug|Win32
+ {B5DBA89B-37EE-425C-A375-4E04E69731FA}.Debug|x86.Build.0 = Debug|Win32
+ {B5DBA89B-37EE-425C-A375-4E04E69731FA}.Release|x64.ActiveCfg = Release|x64
+ {B5DBA89B-37EE-425C-A375-4E04E69731FA}.Release|x64.Build.0 = Release|x64
+ {B5DBA89B-37EE-425C-A375-4E04E69731FA}.Release|x86.ActiveCfg = Release|Win32
+ {B5DBA89B-37EE-425C-A375-4E04E69731FA}.Release|x86.Build.0 = Release|Win32
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {69740CF4-5E42-4A56-AFF5-2D17D42CFB75}
+ EndGlobalSection
+EndGlobal
diff --git a/other-licenses/nsis/Contrib/ExecInExplorer/ExecInExplorer.vcxproj b/other-licenses/nsis/Contrib/ExecInExplorer/ExecInExplorer.vcxproj
new file mode 100644
index 0000000000..fcb217d87c
--- /dev/null
+++ b/other-licenses/nsis/Contrib/ExecInExplorer/ExecInExplorer.vcxproj
@@ -0,0 +1,175 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup Label="ProjectConfigurations">
+ <ProjectConfiguration Include="Debug|Win32">
+ <Configuration>Debug</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|Win32">
+ <Configuration>Release</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Debug|x64">
+ <Configuration>Debug</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|x64">
+ <Configuration>Release</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ </ItemGroup>
+ <PropertyGroup Label="Globals">
+ <VCProjectVersion>15.0</VCProjectVersion>
+ <ProjectGuid>{B5DBA89B-37EE-425C-A375-4E04E69731FA}</ProjectGuid>
+ <Keyword>Win32Proj</Keyword>
+ <RootNamespace>ExecInExplorer</RootNamespace>
+ <WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+ <ConfigurationType>DynamicLibrary</ConfigurationType>
+ <UseDebugLibraries>true</UseDebugLibraries>
+ <PlatformToolset>v141</PlatformToolset>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+ <ConfigurationType>DynamicLibrary</ConfigurationType>
+ <UseDebugLibraries>false</UseDebugLibraries>
+ <PlatformToolset>v141</PlatformToolset>
+ <WholeProgramOptimization>true</WholeProgramOptimization>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
+ <ConfigurationType>DynamicLibrary</ConfigurationType>
+ <UseDebugLibraries>true</UseDebugLibraries>
+ <PlatformToolset>v141</PlatformToolset>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
+ <ConfigurationType>DynamicLibrary</ConfigurationType>
+ <UseDebugLibraries>false</UseDebugLibraries>
+ <PlatformToolset>v141</PlatformToolset>
+ <WholeProgramOptimization>true</WholeProgramOptimization>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+ <ImportGroup Label="ExtensionSettings">
+ </ImportGroup>
+ <ImportGroup Label="Shared">
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <PropertyGroup Label="UserMacros" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <LinkIncremental>false</LinkIncremental>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <LinkIncremental>false</LinkIncremental>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <LinkIncremental>false</LinkIncremental>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <LinkIncremental>false</LinkIncremental>
+ </PropertyGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <ClCompile>
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>Disabled</Optimization>
+ <SDLCheck>false</SDLCheck>
+ <PreprocessorDefinitions>WIN32;_DEBUG;EXECINEXPLORER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
+ <OmitDefaultLibName>true</OmitDefaultLibName>
+ <BufferSecurityCheck>false</BufferSecurityCheck>
+ <BasicRuntimeChecks>Default</BasicRuntimeChecks>
+ </ClCompile>
+ <Link>
+ <SubSystem>Windows</SubSystem>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <EntryPointSymbol>DllMain</EntryPointSymbol>
+ <LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <ClCompile>
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>Disabled</Optimization>
+ <SDLCheck>false</SDLCheck>
+ <PreprocessorDefinitions>_DEBUG;EXECINEXPLORER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
+ <OmitDefaultLibName>true</OmitDefaultLibName>
+ <BufferSecurityCheck>false</BufferSecurityCheck>
+ <BasicRuntimeChecks>Default</BasicRuntimeChecks>
+ </ClCompile>
+ <Link>
+ <SubSystem>Windows</SubSystem>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <EntryPointSymbol>DllMain</EntryPointSymbol>
+ <LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <ClCompile>
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>MaxSpeed</Optimization>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <SDLCheck>false</SDLCheck>
+ <PreprocessorDefinitions>WIN32;NDEBUG;EXECINEXPLORER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
+ <OmitDefaultLibName>true</OmitDefaultLibName>
+ <BufferSecurityCheck>false</BufferSecurityCheck>
+ <WholeProgramOptimization>false</WholeProgramOptimization>
+ </ClCompile>
+ <Link>
+ <SubSystem>Windows</SubSystem>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <OptimizeReferences>true</OptimizeReferences>
+ <GenerateDebugInformation>false</GenerateDebugInformation>
+ <EntryPointSymbol>DllMain</EntryPointSymbol>
+ <LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <ClCompile>
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>MaxSpeed</Optimization>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <SDLCheck>false</SDLCheck>
+ <PreprocessorDefinitions>NDEBUG;EXECINEXPLORER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
+ <OmitDefaultLibName>true</OmitDefaultLibName>
+ <BufferSecurityCheck>false</BufferSecurityCheck>
+ <WholeProgramOptimization>false</WholeProgramOptimization>
+ </ClCompile>
+ <Link>
+ <SubSystem>Windows</SubSystem>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <OptimizeReferences>true</OptimizeReferences>
+ <GenerateDebugInformation>false</GenerateDebugInformation>
+ <EntryPointSymbol>DllMain</EntryPointSymbol>
+ <LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemGroup>
+ <ClCompile Include="ExecInExplorer.cpp" />
+ </ItemGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+ <ImportGroup Label="ExtensionTargets">
+ </ImportGroup>
+</Project> \ No newline at end of file