ProcessHelper.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Runtime.InteropServices;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. namespace SMBLibrary.Win32
  12. {
  13. public class ProcessHelper
  14. {
  15. private static bool? m_is64BitProcess;
  16. private static bool? m_isWow64Process;
  17. [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
  18. [return: MarshalAs(UnmanagedType.Bool)]
  19. private static extern bool IsWow64Process(
  20. [In] IntPtr hProcess,
  21. [Out] out bool wow64Process
  22. );
  23. public static bool IsWow64Process(Process process)
  24. {
  25. if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) ||
  26. Environment.OSVersion.Version.Major >= 6)
  27. {
  28. bool retVal;
  29. if (!IsWow64Process(process.Handle, out retVal))
  30. {
  31. return false;
  32. }
  33. return retVal;
  34. }
  35. else
  36. {
  37. return false;
  38. }
  39. }
  40. public static bool IsWow64Process()
  41. {
  42. if (!m_isWow64Process.HasValue)
  43. {
  44. using (Process process = Process.GetCurrentProcess())
  45. {
  46. m_isWow64Process = IsWow64Process(process);
  47. }
  48. }
  49. return m_isWow64Process.Value;
  50. }
  51. public static bool Is64BitProcess
  52. {
  53. get
  54. {
  55. if (!m_is64BitProcess.HasValue)
  56. {
  57. m_is64BitProcess = (IntPtr.Size == 8);
  58. }
  59. return m_is64BitProcess.Value;
  60. }
  61. }
  62. public static bool Is64BitOperatingSystem
  63. {
  64. get
  65. {
  66. return Is64BitProcess || IsWow64Process();
  67. }
  68. }
  69. }
  70. }