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

让人想犯罪 __ 提交于 2019-12-04 18:40:48

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.

Bogdan Mitrache

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

Desktop Bridge – Identify the application’s context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!