Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SafeNCryptHandle.cs
Go to the documentation of this file.
1using System;
4
6
7public abstract class SafeNCryptHandle : SafeHandleZeroOrMinusOneIsInvalid
8{
9 private enum OwnershipState
10 {
11 Owner,
13 Holder
14 }
15
17
19
21
23 {
24 get
25 {
26 return _holder;
27 }
28 [param: DisallowNull]
29 set
30 {
31 _holder = value;
33 }
34 }
35
36 protected SafeNCryptHandle()
37 : base(ownsHandle: true)
38 {
39 }
40
41 protected SafeNCryptHandle(IntPtr handle, SafeHandle parentHandle)
42 : base(ownsHandle: true)
43 {
44 if (parentHandle == null)
45 {
46 throw new ArgumentNullException("parentHandle");
47 }
48 if (parentHandle.IsClosed || parentHandle.IsInvalid)
49 {
51 }
52 bool success = false;
53 parentHandle.DangerousAddRef(ref success);
54 _parentHandle = parentHandle;
56 if (IsInvalid)
57 {
59 _parentHandle = null;
60 }
61 }
62
63 internal T Duplicate<T>() where T : SafeNCryptHandle, new()
64 {
66 {
68 }
70 }
71
73 {
74 bool success = false;
75 T val = new T();
76 Holder.DangerousAddRef(ref success);
77 val.SetHandle(Holder.DangerousGetHandle());
78 val.Holder = Holder;
79 return val;
80 }
81
82 private T DuplicateOwnerHandle<T>() where T : SafeNCryptHandle, new()
83 {
84 bool success = false;
85 T val = new T();
86 T val2 = new T();
87 val._ownershipState = OwnershipState.Holder;
88 val.SetHandle(DangerousGetHandle());
90 if (_parentHandle != null)
91 {
92 val._parentHandle = _parentHandle;
93 _parentHandle = null;
94 }
95 Holder = val;
96 val.DangerousAddRef(ref success);
97 val2.SetHandle(val.DangerousGetHandle());
98 val2.Holder = val;
99 return val2;
100 }
101
102 protected override bool ReleaseHandle()
103 {
104 if (_ownershipState == OwnershipState.Duplicate)
105 {
107 return true;
108 }
109 if (_parentHandle != null)
110 {
112 return true;
113 }
114 return ReleaseNativeHandle();
115 }
116
117 protected abstract bool ReleaseNativeHandle();
118
120 {
121 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptFreeObject(handle);
122 return errorCode == global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS;
123 }
124}
SafeNCryptHandle(IntPtr handle, SafeHandle parentHandle)
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
void DangerousAddRef(ref bool success)
Definition SafeHandle.cs:76
static string Argument_Invalid_SafeHandleInvalidOrClosed
Definition SR.cs:16
Definition SR.cs:7