Reflection and COM

A

Alan Seunarayan

Hello all,
I am developing a component that uses late-binding to automate Word (as targets have differing versions) and I have created an instance of Word.Application. What I wish to do is to find out the details of the object that I am creating via reflection. At the moment I am creating the Word COM as follows....

Dim oWordX as object = Activator.CreateInstance(Type.CreateFromProgID("Word.Application"))

Can anyone help?


Cheers,

Alan S.
 
M

Mattias Sjögren

Alan,

Reflection only understands managed metadata, not COM type info. So
you'll have to reflect on types in an interop assembly for that to
work.

COM type info can be read with (UCOM)ITypeInfo and related
interfaces (in System.Runtime.InteropServices).



Mattias
 
M

Mattias Sjögren

Alan,
thanks for the reply, is there any chance of some example code please?

Here's some code that prints all names of proporties and methods on
the Word Application object. Error handling excluded for brevity.

<ComImport, Guid("00020400-0000-0000-C000-000000000046"), _
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
Interface IDispatch
Function GetTypeInfoCount() As Integer
Function GetTypeInfo(iTInfo As Integer, lcid As Integer) As
UCOMITypeInfo
Sub _GetIDsOfNames()
Sub _Invoke()
End Interface

Class Test

Shared SUb Main()
Dim t as Type = Type.GetTypeFromProgID("Word.Application")
Dim word As Object = Activator.CreateInstance(t)
Dim disp As IDispatch = DirectCast(word, IDispatch)
If disp.GetTypeInfoCount() = 1 Then
Dim ti As UCOMITypeInfo = disp.GetTypeInfo(0, 0)
Dim pti As IntPtr
ti.GetTypeAttr(pti)
Dim ta As TYPEATTR = DirectCast( _
Marshal.PtrToStructure(pti, GetType(TYPEATTR)), TYPEATTR)
Dim funcs As Integer = ta.CFuncs
ti.ReleaseTypeAttr(pti)
For i As Integer = 0 To funcs - 1
Dim pfd As IntPtr
ti.GetFuncDesc(i, pfd)
Dim fd As FUNCDESC = DirectCast( _
Marshal.PtrToStructure(pfd, GetType(FUNCDESC)), FUNCDESC)
Dim names(0) As String
Dim returned As Integer
ti.GetNames(fd.Memid, names, names.Length, returned)
Console.WriteLine(names(0))
ti.ReleaseFuncDesc(pfd)
Next
End If
Marshal.ReleaseComObject( word )
End Sub
End Class



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top