diff --git a/packages/win_toast/windows/notification_manager.cc b/packages/win_toast/windows/notification_manager.cc index e7af526a..b80fdf8f 100644 --- a/packages/win_toast/windows/notification_manager.cc +++ b/packages/win_toast/windows/notification_manager.cc @@ -3,33 +3,64 @@ // #include +#include +#include +#include #include #include #include "notification_manager.h" #include "dll_importer.h" -namespace { +namespace +{ -bool _checkedHasIdentity = false; -bool _hasIdentity = false; + bool _checkedHasIdentity = false; + bool _hasIdentity = false; -bool hasIdentity() { - // https://stackoverflow.com/questions/39609643/determine-if-c-application-is-running-as-a-uwp-app-in-desktop-bridge-project - UINT32 length; - wchar_t packageFamilyName[PACKAGE_FAMILY_NAME_MAX_LENGTH + 1]; - LONG result = DllImporter::GetPackageFamilyName(GetCurrentProcess(), &length, packageFamilyName); - return result == ERROR_SUCCESS; -} + // https://learn.microsoft.com/en-us/windows/msix/detect-package-identity + bool hasIdentity() + { + UINT32 length = 0; + LONG rc = GetCurrentPackageFullName(&length, NULL); + if (rc != ERROR_INSUFFICIENT_BUFFER) + { + if (rc == APPMODEL_ERROR_NO_PACKAGE) + wprintf(L"Process has no package identity\n"); + else + wprintf(L"Error %d in GetCurrentPackageFullName\n", rc); + return false; + } + + PWSTR fullName = (PWSTR)malloc(length * sizeof(*fullName)); + if (fullName == NULL) + { + wprintf(L"Error allocating memory\n"); + return false; + } + + rc = GetCurrentPackageFullName(&length, fullName); + if (rc != ERROR_SUCCESS) + { + wprintf(L"Error %d retrieving PackageFullName\n", rc); + return false; + } + wprintf(L"%s\n", fullName); + + free(fullName); + + return true; + } } -bool NotificationManager::HasIdentity() { - if (!_checkedHasIdentity) { +bool NotificationManager::HasIdentity() +{ + if (!_checkedHasIdentity) + { _hasIdentity = hasIdentity(); _checkedHasIdentity = true; } return _hasIdentity; } -