none
Question on COM interop via reflection.emit RRS feed

  • Question

  • I have a series of managed applications that are out of process COM servers and need to be accessed via COM from both C++ and managed applications. In order to access them from managed code I am trying to generate interop assemblies. Unfortunately TLBImp is smart enough to recognize the assembly is a managed assembly and tells me it won't generated an interop assembly. This has forced me to write my own tool as I don't want to have to manually write and update the interop assemblies myself. The problem I am running into is generating the .net class for coclasses. Every time I finish generating the methods and call CreateType() I get a System.TypeLoadException: Method with non-zero RVA in an Import. One thing i've noticed is that the TLBImp generated methods have the 'extern' keyword.

     

    Using reflector and custom tools of my own I have inspected an interop assembly for an equivelent native COM application and the attributes, flags, etc... that i'm using to define all fo the types and methods are identical. Another interesting thing I have discovered is that Assembly.Load() does not work on TLBImp generated assemblies however Reflector seems to have no problem with them. Also, interop assemblies do not appear in AppDomain.Current.GetAssemblies(). All of this has me quite perplexed =).

    Tuesday, February 26, 2008 10:10 PM

Answers

  • Personally I'd define the COM types in IDL instead, compile to a typelib and run Tlbimp on that. That way you get around the TlbImp smartness.

     

    That said, I think your TypeLoadException can be solved by calling MethodBuilder.SetImplementationFlags to specify that the method doesn't have an IL method body.

     

    Thursday, February 28, 2008 9:22 AM
    Moderator

All replies

  • Could you please clarify how exactly did you create the "out of process COM servers"?

    And could you please try using Marshal.GetActiveObject() and late-bound invocations in the managed client?
    Thursday, February 28, 2008 8:10 AM
  • Personally I'd define the COM types in IDL instead, compile to a typelib and run Tlbimp on that. That way you get around the TlbImp smartness.

     

    That said, I think your TypeLoadException can be solved by calling MethodBuilder.SetImplementationFlags to specify that the method doesn't have an IL method body.

     

    Thursday, February 28, 2008 9:22 AM
    Moderator