Determine if C++ application is running as a UWP a

2019-04-15 17:42发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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

Desktop Bridge – Identify the application’s context