Advertisement
retesere20

64bit detection c# methods

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