Groups | Blog | Home
all groups > dotnet faqs > january 2004 >

dotnet faqs : Reflection and COM


Alan Seunarayan
1/14/2004 2:45:55 PM
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 =3D =
Activator.CreateInstance(Type.CreateFromProgID("Word.Application"))

Can anyone help?


Cheers,

Mattias Sjögren
1/14/2004 4:15:31 PM
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Alan Seunarayan
1/14/2004 7:29:24 PM
thanks for the reply, is there any chance of some example code please?

[quoted text, click to view]

Mattias Sjögren
1/14/2004 11:56:52 PM
Alan,

[quoted text, click to view]

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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
AddThis Social Bookmark Button