-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathContext.cs
More file actions
81 lines (74 loc) · 2.81 KB
/
Context.cs
File metadata and controls
81 lines (74 loc) · 2.81 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;
using ABI.WinRT.Interop;
using WinRT.Interop;
namespace WinRT
{
internal static partial class Context
{
public unsafe static IntPtr GetContextToken()
{
IntPtr contextToken;
Marshal.ThrowExceptionForHR(Platform.CoGetContextToken(&contextToken));
return contextToken;
}
public static unsafe IntPtr GetContextCallback()
{
Guid iid = IID.IID_IContextCallback;
IntPtr contextCallbackPtr;
Marshal.ThrowExceptionForHR(Platform.CoGetObjectContext(&iid, &contextCallbackPtr));
return contextCallbackPtr;
}
// Calls the given callback in the right context.
// On any exception, calls onFail callback if any set.
// If not set, exception is handled due to today we don't
// have any scenario to propagate it from here.
//
// On modern .NET, we can use function pointers to avoid the
// small binary size increase from all generated fields and
// logic to cache delegates, since we don't need any of that.
public unsafe static void CallInContext(
IntPtr contextCallbackPtr,
IntPtr contextToken,
#if NET && CsWinRT_LANG_11_FEATURES
delegate*<object, void> callback,
delegate*<object, void> onFailCallback,
#else
Action<object> callback,
Action<object> onFailCallback,
#endif
object state)
{
// Check if we are already on the same context, if so we do not need to switch.
if(contextCallbackPtr == IntPtr.Zero || GetContextToken() == contextToken)
{
callback(state);
return;
}
#if NET && CsWinRT_LANG_11_FEATURES
IContextCallbackVftbl.ContextCallback(contextCallbackPtr, callback, onFailCallback, state);
#else
ComCallData data = default;
var contextCallback = new ABI.WinRT.Interop.IContextCallback(ObjectReference<ABI.WinRT.Interop.IContextCallback.Vftbl>.FromAbi(contextCallbackPtr));
try
{
contextCallback.ContextCallback(_ =>
{
callback(state);
return 0;
}, &data, IID.IID_ICallbackWithNoReentrancyToApplicationSTA, 5);
}
catch (Exception)
{
onFailCallback?.Invoke(state);
}
#endif
}
public static void DisposeContextCallback(IntPtr contextCallbackPtr)
{
MarshalInspectable<object>.DisposeAbi(contextCallbackPtr);
}
}
}