// 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.

using System.ComponentModel;
using System.Security;
using System.Security.Principal;

namespace System
{
    internal static class EnvironmentHelpers
    {
        private static volatile bool s_isAppContainerProcess;
        private static volatile bool s_isAppContainerProcessInitalized;
        internal const int TokenIsAppContainer = 29;

        public static bool IsAppContainerProcess
        {
            get
            {
                if (!s_isAppContainerProcessInitalized)
                {
                    s_isAppContainerProcess = HasAppContainerToken();
                    s_isAppContainerProcessInitalized = true;
                }

                return s_isAppContainerProcess;
            }
        }

        [SecuritySafeCritical]
        private static unsafe bool HasAppContainerToken()
        {
            int* dwIsAppContainerPtr = stackalloc int[1];
            uint dwLength = 0;

            using (WindowsIdentity wi = WindowsIdentity.GetCurrent(TokenAccessLevels.Query))
            {
                if (!Interop.Advapi32.GetTokenInformation(wi.Token, TokenIsAppContainer, new IntPtr(dwIsAppContainerPtr), sizeof(int), out dwLength))
                {
                    throw new Win32Exception();
                }
            }

            return (*dwIsAppContainerPtr != 0);
        }
    }
}

// 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.

using System.ComponentModel;
using System.Security;
using System.Security.Principal;

namespace System
{
    internal static class EnvironmentHelpers
    {
        private static volatile bool s_isAppContainerProcess;
        private static volatile bool s_isAppContainerProcessInitalized;
        internal const int TokenIsAppContainer = 29;

        public static bool IsAppContainerProcess
        {
            get
            {
                if(!s_IsAppContainerProcessInitalized) {
                   if(Environment.OSVersion.Platform != PlatformID.Win32NT) {
                       s_IsAppContainerProcess = false;
                   } else if(Environment.OSVersion.Version.Major < 6 || (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor <= 1)) {
                       // Windows 7 or older.
                       s_IsAppContainerProcess = false;
                   } else {
                       s_IsAppContainerProcess = HasAppContainerToken();
                   }
 
                    s_IsAppContainerProcessInitalized = true;
                }
 
                return s_IsAppContainerProcess;
            }
        }

        [SecuritySafeCritical]
        private static unsafe bool HasAppContainerToken()
        {
            int* dwIsAppContainerPtr = stackalloc int[1];
            uint dwLength = 0;

            using (WindowsIdentity wi = WindowsIdentity.GetCurrent(TokenAccessLevels.Query))
            {
                if (!Interop.Advapi32.GetTokenInformation(wi.Token, TokenIsAppContainer, new IntPtr(dwIsAppContainerPtr), sizeof(int), out dwLength))
                {
                    throw new Win32Exception();
                }
            }

            return (*dwIsAppContainerPtr != 0);
        }
    }
}

Fix IsAppContainerProcess for Windows 7
Fix IsAppContainerProcess for Windows 7
