none
VS2017 fails to add a reference to C# project. RRS feed

  • Question

  • Hello,

    I am trying to add a reference to a C# project. The reference is imported from a TLB file. I do "Add reference/Browse" and specify the required TLB file. After I hit "OK", VS2017 shows me the following message: "A reference to <file name> could not be added. Make sure that the file is accessible, and that it is a valid assembly or COM component." When I open the same file with OleView, I can see a valid IDL generated from this type library. What am I missing here?

    The TLB in question can be found at the link below:

    https://1drv.ms/u/s!AkX9Ou21NcEig9FcEVdnTnjXKbVcOg

    Friday, February 22, 2019 12:08 AM

All replies

  • A TLB is a COM solution, which helps to access a COM DLL. it not an assembly,  it's not a .NET solution and it is not a .NET project.
    Friday, February 22, 2019 12:33 AM
  • If a type library has been registered then you can add a reference by selecting it from the COM type libraries -

    If the type library has not been registered you can use TLBIMP.exe to create an assembly from it.  After you have created an assembly, you can browse to it and add a reference -

    This is what the object browser shows for the assembly created from the type library -


    Friday, February 22, 2019 1:57 AM
  • I can import other TLBs into C# projects. Did it many times before. The problem is with this specific TLB. I do not see  why it is different and VS2015 error message is not very informative.

    Friday, February 22, 2019 3:57 AM
  • Maybe I am asking a wrong question. This is what is happening: I have two type libraries: Say A.tlb and B.tlb. When I use Reference/Add/Browse and point to A.TLB - everything works as expected. When I try to add reference to B.TLB - I get error message. The question is: What may cause such error message while importing a TLB file?

    Friday, February 22, 2019 4:46 PM
  • (Emphasis added)

    From https://stackoverflow.com/questions/44019317/visual-studio-cant-reference-tlb-dll-anymore

    There are three distinct ways to use a type library in a C# program:

    • Project > Add Reference > COM tab > tick the entry in the list. This is the best way and provides the best guarantee that your program can properly use the component and can keep up with changes in the component. Only real hangup is that it isn't always terribly obvious what entry to pick if you only know the type library file name. If necessary you can decompile the type library and find the description back, run OleView.exe > File > View Typelib.

    • Project > Add Reference > Browse button > select the .tlb file or the executable file that has the type library embedded. What you are trying to do right now. Only necessary if the component is not registered on the machine or you intentionally want to build your program to target an older version of the component. This is a brittle way, easy to get a mismatch between the type library you use and the actual installed component. And unfortunately the error reporting is lousy, if the type library conversion is not flawless then it produces a "it did not work" error message. The one you are looking at.

    • From the Developer Command Prompt run Tlbimp.exe. This generates the interop library up front, as opposed to the previous two bullets that do it at build time. This is the best way to diagnose problems with the type library, the kind that make the 2nd bullet fail. Or might be useful on a build server. You'll see warnings if the type library contains entries that cannot be converted directly. Which is pretty common and not necessarily fatal. If you do it this way then you'd typically want the interop library in the project directory and checked-in to source control. Use the Browse button to add the reference. Do keep in mind that you will have to re-do this when the vendor component changes.


    Friday, February 22, 2019 6:12 PM
  • Thank you for your detailed response. I was trying to avoid using tlbimp, as it requires creation of additional assembly file for import. For that I would have to change the build script for the appropriate component so that it generates TlbImp output for the component in question along with the DCOM server and its type library. I had no problem importing TLB files directly for other components. The interesting thing is that when I use Tlbimp, it works and creates the reference assembly without errors. I guess TLB import code in VS is not as good as in TlbImp.exe. And yes, I agree: The error reporting could be better.

    Friday, February 22, 2019 7:09 PM