forked from menees/Libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NativeMethods.Core.cs
45 lines (34 loc) · 1.29 KB
/
NativeMethods.Core.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
namespace Menees.Windows
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
#endregion
internal static partial class NativeMethods
{
#region Public Methods
public static object GetActiveObject(string progId)
{
// This code started from ILSpy's disassembly of .NET Framework's Marshal.GetActiveObject.
// But I changed it to use PreserveSig = true for CLSIDFromProgIDEx since the original code
// just caught any exception and deferred to CLSIDFromProgID.
if (CLSIDFromProgIDEx(progId, out Guid guid) != 0)
{
CLSIDFromProgID(progId, out guid);
}
GetActiveObject(ref guid, IntPtr.Zero, out object result);
return result;
}
#endregion
#region Private Methods
[DllImport("ole32.dll", PreserveSig = false)]
private static extern void CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)] string progId, out Guid clsid);
[DllImport("ole32.dll", PreserveSig = true)]
private static extern int CLSIDFromProgIDEx([MarshalAs(UnmanagedType.LPWStr)] string progId, out Guid clsid);
[DllImport("oleaut32.dll", PreserveSig = false)]
private static extern void GetActiveObject(ref Guid rclsid, IntPtr reserved, [MarshalAs(UnmanagedType.Interface)] out object ppunk);
#endregion
}
}