NativeMethods.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.IO;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. using Microsoft.Win32.SafeHandles;
  6. namespace Microsoft.Win32
  7. {
  8. static class NativeMethods
  9. {
  10. public const int E_ABORT = unchecked ((int)0x80004004);
  11. public const int PROCESS_TERMINATE = 0x0001;
  12. public const int PROCESS_CREATE_THREAD = 0x0002;
  13. public const int PROCESS_SET_SESSIONID = 0x0004;
  14. public const int PROCESS_VM_OPERATION = 0x0008;
  15. public const int PROCESS_VM_READ = 0x0010;
  16. public const int PROCESS_VM_WRITE = 0x0020;
  17. public const int PROCESS_DUP_HANDLE = 0x0040;
  18. public const int PROCESS_CREATE_PROCESS = 0x0080;
  19. public const int PROCESS_SET_QUOTA = 0x0100;
  20. public const int PROCESS_SET_INFORMATION = 0x0200;
  21. public const int PROCESS_QUERY_INFORMATION = 0x0400;
  22. public const int PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
  23. public const int STANDARD_RIGHTS_REQUIRED = 0x000F0000;
  24. public const int SYNCHRONIZE = 0x00100000;
  25. public const int PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFF;
  26. public const int DUPLICATE_CLOSE_SOURCE = 1;
  27. public const int DUPLICATE_SAME_ACCESS = 2;
  28. public const int STILL_ACTIVE = 0x00000103;
  29. public const int WAIT_OBJECT_0 = 0x00000000;
  30. public const int WAIT_FAILED = unchecked((int)0xFFFFFFFF);
  31. public const int WAIT_TIMEOUT = 0x00000102;
  32. public const int WAIT_ABANDONED = 0x00000080;
  33. public const int WAIT_ABANDONED_0 = WAIT_ABANDONED;
  34. public static bool DuplicateHandle(HandleRef hSourceProcessHandle, SafeHandle hSourceHandle, HandleRef hTargetProcess,
  35. out SafeWaitHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions)
  36. {
  37. bool release = false;
  38. try {
  39. hSourceHandle.DangerousAddRef (ref release);
  40. MonoIOError error;
  41. IntPtr nakedTargetHandle;
  42. bool ret = MonoIO.DuplicateHandle (hSourceProcessHandle.Handle, hSourceHandle.DangerousGetHandle (), hTargetProcess.Handle,
  43. out nakedTargetHandle, dwDesiredAccess, bInheritHandle ? 1 : 0, dwOptions, out error);
  44. if (error != MonoIOError.ERROR_SUCCESS)
  45. throw MonoIO.GetException (error);
  46. targetHandle = new SafeWaitHandle (nakedTargetHandle, true);
  47. return ret;
  48. } finally {
  49. if (release)
  50. hSourceHandle.DangerousRelease ();
  51. }
  52. }
  53. public static bool DuplicateHandle(HandleRef hSourceProcessHandle, HandleRef hSourceHandle, HandleRef hTargetProcess,
  54. out SafeProcessHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions)
  55. {
  56. MonoIOError error;
  57. IntPtr nakedTargetHandle;
  58. bool ret = MonoIO.DuplicateHandle (hSourceProcessHandle.Handle, hSourceHandle.Handle, hTargetProcess.Handle,
  59. out nakedTargetHandle, dwDesiredAccess, bInheritHandle ? 1 : 0, dwOptions, out error);
  60. if (error != MonoIOError.ERROR_SUCCESS)
  61. throw MonoIO.GetException (error);
  62. targetHandle = new SafeProcessHandle (nakedTargetHandle, true);
  63. return ret;
  64. }
  65. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  66. public static extern IntPtr GetCurrentProcess();
  67. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  68. public static extern bool GetExitCodeProcess (IntPtr processHandle, out int exitCode);
  69. public static bool GetExitCodeProcess (SafeProcessHandle processHandle, out int exitCode)
  70. {
  71. bool release = false;
  72. try {
  73. processHandle.DangerousAddRef (ref release);
  74. return GetExitCodeProcess (processHandle.DangerousGetHandle (), out exitCode);
  75. } finally {
  76. if (release)
  77. processHandle.DangerousRelease ();
  78. }
  79. }
  80. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  81. public static extern bool TerminateProcess (IntPtr processHandle, int exitCode);
  82. public static bool TerminateProcess (SafeProcessHandle processHandle, int exitCode)
  83. {
  84. bool release = false;
  85. try {
  86. processHandle.DangerousAddRef (ref release);
  87. return TerminateProcess (processHandle.DangerousGetHandle (), exitCode);
  88. } finally {
  89. if (release)
  90. processHandle.DangerousRelease ();
  91. }
  92. }
  93. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  94. public static extern int WaitForInputIdle (IntPtr handle, int milliseconds);
  95. public static int WaitForInputIdle (SafeProcessHandle handle, int milliseconds)
  96. {
  97. bool release = false;
  98. try {
  99. handle.DangerousAddRef (ref release);
  100. return WaitForInputIdle (handle.DangerousGetHandle (), milliseconds);
  101. } finally {
  102. if (release)
  103. handle.DangerousRelease ();
  104. }
  105. }
  106. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  107. public static extern bool GetProcessWorkingSetSize (IntPtr handle, out IntPtr min, out IntPtr max);
  108. public static bool GetProcessWorkingSetSize (SafeProcessHandle handle, out IntPtr min, out IntPtr max)
  109. {
  110. bool release = false;
  111. try {
  112. handle.DangerousAddRef (ref release);
  113. return GetProcessWorkingSetSize (handle.DangerousGetHandle (), out min, out max);
  114. } finally {
  115. if (release)
  116. handle.DangerousRelease ();
  117. }
  118. }
  119. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  120. public static extern bool SetProcessWorkingSetSize (IntPtr handle, IntPtr min, IntPtr max);
  121. public static bool SetProcessWorkingSetSize (SafeProcessHandle handle, IntPtr min, IntPtr max)
  122. {
  123. bool release = false;
  124. try {
  125. handle.DangerousAddRef (ref release);
  126. return SetProcessWorkingSetSize (handle.DangerousGetHandle (), min, max);
  127. } finally {
  128. if (release)
  129. handle.DangerousRelease ();
  130. }
  131. }
  132. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  133. public static extern bool GetProcessTimes (IntPtr handle, out long creation, out long exit, out long kernel, out long user);
  134. public static bool GetProcessTimes (SafeProcessHandle handle, out long creation, out long exit, out long kernel, out long user)
  135. {
  136. bool release = false;
  137. try {
  138. handle.DangerousAddRef (ref release);
  139. return GetProcessTimes (handle.DangerousGetHandle (), out creation, out exit, out kernel, out user);
  140. } finally {
  141. if (release)
  142. handle.DangerousRelease ();
  143. }
  144. }
  145. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  146. public static extern int GetCurrentProcessId ();
  147. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  148. public static extern int GetPriorityClass (IntPtr handle);
  149. public static int GetPriorityClass(SafeProcessHandle handle)
  150. {
  151. bool release = false;
  152. try {
  153. handle.DangerousAddRef (ref release);
  154. return GetPriorityClass (handle.DangerousGetHandle ());
  155. } finally {
  156. if (release)
  157. handle.DangerousRelease ();
  158. }
  159. }
  160. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  161. public static extern bool SetPriorityClass (IntPtr handle, int priorityClass);
  162. public static bool SetPriorityClass(SafeProcessHandle handle, int priorityClass)
  163. {
  164. bool release = false;
  165. try {
  166. handle.DangerousAddRef (ref release);
  167. return SetPriorityClass (handle.DangerousGetHandle (), priorityClass);
  168. } finally {
  169. if (release)
  170. handle.DangerousRelease ();
  171. }
  172. }
  173. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  174. public extern static bool CloseProcess (IntPtr handle);
  175. }
  176. }