-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClipboardRedux.RCW_IAgileReference.cs
55 lines (48 loc) · 1.84 KB
/
ClipboardRedux.RCW_IAgileReference.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
using System;
using System.Runtime.InteropServices;
namespace WindowsFormsClipboardRedux
{
partial class ClipboardRedux
{
internal class RCW_IAgileReference
{
private unsafe struct AgileReferenceVTable
{
#pragma warning disable CS0649
// Due to a bug in .NET Core 3.1 (3.1.18 as of now) we must inline IUnknown interface
// members explicitly, and can't do the following:
//
// public IUnknownVTable UnknownVTable;
public delegate* unmanaged[Stdcall]<IntPtr, Guid*, IntPtr*, HRESULT> QueryInterface;
public delegate* unmanaged[Stdcall]<IntPtr, HRESULT> AddRef;
public delegate* unmanaged[Stdcall]<IntPtr, HRESULT> Release;
// IAgileReference
public delegate* unmanaged[Stdcall]<IntPtr, ref Guid, out IntPtr, HRESULT> Resolve;
#pragma warning restore CS0649
}
private readonly IntPtr _instance;
private readonly unsafe AgileReferenceVTable* _vtable;
public unsafe RCW_IAgileReference(IntPtr instance)
{
_instance = instance;
_vtable = *(AgileReferenceVTable**)instance;
}
// An IAgileReference instance handles release on the correct context.
~RCW_IAgileReference()
{
if (_instance != IntPtr.Zero)
{
Marshal.Release(_instance);
}
}
public unsafe IntPtr Resolve(Guid iid)
{
// Marshal and dispatch
_vtable->Resolve(_instance, ref iid, out IntPtr resolvedInstance)
.ThrowIfFailed();
// Unmarshal
return resolvedInstance;
}
}
}
}