William
William

Reputation: 31

Determine if C++ application is running as a UWP app, with legacy support

My first thought was to use GetPackageFamilyName() and look for ERROR_SUCCESS vs APPMODEL_ERROR_NO_PACKAGE.

But, I need to support Windows 7, which makes GetPackageFamilyName() unusable.

Is there a decent alternative method? Anything in the Registry, perhaps?

Upvotes: 2

Views: 1288

Answers (3)

Bogdan Mitrache
Bogdan Mitrache

Reputation: 10993

Here is an article from Microsoft with an example, which should support Windows 7 too.

Desktop Bridge – Identify the application’s context

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 595602

Use GetProcAddress() to load GetPackageFamilyName() dynamically at runtime, eg:

typedef LONG WINAPI (*LPFN_GPFN)(HANDLE, UINT32*, PWSTR);
bool bIsUWP = false;

LPFN_GPFN lpGetPackageFamilyName = (LPFN_GPFN) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetPackageFamilyName");
if (lpGetPackageFamilyName)
{
    UINT32 size = 0;
    if (lpGetPackageFamilyName(GetCurrentProcess(), &size, NULL) == ERROR_INSUFFICIENT_BUFFER)
        bIsUWP = true;
}

if (bIsUWP)
{
    //...
}
else
{
    //...
}

Alternatively, consider using one of the GetCurentPackage...() functions (GetCurrentPackageFamilyName(), GetCurrentPackageId(), GetCurrentPackageInfo(), etc) instead of using GetPackageFamilyName() with a HANDLE to the calling process.

Upvotes: 6

Stefan Wick  MSFT
Stefan Wick MSFT

Reputation: 13850

GetPackageFamilyName is the right way. In order to support Windows 7, you can first check if you are running on Win7. If you are, then you know you are not packaged. Only if you are on version >7 then you call GetPackageFamilyName to check whether or not you are packaged.

Upvotes: 2

Related Questions