4 using System.Runtime.CompilerServices;
5 using System.Runtime.InteropServices;
6 using Microsoft.Win32.SafeHandles;
8 namespace Microsoft.Win32
10 static class NativeMethods
12 public const int E_ABORT = unchecked ((int)0x80004004);
14 public const int PROCESS_TERMINATE = 0x0001;
15 public const int PROCESS_CREATE_THREAD = 0x0002;
16 public const int PROCESS_SET_SESSIONID = 0x0004;
17 public const int PROCESS_VM_OPERATION = 0x0008;
18 public const int PROCESS_VM_READ = 0x0010;
19 public const int PROCESS_VM_WRITE = 0x0020;
20 public const int PROCESS_DUP_HANDLE = 0x0040;
21 public const int PROCESS_CREATE_PROCESS = 0x0080;
22 public const int PROCESS_SET_QUOTA = 0x0100;
23 public const int PROCESS_SET_INFORMATION = 0x0200;
24 public const int PROCESS_QUERY_INFORMATION = 0x0400;
25 public const int PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
26 public const int STANDARD_RIGHTS_REQUIRED = 0x000F0000;
27 public const int SYNCHRONIZE = 0x00100000;
28 public const int PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFF;
30 public const int DUPLICATE_CLOSE_SOURCE = 1;
31 public const int DUPLICATE_SAME_ACCESS = 2;
33 public const int STILL_ACTIVE = 0x00000103;
35 public const int WAIT_OBJECT_0 = 0x00000000;
36 public const int WAIT_FAILED = unchecked((int)0xFFFFFFFF);
37 public const int WAIT_TIMEOUT = 0x00000102;
38 public const int WAIT_ABANDONED = 0x00000080;
39 public const int WAIT_ABANDONED_0 = WAIT_ABANDONED;
41 public const int ERROR_FILE_NOT_FOUND = 2;
42 public const int ERROR_PATH_NOT_FOUND = 3;
43 public const int ERROR_ACCESS_DENIED = 5;
44 public const int ERROR_INVALID_HANDLE = 6;
45 public const int ERROR_SHARING_VIOLATION = 32;
46 public const int ERROR_INVALID_NAME = 0x7B;
47 public const int ERROR_ALREADY_EXISTS = 183;
48 public const int ERROR_FILENAME_EXCED_RANGE = 0xCE;
50 public static bool DuplicateHandle(HandleRef hSourceProcessHandle, SafeHandle hSourceHandle, HandleRef hTargetProcess,
51 out SafeWaitHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions)
55 hSourceHandle.DangerousAddRef (ref release);
58 IntPtr nakedTargetHandle;
59 bool ret = MonoIO.DuplicateHandle (hSourceProcessHandle.Handle, hSourceHandle.DangerousGetHandle (), hTargetProcess.Handle,
60 out nakedTargetHandle, dwDesiredAccess, bInheritHandle ? 1 : 0, dwOptions, out error);
62 if (error != MonoIOError.ERROR_SUCCESS)
63 throw MonoIO.GetException (error);
65 targetHandle = new SafeWaitHandle (nakedTargetHandle, true);
69 hSourceHandle.DangerousRelease ();
73 public static bool DuplicateHandle(HandleRef hSourceProcessHandle, HandleRef hSourceHandle, HandleRef hTargetProcess,
74 out SafeProcessHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions)
77 IntPtr nakedTargetHandle;
78 bool ret = MonoIO.DuplicateHandle (hSourceProcessHandle.Handle, hSourceHandle.Handle, hTargetProcess.Handle,
79 out nakedTargetHandle, dwDesiredAccess, bInheritHandle ? 1 : 0, dwOptions, out error);
81 if (error != MonoIOError.ERROR_SUCCESS)
82 throw MonoIO.GetException (error);
84 targetHandle = new SafeProcessHandle (nakedTargetHandle, true);
88 [MethodImplAttribute (MethodImplOptions.InternalCall)]
89 public static extern IntPtr GetCurrentProcess();
91 [MethodImplAttribute (MethodImplOptions.InternalCall)]
92 public static extern bool GetExitCodeProcess (IntPtr processHandle, out int exitCode);
94 public static bool GetExitCodeProcess (SafeProcessHandle processHandle, out int exitCode)
98 processHandle.DangerousAddRef (ref release);
99 return GetExitCodeProcess (processHandle.DangerousGetHandle (), out exitCode);
102 processHandle.DangerousRelease ();
106 [MethodImplAttribute (MethodImplOptions.InternalCall)]
107 public static extern bool TerminateProcess (IntPtr processHandle, int exitCode);
109 public static bool TerminateProcess (SafeProcessHandle processHandle, int exitCode)
111 bool release = false;
113 processHandle.DangerousAddRef (ref release);
114 return TerminateProcess (processHandle.DangerousGetHandle (), exitCode);
117 processHandle.DangerousRelease ();
121 [MethodImplAttribute (MethodImplOptions.InternalCall)]
122 public static extern int WaitForInputIdle (IntPtr handle, int milliseconds);
124 public static int WaitForInputIdle (SafeProcessHandle handle, int milliseconds)
126 bool release = false;
128 handle.DangerousAddRef (ref release);
129 return WaitForInputIdle (handle.DangerousGetHandle (), milliseconds);
132 handle.DangerousRelease ();
136 [MethodImplAttribute (MethodImplOptions.InternalCall)]
137 public static extern bool GetProcessWorkingSetSize (IntPtr handle, out IntPtr min, out IntPtr max);
139 public static bool GetProcessWorkingSetSize (SafeProcessHandle handle, out IntPtr min, out IntPtr max)
141 bool release = false;
143 handle.DangerousAddRef (ref release);
144 return GetProcessWorkingSetSize (handle.DangerousGetHandle (), out min, out max);
147 handle.DangerousRelease ();
151 [MethodImplAttribute (MethodImplOptions.InternalCall)]
152 public static extern bool SetProcessWorkingSetSize (IntPtr handle, IntPtr min, IntPtr max);
154 public static bool SetProcessWorkingSetSize (SafeProcessHandle handle, IntPtr min, IntPtr max)
156 bool release = false;
158 handle.DangerousAddRef (ref release);
159 return SetProcessWorkingSetSize (handle.DangerousGetHandle (), min, max);
162 handle.DangerousRelease ();
166 [MethodImplAttribute (MethodImplOptions.InternalCall)]
167 public static extern bool GetProcessTimes (IntPtr handle, out long creation, out long exit, out long kernel, out long user);
169 public static bool GetProcessTimes (SafeProcessHandle handle, out long creation, out long exit, out long kernel, out long user)
171 bool release = false;
173 handle.DangerousAddRef (ref release);
174 return GetProcessTimes (handle.DangerousGetHandle (), out creation, out exit, out kernel, out user);
177 handle.DangerousRelease ();
181 [MethodImplAttribute (MethodImplOptions.InternalCall)]
182 public static extern int GetCurrentProcessId ();
184 [MethodImplAttribute (MethodImplOptions.InternalCall)]
185 public static extern int GetPriorityClass (IntPtr handle);
187 public static int GetPriorityClass(SafeProcessHandle handle)
189 bool release = false;
191 handle.DangerousAddRef (ref release);
192 return GetPriorityClass (handle.DangerousGetHandle ());
195 handle.DangerousRelease ();
199 [MethodImplAttribute (MethodImplOptions.InternalCall)]
200 public static extern bool SetPriorityClass (IntPtr handle, int priorityClass);
202 public static bool SetPriorityClass(SafeProcessHandle handle, int priorityClass)
204 bool release = false;
206 handle.DangerousAddRef (ref release);
207 return SetPriorityClass (handle.DangerousGetHandle (), priorityClass);
210 handle.DangerousRelease ();
214 [MethodImplAttribute(MethodImplOptions.InternalCall)]
215 public extern static bool CloseProcess (IntPtr handle);