importing a lib file using visual studio

40,374

Solution 1

You need to add the .lib to the project properties. Do you have a .lib to go along with the DLL? Another question dealing with a similar issue can be found here: How do I use a third-party DLL file in Visual Studio C++?

Main thing to note is whether or not you have a .lib to go along with your DLL. Another thing that I have done before but I wouldn't completely recommend is to copy the DLL into the same directory as your executable. Whenever loading DLLs, Windows automatically checks the path that the executable is in.

1) On the menu bar (top of window) click project->properties (or alt+f7).

Project Properties

2) Open configuration properties->linker. Then in the general tab, add the directory to your "Additional Library Directories".

Include Dirs

3) Then click on input right below general and add the name of the lib to the "Additional Dependencies"

Additional Libs

Solution 2

It is not a "library file", it is a COM server. The #import directive auto-generates a .tli and a .tlh file from the type library that's embedded in francais.dll. There's no obvious reason why it wouldn't be able to load that .tlh file, there's probably something wrong with that DLL. Hard to see from here. Do make sure that you are not ignoring earlier errors, start at the top of the Error List window.

As a basic check, you can look at that type library yourself. Run OleView.exe from the Visual Studio Command Prompt and use File + View Typelib, select that DLL. You need to see the content of the type library, decompiled into IDL.

You can also see it in VS itself, use File + Open + File and select the DLL. You'll see the resources that are embedded in the DLL, there needs to be a node labeled "TYPELIB" with one resource with ID 1 that's the actual type library. If anything goes wrong with these two verifications then the #import directive isn't likely to work either.

And do note that it is odd that it tries to find the file in the Release directory. You'd normally always start with the Debug configuration.

Solution 3

if you insist on coupling your Dll from code you can use (not all compiler support that, VS does) :

#pragma comment(lib, "path_to_lib\\libname.lib")

use #import for COM libraries not for standard Dlls

Solution 4

I had similiar trouble with COM server MSO.DLL (MS Office 14 / 2010) in Visual Studio 2015.

The (red) IntelliSense warning goes away after first building/compiling! My real problem was the exact path. I had to write it manually. Copy/paste vom Explorer didn't work.

#import "C:\Program Files\Common Files\microsoft shared\OFFICE14\MSO.DLL" no_implementation rename("RGB", "ExclRGB") rename("DocumentProperties", "ExclDocumentProperties") rename("SearchPath", "ExclSearchPath")
Share:
40,374
MelMed
Author by

MelMed

Updated on October 11, 2020

Comments

  • MelMed
    MelMed over 3 years

    I am trying to import a file as below:

    #import "francais.dll"
    

    It says to me that it cannot open source file "C:/xxxx/Proj/Release/francais.tlh"

    The Library file is existing in Proj.

    How to solve that?