-
Notifications
You must be signed in to change notification settings - Fork 107
/
AgileReference.cs
189 lines (168 loc) · 5.39 KB
/
AgileReference.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using WinRT.Interop;
namespace WinRT
{
#if EMBED
internal
#else
public
#endif
class AgileReference : IDisposable
{
private static readonly Guid CLSID_StdGlobalInterfaceTable = new(0x00000323, 0, 0, 0xc0, 0, 0, 0, 0, 0, 0, 0x46);
private static readonly object _lock = new();
#if NET
private static volatile ABI.WinRT.Interop.IGlobalInterfaceTable _git;
#else
private static volatile global::WinRT.Interop.IGlobalInterfaceTable _git;
#endif
// Simple singleton lazy-initialization scheme (and saving the Lazy<T> size)
#if NET
internal static ABI.WinRT.Interop.IGlobalInterfaceTable Git
#else
internal static global::WinRT.Interop.IGlobalInterfaceTable Git
#endif
{
get
{
return _git ?? Git_Slow();
[MethodImpl(MethodImplOptions.NoInlining)]
#if NET
static ABI.WinRT.Interop.IGlobalInterfaceTable Git_Slow()
#else
static global::WinRT.Interop.IGlobalInterfaceTable Git_Slow()
#endif
{
lock (_lock)
{
return _git ??= GetGitTable();
}
}
}
}
private readonly IObjectReference _agileReference;
private readonly IntPtr _cookie;
private bool disposed;
public AgileReference(IObjectReference instance)
: this(instance?.ThisPtr ?? IntPtr.Zero)
{
}
internal AgileReference(in ObjectReferenceValue instance)
: this(instance.GetAbi())
{
}
internal unsafe AgileReference(IntPtr thisPtr)
{
if (thisPtr == IntPtr.Zero)
{
return;
}
IntPtr agileReference = default;
Guid iid = IID.IID_IUnknown;
try
{
Marshal.ThrowExceptionForHR(Platform.RoGetAgileReference(
0 /*AGILEREFERENCE_DEFAULT*/,
&iid,
thisPtr,
&agileReference));
_agileReference = ObjectReference<IUnknownVftbl>.Attach(ref agileReference, IID.IID_IUnknown);
}
catch (TypeLoadException)
{
_cookie = Git.RegisterInterfaceInGlobal(thisPtr, iid);
}
finally
{
MarshalInterface<global::WinRT.Interop.IAgileReference>.DisposeAbi(agileReference);
}
}
public IObjectReference Get() => _cookie == IntPtr.Zero ? ABI.WinRT.Interop.IAgileReferenceMethods.Resolve(_agileReference, IID.IID_IUnknown) : Git.GetInterfaceFromGlobal(_cookie, IID.IID_IUnknown);
internal ObjectReference<T> Get<T>(Guid iid) => _cookie == IntPtr.Zero ? ABI.WinRT.Interop.IAgileReferenceMethods.Resolve<T>(_agileReference, iid) : Git.GetInterfaceFromGlobal(_cookie, IID.IID_IUnknown)?.As<T>(iid);
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (_cookie != IntPtr.Zero)
{
#if NET
Git.TryRevokeInterfaceFromGlobal(_cookie);
#else
try
{
Git.RevokeInterfaceFromGlobal(_cookie);
}
catch (ArgumentException)
{
// Revoking cookie from GIT table may fail if apartment is gone.
}
#endif
}
disposed = true;
}
}
#if NET
private static unsafe ABI.WinRT.Interop.IGlobalInterfaceTable GetGitTable()
#else
private static unsafe global::WinRT.Interop.IGlobalInterfaceTable GetGitTable()
#endif
{
Guid gitClsid = CLSID_StdGlobalInterfaceTable;
Guid gitIid = IID.IID_IGlobalInterfaceTable;
IntPtr gitPtr = default;
try
{
Marshal.ThrowExceptionForHR(Platform.CoCreateInstance(
&gitClsid,
IntPtr.Zero,
1 /*CLSCTX_INPROC_SERVER*/,
&gitIid,
&gitPtr));
#if NET
return new ABI.WinRT.Interop.IGlobalInterfaceTable(gitPtr);
#else
return new ABI.WinRT.Interop.IGlobalInterfaceTable(ABI.WinRT.Interop.IGlobalInterfaceTable.FromAbi(gitPtr));
#endif
}
finally
{
MarshalExtensions.ReleaseIfNotNull(gitPtr);
}
}
~AgileReference()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
#if EMBED
internal
#else
public
#endif
sealed class AgileReference<T> : AgileReference
where T : class
{
public AgileReference(IObjectReference instance)
: base(instance)
{
}
internal AgileReference(in ObjectReferenceValue instance)
: base(instance)
{
}
public new T Get()
{
using var objRef = base.Get();
return ComWrappersSupport.CreateRcwForComObject<T>(objRef?.ThisPtr ?? IntPtr.Zero);
}
}
}