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

Change hasIdentity to work on Windows10 #232

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
57 changes: 44 additions & 13 deletions packages/win_toast/windows/notification_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,64 @@
//

#include <Windows.h>
#include <appmodel.h>
#include <malloc.h>
#include <stdio.h>
#include <hstring.h>
#include <minappmodel.h>

#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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for later, but seesm the GetCurrentPackageFullName wouldn't working on Win7.

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;
}