forked from menees/Libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComUtility.Core.cs
More file actions
51 lines (41 loc) · 1.38 KB
/
ComUtility.Core.cs
File metadata and controls
51 lines (41 loc) · 1.38 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
namespace Menees.Windows
{
#region Using Directives
using System.Runtime.InteropServices;
#endregion
public static partial class ComUtility
{
#region Public Methods
/// <summary>
/// Performs the final release on a COM object's runtime callable wrapper (RCW).
/// </summary>
/// <param name="instance">The instance to release.</param>
/// <returns>
/// The new value of the reference count of the RCW associated with <paramref name="instance"/>,
/// which is 0 (zero) if the release is successful.
/// </returns>
public static int FinalRelease(object instance)
{
if (instance is ComObject comObject)
{
instance = comObject.Instance;
}
int result = Marshal.FinalReleaseComObject(instance);
return result;
}
#endregion
#region Internal Methods
internal static object GetActiveObject(string progID) => NativeMethods.GetActiveObject(progID);
internal static object EnsureDynamic(object value)
{
// .NET Core 3.x doesn't support dynamic for COM Interop.
// https://github.com/dotnet/runtime/issues/30502#issuecomment-518748077
// To get around that limitation until .NET 5, we have to use a DynamicObject.
// https://github.com/dotnet/runtime/issues/12587#issuecomment-585591984
// https://github.com/dotnet/runtime/issues/12587#issuecomment-534611966
dynamic result = new ComObject(value);
return result;
}
#endregion
}
}