-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMarshalHelper.vb
More file actions
51 lines (41 loc) · 1.41 KB
/
MarshalHelper.vb
File metadata and controls
51 lines (41 loc) · 1.41 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
Imports System.Runtime.InteropServices
Public Class MarshalHelper
Public Shared Function GetActiveObject(
ByVal progId As String,
ByVal Optional throwOnError As Boolean = False
) As Object
If progId Is Nothing Then
Throw New ArgumentNullException(NameOf(progId))
End If
Dim clsid = Nothing
Dim hr = CLSIDFromProgIDEx(progId, clsid)
If hr < 0 Then
If throwOnError Then
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr)
End If
Return Nothing
End If
Dim obj As Object = Nothing
hr = GetActiveObject(clsid, IntPtr.Zero, obj)
If hr < 0 Then
If throwOnError Then
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr)
End If
Return Nothing
End If
Return obj
End Function
<DllImport("ole32")>
Private Shared Function CLSIDFromProgIDEx(
<MarshalAs(UnmanagedType.LPWStr)> ByVal lpszProgID As String,
<Out> ByRef lpclsid As Guid
) As Integer
End Function
<DllImport("oleaut32")>
Private Shared Function GetActiveObject(
<MarshalAs(UnmanagedType.LPStruct)> ByVal rclsid As Guid,
ByVal pvReserved As IntPtr,
<Out> <MarshalAs(UnmanagedType.IUnknown)> ByRef ppunk As Object
) As Integer
End Function
End Class