forked from microsoft/CsWinRT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgileReference.cs
More file actions
143 lines (126 loc) · 4.25 KB
/
AgileReference.cs
File metadata and controls
143 lines (126 loc) · 4.25 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;
using WinRT.Interop;
namespace WinRT
{
#if EMBED
internal
#else
public
#endif
class AgileReference : IDisposable
{
private readonly static Guid CLSID_StdGlobalInterfaceTable = new(0x00000323, 0, 0, 0xc0, 0, 0, 0, 0, 0, 0, 0x46);
private readonly static Lazy<IGlobalInterfaceTable> Git = new Lazy<IGlobalInterfaceTable>(() => 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 = IUnknownVftbl.IID;
try
{
Marshal.ThrowExceptionForHR(Platform.RoGetAgileReference(
0 /*AGILEREFERENCE_DEFAULT*/,
ref iid,
thisPtr,
&agileReference));
_agileReference = ObjectReference<IUnknownVftbl>.Attach(ref agileReference);
}
catch (TypeLoadException)
{
_cookie = Git.Value.RegisterInterfaceInGlobal(thisPtr, iid);
}
finally
{
MarshalInterface<IAgileReference>.DisposeAbi(agileReference);
}
}
public IObjectReference Get() => _cookie == IntPtr.Zero ? ABI.WinRT.Interop.IAgileReferenceMethods.Resolve(_agileReference, IUnknownVftbl.IID) : Git.Value?.GetInterfaceFromGlobal(_cookie, IUnknownVftbl.IID);
internal ObjectReference<T> Get<T>(Guid iid) => _cookie == IntPtr.Zero ? ABI.WinRT.Interop.IAgileReferenceMethods.Resolve<T>(_agileReference, iid) : Git.Value?.GetInterfaceFromGlobal(_cookie, IUnknownVftbl.IID)?.As<T>(iid);
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (_cookie != IntPtr.Zero)
{
try
{
Git.Value.RevokeInterfaceFromGlobal(_cookie);
}
catch(ArgumentException)
{
// Revoking cookie from GIT table may fail if apartment is gone.
}
}
disposed = true;
}
}
private static unsafe IGlobalInterfaceTable GetGitTable()
{
Guid gitClsid = CLSID_StdGlobalInterfaceTable;
Guid gitIid = ABI.WinRT.Interop.IGlobalInterfaceTable.IID;
IntPtr gitPtr = default;
try
{
Marshal.ThrowExceptionForHR(Platform.CoCreateInstance(
ref gitClsid,
IntPtr.Zero,
1 /*CLSCTX_INPROC_SERVER*/,
ref gitIid,
&gitPtr));
return ABI.WinRT.Interop.IGlobalInterfaceTable.FromAbi(gitPtr).AsType<ABI.WinRT.Interop.IGlobalInterfaceTable>();
}
finally
{
MarshalInterface<IGlobalInterfaceTable>.DisposeAbi(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);
}
}
}