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. Advertisements

  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. Advertisements

  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. Advertisements

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. Replies:
    2
    Views:
    660
  2. Andrew Feldman

    Type Reflection and Code Directory

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

    Medium Level Trust and Reflection

    Paul Hatcher, Feb 25, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    608
    Paul Hatcher
    Feb 25, 2005
  4. learningjava

    reflection and CLASSPATH

    learningjava, Oct 22, 2003, in forum: Java
    Replies:
    3
    Views:
    2,340
    Chris Uppal
    Oct 23, 2003
  5. Conrad Eaglehill

    AspectJ and Reflection

    Conrad Eaglehill, Jul 29, 2004, in forum: Java
    Replies:
    5
    Views:
    4,244
    Conrad Eaglehill
    Jul 30, 2004
  6. Stalin
    Replies:
    1
    Views:
    589
    Roedy Green
    Sep 29, 2005
  7. Replies:
    6
    Views:
    5,359
    Roedy Green
    Oct 17, 2005
  8. Brendan Guild
    Replies:
    6
    Views:
    855
Loading...