Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSIX: prompt and uninstall MSI PT version #1248

Merged
merged 15 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion installer/MSIX/PackagingLayout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Files>
<File DestinationPath="License.rtf" SourcePath="..\..\License.rtf"/>

<File DestinationPath="PowerToys.exe" SourcePath="..\..\x64\Release\PowerToys.exe"/>
<File DestinationPath="Power Toys.exe" SourcePath="..\..\x64\Release\PowerToys.exe"/>
<File DestinationPath="PowerToysSettings.exe" SourcePath="..\..\x64\Release\PowerToysSettings.exe"/>

<File DestinationPath="modules\FancyZonesEditor.exe" SourcePath="..\..\x64\Release\modules\FancyZonesEditor.exe"/>
Expand Down
4 changes: 2 additions & 2 deletions installer/MSIX/appxmanifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
<rescap:Capability Name="allowElevation"/>
</Capabilities>
<Applications>
<Application Id="PowerToys" Executable="PowerToys.exe" EntryPoint="Windows.FullTrustApplication">
<Application Id="PowerToys" Executable="Power Toys.exe" EntryPoint="Windows.FullTrustApplication">
<uap:VisualElements DisplayName="PowerToys (Preview)" Description="Windows system utilities to maximize productivity" Square150x150Logo="Images\logo150.png" Square44x44Logo="Images\logo44.png" BackgroundColor="transparent" />
<Extensions>
<uap5:Extension Category="windows.startupTask" Executable="PowerToys.exe" EntryPoint="Windows.FullTrustApplication">
<uap5:Extension Category="windows.startupTask" Executable="Power Toys.exe" EntryPoint="Windows.FullTrustApplication">
<uap5:StartupTask TaskId="PowerToysStartupTaskID" Enabled="true" DisplayName="PowerToys" />
</uap5:Extension>
<com:Extension Category="windows.comServer">
Expand Down
45 changes: 24 additions & 21 deletions src/common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,31 @@ int run_message_loop() {
return static_cast<int>(msg.wParam);
}

std::optional<std::wstring> get_last_error_message(const DWORD dw) {
std::optional<std::wstring> message;
try {
const auto msg = std::system_category().message(dw);
message.emplace(begin(msg), end(msg));
}
catch(...) {

}
return message;
}

void show_last_error_message(LPCWSTR lpszFunction, DWORD dw) {
// Retrieve the system error message for the error code
LPWSTR lpMsgBuf = NULL;
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
lpMsgBuf,
0, NULL) > 0) {
// Display the error message and exit the process
LPWSTR lpDisplayBuf = (LPWSTR)LocalAlloc(LMEM_ZEROINIT, (lstrlenW(lpMsgBuf) + lstrlenW(lpszFunction) + 40) * sizeof(WCHAR));
if (lpDisplayBuf != NULL) {
StringCchPrintfW(lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(WCHAR),
L"%s failed with error %d: %s",
lpszFunction, dw, lpMsgBuf);
MessageBoxW(NULL, (LPCTSTR)lpDisplayBuf, L"Error", MB_OK);
LocalFree(lpDisplayBuf);
}
LocalFree(lpMsgBuf);
const auto system_message = get_last_error_message(dw);
if(!system_message.has_value()) {
return;
}
LPWSTR lpDisplayBuf = (LPWSTR)LocalAlloc(LMEM_ZEROINIT, (system_message->size() + lstrlenW(lpszFunction) + 40) * sizeof(WCHAR));
if (lpDisplayBuf != NULL) {
StringCchPrintfW(lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(WCHAR),
L"%s failed with error %d: %s",
yuyoyuppe marked this conversation as resolved.
Show resolved Hide resolved
lpszFunction, dw, system_message->c_str());
MessageBoxW(NULL, (LPCTSTR)lpDisplayBuf, L"Error", MB_OK);
LocalFree(lpDisplayBuf);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ RECT keep_rect_inside_rect(const RECT& small_rect, const RECT& big_rect);
// Initializes and runs windows message loop
int run_message_loop();

std::optional<std::wstring> get_last_error_message(const DWORD dw);
void show_last_error_message(LPCWSTR lpszFunction, DWORD dw);

enum WindowState {
Expand Down
6 changes: 6 additions & 0 deletions src/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <common/common.h>
#include <common/dpi_aware.h>

#include "msi_to_msix_upgrade.h"
#include <common/winstore.h>
#include <common/notifications.h>

Expand Down Expand Up @@ -53,6 +54,11 @@ int runner()
int result;
try
{
// If we're running as a MSIX application, offer a user uninstall option of an old version if detected
std::thread{ [] {
uninstall_msi_with_confirmation();
} }.detach();

chdir_current_executable();
// Load Powertyos DLLS
// For now only load known DLLs
Expand Down
65 changes: 65 additions & 0 deletions src/runner/msi_to_msix_upgrade.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "pch.h"
#include "msi_to_msix_upgrade.h"

#include <msi.h>
#include <common/common.h>

#include <common/winstore.h>
#include <common/notifications.h>
#include <MsiQuery.h>

namespace
{
const wchar_t* POWER_TOYS_UPGRADE_CODE = L"{42B84BF7-5FBF-473B-9C8B-049DC16F7708}";
const wchar_t* DONT_SHOW_AGAIN_RECORD_REGISTRY_PATH = L"delete_previous_powertoys_confirm";
}

void uninstall_msi_with_confirmation()
{
if (!winstore::running_as_packaged())
{
return;
}

wchar_t GUID_product_string[39];
if (const bool found = ERROR_SUCCESS == MsiEnumRelatedProductsW(POWER_TOYS_UPGRADE_CODE, 0, 0, GUID_product_string); !found)
{
return;
}

if (const bool installed = INSTALLSTATE_DEFAULT == MsiQueryProductStateW(GUID_product_string); !installed)
{
return;
}

DWORD package_path_size = 0;

if (const bool has_package_path = ERROR_SUCCESS == MsiGetProductInfoW(GUID_product_string, INSTALLPROPERTY_LOCALPACKAGE, nullptr, &package_path_size); !has_package_path)
{
return;
}

const auto package_path = std::make_unique<wchar_t[]>(package_path_size++);

if (const bool got_package_path = ERROR_SUCCESS == MsiGetProductInfoW(GUID_product_string, INSTALLPROPERTY_LOCALPACKAGE, package_path.get(), &package_path_size); !got_package_path)
{
return;
}

const auto selection = SHMessageBoxCheckW(nullptr, L"We've detected a previous installation of PowerToys. Would you like to remove it?", L"PowerToys: uninstall previous version?", MB_ICONQUESTION | MB_YESNO, IDNO, DONT_SHOW_AGAIN_RECORD_REGISTRY_PATH);
yuyoyuppe marked this conversation as resolved.
Show resolved Hide resolved
if (selection != IDYES)
{
return;
}

const auto uninstall_result = MsiInstallProductW(package_path.get(), L"REMOVE=ALL");
auto system_message = get_last_error_message(uninstall_result);
if (ERROR_SUCCESS == uninstall_result)
{
notifications::show_toast(L"Previous version of PowerToys was uninstalled successfully.");
yuyoyuppe marked this conversation as resolved.
Show resolved Hide resolved
}
else if (auto system_message = get_last_error_message(uninstall_result); system_message.has_value())
{
notifications::show_toast(*system_message);
}
}
3 changes: 3 additions & 0 deletions src/runner/msi_to_msix_upgrade.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void uninstall_msi_with_confirmation();
4 changes: 4 additions & 0 deletions src/runner/runner.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Link>
<UACExecutionLevel>AsInvoker</UACExecutionLevel>
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
<AdditionalDependencies>Msi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Manifest>
<EnableDpiAwareness>false</EnableDpiAwareness>
Expand All @@ -90,6 +91,7 @@
<OptimizeReferences>true</OptimizeReferences>
<UACExecutionLevel>AsInvoker</UACExecutionLevel>
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
<AdditionalDependencies>Msi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Manifest>
<EnableDpiAwareness>false</EnableDpiAwareness>
Expand All @@ -99,6 +101,7 @@
<ClCompile Include="auto_start_helper.cpp" />
<ClCompile Include="general_settings.cpp" />
<ClCompile Include="lowlevel_keyboard_event.cpp" />
<ClCompile Include="msi_to_msix_upgrade.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
Expand All @@ -118,6 +121,7 @@
<ClInclude Include="auto_start_helper.h" />
<ClInclude Include="general_settings.h" />
<ClInclude Include="lowlevel_keyboard_event.h" />
<ClInclude Include="msi_to_msix_upgrade.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="powertoys_events.h" />
<ClInclude Include="powertoy_module.h" />
Expand Down
6 changes: 6 additions & 0 deletions src/runner/runner.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<ClCompile Include="restart_elevated.cpp">
<Filter>Utils</Filter>
</ClCompile>
<ClCompile Include="msi_to_msix_upgrade.cpp">
<Filter>Utils</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
Expand Down Expand Up @@ -79,6 +82,9 @@
<ClInclude Include="restart_elevated.h">
<Filter>Utils</Filter>
</ClInclude>
<ClInclude Include="msi_to_msix_upgrade.h">
<Filter>Utils</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Utils">
Expand Down