Debian Codegraph @

mcs/class/referencesource/System/compmod/microsoft/win32/safehandles/SafeProcessHandle.cs

raw
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 /*============================================================
7 **
8 ** Class: SafeProcessHandle
9 **
10 ** A wrapper for a process handle
11 **
12 **
13 ===========================================================*/
14
15 using System;
16 using System.Security;
17 using System.Diagnostics;
18 using System.Security.Permissions;
19 using System.Runtime.InteropServices;
20 using System.Runtime.CompilerServices;
21 using Microsoft.Win32;
22 using Microsoft.Win32.SafeHandles;
23 using System.Runtime.ConstrainedExecution;
24 using System.Runtime.Versioning;
25
26 namespace Microsoft.Win32.SafeHandles {
27 [SuppressUnmanagedCodeSecurityAttribute]
28 public sealed class SafeProcessHandle : SafeHandleZeroOrMinusOneIsInvalid
29 {
30 internal static SafeProcessHandle InvalidHandle = new SafeProcessHandle(IntPtr.Zero);
31
32 // Note that OpenProcess returns 0 on failure
33
34 internal SafeProcessHandle() : base(true) {}
35
36 internal SafeProcessHandle(IntPtr handle) : base(true) {
37 SetHandle(handle);
38 }
39
40 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
41 public SafeProcessHandle(IntPtr existingHandle, bool ownsHandle) : base(ownsHandle) {
42 SetHandle(existingHandle);
43 }
44
45 #if !MONO
46 [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true)]
47 [ResourceExposure(ResourceScope.Machine)]
48 internal static extern SafeProcessHandle OpenProcess(int access, bool inherit, int processId);
49 #endif
50
51 internal void InitialSetHandle(IntPtr h){
52 Debug.Assert(base.IsInvalid, "Safe handle should only be set once");
53 base.handle = h;
54 }
55
56 override protected bool ReleaseHandle()
57 {
58 #if !MONO
59 return SafeNativeMethods.CloseHandle(handle);
60 #else
61 return NativeMethods.CloseProcess (handle);
62 #endif
63 }
64
65 }
66 }
67
68
69
70