-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComMacros.fs
87 lines (78 loc) · 3.81 KB
/
ComMacros.fs
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
82
83
84
85
86
87
namespace Macros
open System
open System.Runtime.InteropServices
/// <summary>
/// Exposes objects, methods and properties to programming tools and other
/// applications that support Automation.
/// </summary>
[<ComImport()>]
[<Guid("00020400-0000-0000-C000-000000000046")>]
[<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>]
[<AllowNullLiteral>]
type IDispatch =
[<PreserveSig>]
abstract member GetTypeInfoCount : [<Out>] Count: byref<int> -> int
[<PreserveSig>]
abstract member GetTypeInfo :
[<MarshalAs(UnmanagedType.U4)>] iTInfo:int *
[<MarshalAs(UnmanagedType.U4)>] lcid:int *
[<Out>] typeInfo:byref<System.Runtime.InteropServices.ComTypes.ITypeInfo> -> int
[<PreserveSig>]
abstract member GetIDsOfNames :
riid : byref<Guid> *
[<MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)>]
rgsNames:string[] *
cNames:int *
lcid:int *
[<MarshalAs(UnmanagedType.LPArray)>] rgDispId:int[] -> int
[<PreserveSig>]
abstract member Invoke :
dispIdMember:int *
riid:byref<Guid> *
lcid:uint32 *
wFlags:uint16 *
pDispParams: byref<System.Runtime.InteropServices.ComTypes.DISPPARAMS> *
[<Out>] pVarResult:byref<obj> *
pExcepInfo: byref<System.Runtime.InteropServices.ComTypes.EXCEPINFO> *
pArgErr : IntPtr[] -> int
module Com =
/// <summary>
/// Returns a string value representing the type name of the specified COM object.
/// </summary>
/// <param name="comObj">A COM object the type name of which to return.</param>
/// <returns>A string containing the type name.</returns>
let GetTypeName(comObj:obj):string =
if comObj = null ||
//The specified object is not a COM object
Marshal.IsComObject(comObj) = false then
String.Empty
else
let mutable dispatch = comObj :?> IDispatch
match dispatch with
| null -> String.Empty //The specified COM object doesn't support getting type information
| _ ->
let typeInfoRef = ref null
try
try
let typeInfo =
try
// obtain the ITypeInfo interface from the object
let _success = dispatch.GetTypeInfo(0, 0, typeInfoRef)
Some !typeInfoRef
with |ex -> (* ex.Dump(); *)
//Cannot get the ITypeInfo interface for the specified COM object
None
if typeInfo.IsNone then String.Empty
else
try
let typeName = ref null
let documentation = ref null
let helpContext = ref 0
let helpFile = ref null
//retrieves the documentation string for the specified type description
let _returnVal = typeInfo.Value.GetDocumentation(-1,typeName,documentation,helpContext,helpFile)
!typeName
with |ex -> String.Empty // Cannot extract ITypeInfo information
with | ex -> String.Empty // Unexpected error
finally
if !typeInfoRef <> null then Marshal.ReleaseComObject(!typeInfoRef) |> ignore