Having this algorithm, for some reason, the most part shows x32 . I have x64


 private static bool Is64Bit() { return IntPtr.Size == 8; } public static string CheckOS() { if (Is64Bit()) return "x64"; else return "x32"; } [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool IsWow64Process([In] IntPtr hProcess, out bool lpSystemInfo); 

How to check the width on all axes?

  • 2
  • Solved the problem differently) Now everything works as it should) Took OSVersionInfo as a basis - GooliveR
  • @Nikita: Why not as an answer? - VladD
  • @VladD, I thought maybe the author should use WinAPI calls for this task. But ok, I'll take it. - Nikita
  • Well, we do as an answer) - GooliveR

2 answers 2

Starting with version 4 of the framework, there is an easy way to check the OS bit depth using the static class Environment and its Is64BitOperatingSystem property:

 using System; static string GetOSBit() { if (Environment.Is64BitOperatingSystem) return "x64"; else return "x32"; } 

Console.WriteLine(GetOSBit());

    Solved the problem like this:

     internal class OSCheckBit { private static bool Is32BitProcessOn64BitProcessor() { IsWow64ProcessDelegate fnDelegate = GetIsWow64ProcessDelegate(); if (fnDelegate == null) return false; bool isWow64; bool retVal = fnDelegate.Invoke(Process.GetCurrentProcess().Handle, out isWow64); if (retVal == false) return false; return isWow64; } private static IsWow64ProcessDelegate GetIsWow64ProcessDelegate() { IntPtr handle = LoadLibrary("kernel32"); if (handle != IntPtr.Zero) { IntPtr fnPtr = GetProcAddress(handle, "IsWow64Process"); if (fnPtr != IntPtr.Zero) return (IsWow64ProcessDelegate)Marshal.GetDelegateForFunctionPointer((IntPtr)fnPtr, typeof(IsWow64ProcessDelegate)); } return null; } public enum SoftwareArchitecture { Unknown = 0, x32 = 1, x64 = 2 } static public SoftwareArchitecture OSBits { get { SoftwareArchitecture osbits = SoftwareArchitecture.Unknown; switch (IntPtr.Size * 8) { case 64: osbits = SoftwareArchitecture.x64; break; case 32: if (Is32BitProcessOn64BitProcessor()) osbits = SoftwareArchitecture.x64; else osbits = SoftwareArchitecture.x32; break; default: osbits = SoftwareArchitecture.Unknown; break; } return osbits; } } [DllImport("kernel32", SetLastError = true, CallingConvention = CallingConvention.Winapi)] public extern static IntPtr LoadLibrary(string libraryName); [DllImport("kernel32", SetLastError = true, CallingConvention = CallingConvention.Winapi)] public extern static IntPtr GetProcAddress(IntPtr hwnd, string procedureName); private delegate bool IsWow64ProcessDelegate([In] IntPtr handle, [Out] out bool isWow64Process); 

    Call: Console.WriteLine($"Версия Вашей ОС: {OSCheckBit.OSBits}");