Merge pull request #3040 from xmcclure/debugger-step-recursive
[mono.git] / mcs / class / System / ReferenceSources / NativeMethods.cs
1
2 using System;
3 using System.IO;
4 using System.Runtime.CompilerServices;
5 using System.Runtime.InteropServices;
6 using Microsoft.Win32.SafeHandles;
7
8 namespace Microsoft.Win32
9 {
10         static class NativeMethods
11         {
12                 public const int E_ABORT = unchecked ((int)0x80004004);        
13
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;
29
30                 public const int DUPLICATE_CLOSE_SOURCE = 1;
31                 public const int DUPLICATE_SAME_ACCESS = 2;
32
33                 public const int STILL_ACTIVE = 0x00000103;
34
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;
40
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;
49
50                 public static bool DuplicateHandle(HandleRef hSourceProcessHandle, SafeHandle hSourceHandle, HandleRef hTargetProcess,
51                         out SafeWaitHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions)
52                 {
53                         bool release = false;
54                         try {
55                                 hSourceHandle.DangerousAddRef (ref release);
56
57                                 MonoIOError error;
58                                 IntPtr nakedTargetHandle;
59                                 bool ret = MonoIO.DuplicateHandle (hSourceProcessHandle.Handle, hSourceHandle.DangerousGetHandle (), hTargetProcess.Handle,
60                                         out nakedTargetHandle, dwDesiredAccess, bInheritHandle ? 1 : 0, dwOptions, out error);
61
62                                 if (error != MonoIOError.ERROR_SUCCESS)
63                                         throw MonoIO.GetException (error);
64
65                                 targetHandle = new SafeWaitHandle (nakedTargetHandle, true);
66                                 return ret;
67                         } finally {
68                                 if (release)
69                                         hSourceHandle.DangerousRelease ();
70                         }
71                 }
72
73                 public static bool DuplicateHandle(HandleRef hSourceProcessHandle, HandleRef hSourceHandle, HandleRef hTargetProcess,
74                         out SafeProcessHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions)
75                 {
76                                 MonoIOError error;
77                                 IntPtr nakedTargetHandle;
78                                 bool ret = MonoIO.DuplicateHandle (hSourceProcessHandle.Handle, hSourceHandle.Handle, hTargetProcess.Handle,
79                                         out nakedTargetHandle, dwDesiredAccess, bInheritHandle ? 1 : 0, dwOptions, out error);
80
81                                 if (error != MonoIOError.ERROR_SUCCESS)
82                                         throw MonoIO.GetException (error);
83
84                                 targetHandle = new SafeProcessHandle (nakedTargetHandle, true);
85                                 return ret;
86                 }
87
88                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
89                 public static extern IntPtr GetCurrentProcess();
90
91                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
92                 public static extern bool GetExitCodeProcess (IntPtr processHandle, out int exitCode);
93
94                 public static bool GetExitCodeProcess (SafeProcessHandle processHandle, out int exitCode)
95                 {
96                         bool release = false;
97                         try {
98                                 processHandle.DangerousAddRef (ref release);
99                                 return GetExitCodeProcess (processHandle.DangerousGetHandle (), out exitCode);
100                         } finally {
101                                 if (release)
102                                         processHandle.DangerousRelease ();
103                         }
104                 }
105
106                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
107                 public static extern bool TerminateProcess (IntPtr processHandle, int exitCode);
108
109                 public static bool TerminateProcess (SafeProcessHandle processHandle, int exitCode)
110                 {
111                         bool release = false;
112                         try {
113                                 processHandle.DangerousAddRef (ref release);
114                                 return TerminateProcess (processHandle.DangerousGetHandle (), exitCode);
115                         } finally {
116                                 if (release)
117                                         processHandle.DangerousRelease ();
118                         }
119                 }
120
121                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
122                 public static extern int WaitForInputIdle (IntPtr handle, int milliseconds);
123
124                 public static int WaitForInputIdle (SafeProcessHandle handle, int milliseconds)
125                 {
126                         bool release = false;
127                         try {
128                                 handle.DangerousAddRef (ref release);
129                                 return WaitForInputIdle (handle.DangerousGetHandle (), milliseconds);
130                         } finally {
131                                 if (release)
132                                         handle.DangerousRelease ();
133                         }
134                 }
135
136                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
137                 public static extern bool GetProcessWorkingSetSize (IntPtr handle, out IntPtr min, out IntPtr max);
138
139                 public static bool GetProcessWorkingSetSize (SafeProcessHandle handle, out IntPtr min, out IntPtr max)
140                 {
141                         bool release = false;
142                         try {
143                                 handle.DangerousAddRef (ref release);
144                                 return GetProcessWorkingSetSize (handle.DangerousGetHandle (), out min, out max);
145                         } finally {
146                                 if (release)
147                                         handle.DangerousRelease ();
148                         }
149                 }
150
151                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
152                 public static extern bool SetProcessWorkingSetSize (IntPtr handle, IntPtr min, IntPtr max);
153
154                 public static bool SetProcessWorkingSetSize (SafeProcessHandle handle, IntPtr min, IntPtr max)
155                 {
156                         bool release = false;
157                         try {
158                                 handle.DangerousAddRef (ref release);
159                                 return SetProcessWorkingSetSize (handle.DangerousGetHandle (), min, max);
160                         } finally {
161                                 if (release)
162                                         handle.DangerousRelease ();
163                         }
164                 }
165
166                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
167                 public static extern bool GetProcessTimes (IntPtr handle, out long creation, out long exit, out long kernel, out long user);
168
169                 public static bool GetProcessTimes (SafeProcessHandle handle, out long creation, out long exit, out long kernel, out long user)
170                 {
171                         bool release = false;
172                         try {
173                                 handle.DangerousAddRef (ref release);
174                                 return GetProcessTimes (handle.DangerousGetHandle (), out creation, out exit, out kernel, out user);
175                         } finally {
176                                 if (release)
177                                         handle.DangerousRelease ();
178                         }
179                 }
180
181                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
182                 public static extern int GetCurrentProcessId ();
183
184                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
185                 public static extern int GetPriorityClass (IntPtr handle);
186
187                 public static int GetPriorityClass(SafeProcessHandle handle)
188                 {
189                         bool release = false;
190                         try {
191                                 handle.DangerousAddRef (ref release);
192                                 return GetPriorityClass (handle.DangerousGetHandle ());
193                         } finally {
194                                 if (release)
195                                         handle.DangerousRelease ();
196                         }
197                 }
198
199                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
200                 public static extern bool SetPriorityClass (IntPtr handle, int priorityClass);
201
202                 public static bool SetPriorityClass(SafeProcessHandle handle, int priorityClass)
203                 {
204                         bool release = false;
205                         try {
206                                 handle.DangerousAddRef (ref release);
207                                 return SetPriorityClass (handle.DangerousGetHandle (), priorityClass);
208                         } finally {
209                                 if (release)
210                                         handle.DangerousRelease ();
211                         }
212                 }
213
214                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
215                 public extern static bool CloseProcess (IntPtr handle);
216         }
217 }