Create Interop dll from type library (Tlbimp) without marshalling attributes RRS feed

  • Question

  • Hello,

    I have the idl of com component, I generate type library using midl and use type library to generate interop dll. I want to create interop dll without any marshalling attribute, for example BSTR should be returned as IntPtr, not as string after marshalling. I could not find any attirbute in TlbImp to control the marshalling, how can I achieve this requirement?

    Wednesday, December 1, 2010 2:33 AM


  • Hello vjdj4,


    1. One way to achieve this is to manually modify the interop assembly as follows :

    2. Use ildasm to transform the interop assembly into a text file, e.g. ildasm MyInteropAssembly.dll /out:MyInteropAssembly.il

    3. Then open MyInteropAssembly.il (a text file) and change the method which contains an [in] BSTR as follows :

    original :

      .method public hidebysig newslot abstract virtual
              instance void  Method01([in] string  marshal( bstr) strParam01) runtime managed internalcall

    new :

      .method public hidebysig newslot abstract virtual
              instance void  Method01([in] native int strParam01) runtime managed internalcall

    4. That is, we replace the 'string  marshal( bstr)' with 'native int'.

    5. You then re-create the interop assembly by using the ilasm.exe program, e.g. :

    ilasm /dll MyInteropAssembly.il

    That is, you re-create the interop assembly (e.g. MyInteropAssembly.dll) using the newly modified MyInteropAssembly.il.

    6. Then when you refer to the interop assembly, you will see that Method01() is declared as :

    public virtual void Method01(System.IntPtr strParam01);


    - Bio.


    • Marked as answer by Cookie Luo Monday, December 6, 2010 5:56 AM
    Wednesday, December 1, 2010 4:59 AM