-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
DesktopBridgeHelpers.cs
99 lines (85 loc) · 3.06 KB
/
DesktopBridgeHelpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#if WIN32
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Windows.ApplicationModel;
namespace Microsoft.Toolkit.Uwp.Notifications
{
/// <summary>
/// Code from https://github.com/qmatteoq/DesktopBridgeHelpers/tree/master/DesktopBridge.Helpers/Helpers.cs
/// </summary>
internal class DesktopBridgeHelpers
{
private const long APPMODEL_ERROR_NO_PACKAGE = 15700L;
private static bool? _hasIdentity;
public static bool HasIdentity()
{
if (_hasIdentity == null)
{
if (IsWindows7OrLower)
{
_hasIdentity = false;
}
else
{
int length = 0;
var sb = new StringBuilder(0);
NativeMethods.GetCurrentPackageFullName(ref length, sb);
sb = new StringBuilder(length);
int error = NativeMethods.GetCurrentPackageFullName(ref length, sb);
_hasIdentity = error != APPMODEL_ERROR_NO_PACKAGE;
}
}
return _hasIdentity.Value;
}
private static bool? _isContainerized;
/// <summary>
/// Returns true if the app is running in a container (MSIX) or false if not running in a container (sparse or plain Win32)
/// </summary>
/// <returns>Boolean</returns>
public static bool IsContainerized()
{
if (_isContainerized == null)
{
// If MSIX or sparse
if (HasIdentity())
{
// Sparse is identified if EXE is running outside of installed package location
var packageInstalledLocation = Package.Current.InstalledLocation.Path;
var actualExeFullPath = Process.GetCurrentProcess().MainModule.FileName;
// If inside package location
if (actualExeFullPath.StartsWith(packageInstalledLocation))
{
_isContainerized = true;
}
else
{
_isContainerized = false;
}
}
// Plain Win32
else
{
_isContainerized = false;
}
}
return _isContainerized.Value;
}
private static bool IsWindows7OrLower
{
get
{
int versionMajor = Environment.OSVersion.Version.Major;
int versionMinor = Environment.OSVersion.Version.Minor;
double version = versionMajor + ((double)versionMinor / 10);
return version <= 6.1;
}
}
}
}
#endif