Search code examples
c#com-interoptlbimp

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 :-)


Solution

  • 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%
    )