diff --git a/installer/PowerToysSetupCustomActions/CustomAction.cpp b/installer/PowerToysSetupCustomActions/CustomAction.cpp index 290df09898..a24713b5ac 100644 --- a/installer/PowerToysSetupCustomActions/CustomAction.cpp +++ b/installer/PowerToysSetupCustomActions/CustomAction.cpp @@ -952,7 +952,7 @@ UINT __stdcall UnRegisterContextMenuPackagesCA(MSIHANDLE hInstall) try { // Packages to unregister - const std::vector packagesToRemoveDisplayName{ { L"PowerRenameContextMenu" }, { L"ImageResizerContextMenu" } }; + const std::vector packagesToRemoveDisplayName{ { L"PowerRenameContextMenu" }, { L"ImageResizerContextMenu" }, { L"FileLocksmithContextMenu" } }; PackageManager packageManager; diff --git a/src/modules/FileLocksmith/FileLocksmithContextMenu/Assets/FileLocksmith/FileLockmsith.ico b/src/modules/FileLocksmith/FileLocksmithContextMenu/Assets/FileLocksmith/FileLocksmith.ico similarity index 100% rename from src/modules/FileLocksmith/FileLocksmithContextMenu/Assets/FileLocksmith/FileLockmsith.ico rename to src/modules/FileLocksmith/FileLocksmithContextMenu/Assets/FileLocksmith/FileLocksmith.ico diff --git a/src/modules/FileLocksmith/FileLocksmithContextMenu/FileLocksmithContextMenu.vcxproj b/src/modules/FileLocksmith/FileLocksmithContextMenu/FileLocksmithContextMenu.vcxproj index 93b4879212..f1d35839c0 100644 --- a/src/modules/FileLocksmith/FileLocksmithContextMenu/FileLocksmithContextMenu.vcxproj +++ b/src/modules/FileLocksmith/FileLocksmithContextMenu/FileLocksmithContextMenu.vcxproj @@ -1,6 +1,5 @@ - + @@ -9,7 +8,6 @@ Win32Proj {799a50d8-de89-4ed1-8ff8-ad5a9ed8c0ca} FileLocksmithContextMenu - 10.0 @@ -98,8 +96,12 @@ MakeAppx.exe pack /d . /p $(OutDir)FileLocksmithContextMenuPackage.msix /nv + + + + {6955446d-23f7-4023-9bb3-8657f904af99} @@ -114,7 +116,15 @@ MakeAppx.exe pack /d . /p $(OutDir)FileLocksmithContextMenuPackage.msix /nv + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + \ No newline at end of file diff --git a/src/modules/FileLocksmith/FileLocksmithContextMenu/dllmain.cpp b/src/modules/FileLocksmith/FileLocksmithContextMenu/dllmain.cpp index 012a22ce51..ffc21058cf 100644 --- a/src/modules/FileLocksmith/FileLocksmithContextMenu/dllmain.cpp +++ b/src/modules/FileLocksmith/FileLocksmithContextMenu/dllmain.cpp @@ -1,14 +1,14 @@ // dllmain.cpp : Defines the entry point for the DLL application. #include "pch.h" -#include "FileLocksmithLib/AppLaunch.h" +#include +#include +#include + #include "FileLocksmithLib/IPC.h" #include "FileLocksmithLib/Settings.h" #include "FileLocksmithLib/Trace.h" -#include -#include - #include #include #include @@ -16,6 +16,7 @@ #include "Generated Files/resource.h" + using namespace Microsoft::WRL; HINSTANCE g_hInst = 0; @@ -54,8 +55,8 @@ public: IFACEMETHODIMP GetIcon(_In_opt_ IShellItemArray*, _Outptr_result_nullonfailure_ PWSTR* icon) { std::wstring iconResourcePath = get_module_folderpath(g_hInst); - iconResourcePath += L"\\Assets\\FileLockmsith\\"; - iconResourcePath += L"FileLockmsith.ico"; + iconResourcePath += L"\\Assets\\FileLocksmith\\"; + iconResourcePath += L"FileLocksmith.ico"; return SHStrDup(iconResourcePath.c_str(), icon); } @@ -93,13 +94,18 @@ public: return result; } - if (HRESULT result = LaunchUI(g_hInst); FAILED(result)) + std::wstring path = get_module_folderpath(g_hInst); + path = path + L"\\PowerToys.FileLocksmithUI.exe"; + + HRESULT result; + + if (!RunNonElevatedEx(path.c_str(), L"", get_module_folderpath(g_hInst))) { + result = E_FAIL; Trace::InvokedRet(result); return result; } - HRESULT result; DWORD num_items; selection->GetCount(&num_items); diff --git a/src/modules/FileLocksmith/FileLocksmithContextMenu/packages.config b/src/modules/FileLocksmith/FileLocksmithContextMenu/packages.config new file mode 100644 index 0000000000..6199e2345c --- /dev/null +++ b/src/modules/FileLocksmith/FileLocksmithContextMenu/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/modules/FileLocksmith/FileLocksmithExt/ExplorerCommand.cpp b/src/modules/FileLocksmith/FileLocksmithExt/ExplorerCommand.cpp index 6491b511ef..e0eee3d781 100644 --- a/src/modules/FileLocksmith/FileLocksmithExt/ExplorerCommand.cpp +++ b/src/modules/FileLocksmith/FileLocksmithExt/ExplorerCommand.cpp @@ -4,7 +4,7 @@ #include "dllmain.h" #include "Generated Files/resource.h" -#include "FileLocksmithLib/AppLaunch.h" +#include "FileLocksmithLib/Constants.h" #include "FileLocksmithLib/Settings.h" #include "FileLocksmithLib/Trace.h" @@ -175,7 +175,7 @@ IFACEMETHODIMP ExplorerCommand::InvokeCommand(CMINVOKECOMMANDINFO* pici) return result; } - if (HRESULT result = LaunchUI(globals::instance); FAILED(result)) + if (HRESULT result = LaunchUI(pici, &writer); FAILED(result)) { Trace::InvokedRet(result); return result; @@ -241,3 +241,48 @@ ExplorerCommand::~ExplorerCommand() { --globals::ref_count; } + +HRESULT ExplorerCommand::LaunchUI(CMINVOKECOMMANDINFO* pici, ipc::Writer* writer) +{ + // Compute exe path + std::wstring exe_path = get_module_folderpath(globals::instance); + exe_path += L'\\'; + exe_path += constants::nonlocalizable::FileNameUIExe; + + STARTUPINFO startupInfo; + ZeroMemory(&startupInfo, sizeof(STARTUPINFO)); + startupInfo.cb = sizeof(STARTUPINFO); + startupInfo.dwFlags = STARTF_USESHOWWINDOW; + + if (pici) + { + startupInfo.wShowWindow = pici->nShow; + } + else + { + startupInfo.wShowWindow = SW_SHOWNORMAL; + } + + PROCESS_INFORMATION processInformation; + std::wstring command_line = L"\""; + command_line += exe_path; + command_line += L"\"\0"; + + CreateProcessW( + NULL, + command_line.data(), + NULL, + NULL, + TRUE, + 0, + NULL, + NULL, + &startupInfo, + &processInformation); + + // Discard handles + CloseHandle(processInformation.hProcess); + CloseHandle(processInformation.hThread); + + return S_OK; +} \ No newline at end of file diff --git a/src/modules/FileLocksmith/FileLocksmithExt/ExplorerCommand.h b/src/modules/FileLocksmith/FileLocksmithExt/ExplorerCommand.h index 04da00c430..d59f266157 100644 --- a/src/modules/FileLocksmith/FileLocksmithExt/ExplorerCommand.h +++ b/src/modules/FileLocksmith/FileLocksmithExt/ExplorerCommand.h @@ -44,6 +44,9 @@ public: private: HBITMAP m_hbmpIcon = nullptr; + // Helpers + HRESULT LaunchUI(CMINVOKECOMMANDINFO* pici, ipc::Writer* writer); + std::atomic m_ref_count = 1; IDataObject* m_data_obj = NULL; std::wstring context_menu_caption; diff --git a/src/modules/FileLocksmith/FileLocksmithExt/PowerToysModule.cpp b/src/modules/FileLocksmith/FileLocksmithExt/PowerToysModule.cpp index 19b0652c89..ee692603d2 100644 --- a/src/modules/FileLocksmith/FileLocksmithExt/PowerToysModule.cpp +++ b/src/modules/FileLocksmith/FileLocksmithExt/PowerToysModule.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include "FileLocksmithLib/Constants.h" @@ -75,6 +77,18 @@ public: virtual void enable() override { Logger::info(L"File Locksmith enabled"); + + if (package::IsWin11OrGreater()) + { + std::wstring path = get_module_folderpath(globals::instance); + std::wstring packageUri = path + L"\\FileLocksmithContextMenuPackage.msix"; + + if (!package::IsPackageRegistered(constants::nonlocalizable::ContextMenuPackageName)) + { + package::RegisterSparsePackage(path, packageUri); + } + } + m_enabled = true; save_settings(); } diff --git a/src/modules/FileLocksmith/FileLocksmithLib/AppLaunch.cpp b/src/modules/FileLocksmith/FileLocksmithLib/AppLaunch.cpp deleted file mode 100644 index be3d8044aa..0000000000 --- a/src/modules/FileLocksmith/FileLocksmithLib/AppLaunch.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "pch.h" -#include "AppLaunch.h" - -HRESULT LaunchUI(HMODULE module) -{ - // Compute exe path - std::wstring exe_path = get_module_folderpath(module); - exe_path += L'\\'; - exe_path += constants::nonlocalizable::FileNameUIExe; - - STARTUPINFO startupInfo; - ZeroMemory(&startupInfo, sizeof(STARTUPINFO)); - startupInfo.cb = sizeof(STARTUPINFO); - startupInfo.dwFlags = STARTF_USESHOWWINDOW; - startupInfo.wShowWindow = SW_SHOWNORMAL; - - PROCESS_INFORMATION processInformation; - std::wstring command_line = L"\""; - command_line += exe_path; - command_line += L"\"\0"; - - CreateProcessW( - NULL, - command_line.data(), - NULL, - NULL, - TRUE, - 0, - NULL, - NULL, - &startupInfo, - &processInformation); - - // Discard handles - CloseHandle(processInformation.hProcess); - CloseHandle(processInformation.hThread); - - return S_OK; -} \ No newline at end of file diff --git a/src/modules/FileLocksmith/FileLocksmithLib/AppLaunch.h b/src/modules/FileLocksmith/FileLocksmithLib/AppLaunch.h deleted file mode 100644 index 5c9b8ec5cb..0000000000 --- a/src/modules/FileLocksmith/FileLocksmithLib/AppLaunch.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include -#include -#include - -#include "Constants.h" - -HRESULT LaunchUI(HMODULE module); - diff --git a/src/modules/FileLocksmith/FileLocksmithLib/Constants.h b/src/modules/FileLocksmith/FileLocksmithLib/Constants.h index d6ac9c9b73..98a141831b 100644 --- a/src/modules/FileLocksmith/FileLocksmithLib/Constants.h +++ b/src/modules/FileLocksmith/FileLocksmithLib/Constants.h @@ -28,6 +28,9 @@ namespace constants::nonlocalizable // Name of the file where the list of files to checked will be stored constexpr WCHAR LastRunPath[] = L"\\last-run.log"; + + // Name of the tier 1 context menu package + constexpr WCHAR ContextMenuPackageName[] = L"FileLocksmithContextMenu"; } // Macros, non-localizable diff --git a/src/modules/FileLocksmith/FileLocksmithLib/FileLocksmithLib.vcxproj b/src/modules/FileLocksmith/FileLocksmithLib/FileLocksmithLib.vcxproj index 80ad53fc61..3e3ecc8756 100644 --- a/src/modules/FileLocksmith/FileLocksmithLib/FileLocksmithLib.vcxproj +++ b/src/modules/FileLocksmith/FileLocksmithLib/FileLocksmithLib.vcxproj @@ -1,6 +1,5 @@ - + 17.0 Win32Proj @@ -66,7 +65,6 @@ - @@ -74,7 +72,6 @@ - diff --git a/src/modules/FileLocksmith/FileLocksmithLib/FileLocksmithLib.vcxproj.filters b/src/modules/FileLocksmith/FileLocksmithLib/FileLocksmithLib.vcxproj.filters index 690f5ede53..8b223a619b 100644 --- a/src/modules/FileLocksmith/FileLocksmithLib/FileLocksmithLib.vcxproj.filters +++ b/src/modules/FileLocksmith/FileLocksmithLib/FileLocksmithLib.vcxproj.filters @@ -30,9 +30,6 @@ Header Files - - Header Files - Header Files @@ -53,8 +50,5 @@ Source Files - - Source Files - \ No newline at end of file