views:

493

answers:

2

In a visual studio C# project, it is possible to add references to COM libraries. Visual Studio will then use tlbimp.exe to generate the interop assembly when building the project. The reference looks like this in the .csproj file:

  <ItemGroup>
    <COMReference Include="TDAPIOLELib">
      <Guid>{F645BD06-E1B4-4E6A-82FB-E97D027FD456}</Guid>
      <VersionMajor>1</VersionMajor>
      <VersionMinor>0</VersionMinor>
      <Lcid>0</Lcid>
      <WrapperTool>tlbimp</WrapperTool>
      <Isolated>False</Isolated>
    </COMReference>
  </ItemGroup>

However, the 3rdparty type library which I am importing here causes tlbimp to emit some warnings. How do I suppress these warnings in visual studio? I tried to change the wrapper tool to

  <WrapperTool>tlbimp /silent</WrapperTool>

but that causes visual studio to complain with

An error has been encountered that prevents reference 'TDAPIOLELib' from loading. The wrapper tool 'tlbimp /silent' is not a valid wrapper tool.

+1  A: 

I ended up using the BeforeBuild target to explicitly invoke tlbimp.exe:

  <Target Name="BeforeBuild">
     <Exec Command="tlbimp /silent ..\3rdparty\comlibrary.dll /out:..\bin\interop.comlibrary.dll" />
  </Target>

This does require referencing the interop.comlibrary.dll binary, resulting in a little yellow warning sign on the reference in visual studio when opening the project before the first build.

Wim Coenen
+1  A: 

unless the COM library is constantly changing it's COM interfaces you could run the typelib importer once and then refer to the interop assembly in your project from then on. If you have reg free COM all that's needed is that the COM library be copied into your build output folder.

Joe Caffeine
I considered this, but I prefer to generate the interop assembly during the build. Upgrading the COM library is simpler that way; no need to document that the interop assembly also needs to be recreated.
Wim Coenen