forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnknownHelpers.cs
More file actions
34 lines (30 loc) · 1 KB
/
UnknownHelpers.cs
File metadata and controls
34 lines (30 loc) · 1 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Runtime.InteropServices;
namespace Microsoft.ClearScript.Util.COM
{
internal static class UnknownHelpers
{
public static IntPtr QueryInterface<T>(IntPtr pUnknown)
{
var iid = typeof(T).GUID;
HResult.Check(Marshal.QueryInterface(pUnknown, ref iid, out var pInterface));
return pInterface;
}
public static IntPtr QueryInterfaceNoThrow<T>(IntPtr pUnknown)
{
var iid = typeof(T).GUID;
var result = Marshal.QueryInterface(pUnknown, ref iid, out var pInterface);
return (result == HResult.S_OK) ? pInterface : IntPtr.Zero;
}
public static void ReleaseAndEmpty(ref IntPtr pInterface)
{
if (pInterface != IntPtr.Zero)
{
Marshal.Release(pInterface);
pInterface = IntPtr.Zero;
}
}
}
}