using System;
using System.Runtime.InteropServices;

namespace LauncherIcarus.Daemon
{
    public static class EnvironmentHelper
    {
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetCurrentProcess();

        [DllImport("kernel32.dll")]
        private static extern IntPtr GetModuleHandle(string moduleName);

        [DllImport("kernel32")]
        private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

        [DllImport("kernel32.dll")]
        private static extern bool IsWow64Process(IntPtr hProcess, out bool wow64Process);

        public static bool Is64BitOperatingSystem()
        {
            if (IntPtr.Size == 8)
                return true;
            IntPtr moduleHandle = EnvironmentHelper.GetModuleHandle("kernel32");
            bool wow64Process;
            return moduleHandle != IntPtr.Zero &&
                   EnvironmentHelper.GetProcAddress(moduleHandle, "IsWow64Process") != IntPtr.Zero &&
                   EnvironmentHelper.IsWow64Process(EnvironmentHelper.GetCurrentProcess(), out wow64Process) &
                   wow64Process;
        }
    }
}