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?
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.
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.
Here is an article from Microsoft with an example, which should support Windows 7 too.
来源:https://stackoverflow.com/questions/52207484/determine-if-c-application-is-running-as-a-uwp-app-with-legacy-support