Reflection and COM

Discussion in 'ASP .Net' started by Alan Seunarayan, Jan 14, 2004.

  1. 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.
    Alan Seunarayan, Jan 14, 2004
    #1
    1. Advertising

  2. 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
    Please reply only to the newsgroup.
    Mattias Sjögren, Jan 14, 2004
    #2
    1. Advertising

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

    "Mattias Sjögren" <> wrote in message
    news:u7$...
    > 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
    > Please reply only to the newsgroup.
    Alan Seunarayan, Jan 14, 2004
    #3
  4. 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

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.
    Mattias Sjögren, Jan 14, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. WebDevHokie

    Re: Reflection and SqlConnection

    WebDevHokie, Jul 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    431
    WebDevHokie
    Jul 10, 2003
  2. WebDevHokie

    Re: Reflection and SqlConnection

    WebDevHokie, Jul 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    400
    WebDevHokie
    Jul 10, 2003
  3. Replies:
    2
    Views:
    484
  4. Andrew Feldman

    Type Reflection and Code Directory

    Andrew Feldman, Dec 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,086
    Andrew Feldman
    Dec 20, 2004
  5. Paul Hatcher

    Medium Level Trust and Reflection

    Paul Hatcher, Feb 25, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    472
    Paul Hatcher
    Feb 25, 2005
Loading...

Share This Page