From a8e0b9ccf602ecc994a30005d76c0756e5968473 Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Wed, 25 Sep 2024 19:49:40 +0200 Subject: [PATCH] Fix an exception on startup (#17960) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It bothered me. :) ## Validation Steps Performed * Launch packaged WT. `IsPackaged() == true` ✅ * Launch unpackaged WT. `IsPackaged() == false` ✅ --- src/cascadia/WinRTUtils/inc/WtExeUtils.h | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/cascadia/WinRTUtils/inc/WtExeUtils.h b/src/cascadia/WinRTUtils/inc/WtExeUtils.h index 9728c177d8e..680177fdf9b 100644 --- a/src/cascadia/WinRTUtils/inc/WtExeUtils.h +++ b/src/cascadia/WinRTUtils/inc/WtExeUtils.h @@ -1,22 +1,26 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +#pragma once + constexpr std::wstring_view WtExe{ L"wt.exe" }; constexpr std::wstring_view WtdExe{ L"wtd.exe" }; constexpr std::wstring_view WindowsTerminalExe{ L"WindowsTerminal.exe" }; constexpr std::wstring_view LocalAppDataAppsPath{ L"%LOCALAPPDATA%\\Microsoft\\WindowsApps\\" }; constexpr std::wstring_view ElevateShimExe{ L"elevate-shim.exe" }; +// Forward declared from appmodel.h so that we don't need to pull in that header everywhere. +extern "C" { +WINBASEAPI LONG WINAPI GetCurrentPackageId(UINT32* bufferLength, BYTE* buffer); +} + #ifdef WINRT_Windows_ApplicationModel_H _TIL_INLINEPREFIX bool IsPackaged() { - static const auto isPackaged = []() -> bool { - try - { - const auto package = winrt::Windows::ApplicationModel::Package::Current(); - return true; - } - catch (...) - { - return false; - } + static const auto isPackaged = []() { + UINT32 bufferLength = 0; + const auto hr = GetCurrentPackageId(&bufferLength, nullptr); + return hr != APPMODEL_ERROR_NO_PACKAGE; }(); return isPackaged; }