Имея данный алгоритм, почему-то большую часть показывает x32. У меня стоит 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);
Как нормально проверить на всех осях разрядность?
Ответ
Решил проблему так:
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);
Вызов: Console.WriteLine($"Версия Вашей ОС: {OSCheckBit.OSBits}");
Комментариев нет:
Отправить комментарий