Tuesday, April 5, 2011

Tool to import Type Libraries as C# code

Is there a tool to import/convert COM type libraries into C# code rather than generating an assembly? The TLBIMP tool and the TypeLibaryConverter class only generate assemblies.

I've had some success ripping the C# ComImport definitions by running Reflector over the generated Interop assembly and copying a pasting the disassembled source, but this usually requires quite a bit of manual patching up before it'll compile.

Desired goal is a single EXE without satellite Interop DLLs, so perhaps the answer is to use ILMerge to effectively embed the interop DLL in the EXE.

I was sure in the past I'd come across such a tool - but maybe it dreamt it :-)

From stackoverflow
  • This won't help you out today, but there is a feature coming in the next version of C#. It's called NoPia or Type Embedding depending on which presentation you read. This feature essentially will link a PIA assembly into whatever project you reference it from. The end resulrt is a single EXE which no need to deploy an interop/PIA DLL.

    Misha's post on the subject: http://blogs.msdn.com/mshneer/archive/2008/10/28/type-embedding-support-in-c.aspx

    Short term though, you may have to go with ILMerge or reflector + copy code.

    Duncan Smart : Yes I'd heard about that. Roll on CLR 4.0!
  • I'm not so sure it is going to be useful to you, but the source code for a managed version of Tlbimp.exe has been released on CodePlex. VS2010 will definitely solve your problem.

    Duncan Smart : Cool - I didn't know that! I'll take a look...
  • As I originally suspected the best solution is going with ILMerge. I can't be selective about parts of a COM API to embed, but it works well enough.

    Here is the Post Build Event Command Line I'm using, which should be easy enough to reuse:

    set MERGEFILES=Interop.Foo.dll Interop.Bar.dll
    
    if "$(ConfigurationName)" == "Release" (
       ren "$(TargetFileName)" "_$(TargetFileName)"
       "$(ProgramFiles)\Microsoft\ILMerge\ILMerge.exe" /out:"$(TargetFileName)" "_$(TargetFileName)" %MERGEFILES%
    
       del "_$(TargetFileName)"
       del %MERGEFILES%
    )
    

0 comments:

Post a Comment