retesere20

64bit-os-detection

Feb 9th, 2020
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. /*
  4. public class Is64BitDetection
  5. {
  6. public static bool Is64BitOperatingSystem()
  7. {
  8. if (IntPtr.Size == 8) // 64-bit programs run only on Win64
  9. {
  10. return true;
  11. }
  12. else // 32-bit programs run on both 32-bit and 64-bit Windows
  13. {
  14. // Detect whether the current process is a 32-bit process running on a 64-bit system.
  15. bool flag;
  16. return ((DoesWin32MethodExist("kernel32.dll", "IsWow64Process") &&
  17. IsWow64Process(GetCurrentProcess(), out flag)) && flag);
  18. }
  19. }
  20.  
  21. static bool DoesWin32MethodExist(string moduleName, string methodName)
  22. {
  23. IntPtr moduleHandle = GetModuleHandle(moduleName);
  24. if (moduleHandle == IntPtr.Zero)
  25. {
  26. return false;
  27. }
  28. return (GetProcAddress(moduleHandle, methodName) != IntPtr.Zero);
  29. }
  30.  
  31. [DllImport("kernel32.dll")]
  32. static extern IntPtr GetCurrentProcess();
  33.  
  34. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  35. static extern IntPtr GetModuleHandle(string moduleName);
  36.  
  37. [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
  38. static extern IntPtr GetProcAddress(IntPtr hModule,
  39. [MarshalAs(UnmanagedType.LPStr)]string procName);
  40.  
  41. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  42. [return: MarshalAs(UnmanagedType.Bool)]
  43. static extern bool IsWow64Process(IntPtr hProcess, out bool wow64Process);
  44.  
  45. static bool is64BitOS = Is64BitOperatingSystem();
  46. // ===========================
  47.  
  48. }
  49. */
  50.  
  51.  
  52. // ============ 64 bit detection ===========//
  53.  
  54. /*
  55. static bool is64BitProcess = (IntPtr.Size == 8);
  56. static bool is64BitOS = is64BitProcess || IsWow64();
  57. [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
  58. [return: MarshalAs(UnmanagedType.Bool)]
  59. private static extern bool IsWow64Process( [In] IntPtr hProcess, [Out] out bool wow64Process );
  60. // lpSystemInfo);
  61.  
  62. public static bool IsWow64()
  63. {
  64. //IntPtr.Size == 4 &&
  65. if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) ||
  66. Environment.OSVersion.Version.Major >= 6)
  67. {
  68. using (Process p = Process.GetCurrentProcess())
  69. {
  70. bool retVal;
  71. if (!IsWow64Process(p.Handle, out retVal))
  72. {
  73. return false;
  74. }
  75. return retVal;
  76. }
  77. }
  78. else
  79. {
  80. return false;
  81. }
  82. }
  83. */
RAW Paste Data