how to call unmanaged dll from managed c++ - c++

I am calling an unmanaged DLL from managed C++. The DLL has more than one native dependency. I am trying to compile this through Visual Studio.
I have done the following:
Added the directory that contains the DLLs and the .lib to Properties -> VC++ Directories -> Library directories
Added the unmanaged .lib to Properties -> Input -> Additional Dependencies
However, I get linking errors for the functions that I refer to. I haven't added any DLL or lib to the project except for the header file of the library.
UPDATE:
So it is able to find the .lib because if I give a bogus .lib in additional dependencies I get compilation error of the .lib not found. The error that I have is "unresolved token .... " .

I suggest you have a good read of this. Calling DLLs from C# is straightforward, because .NET supports it and managed C++ is just another .NET implementation, and should also be straightforward.

Related

Visual Studio 2019 - How to add a .lib to a c++ library project?

I created a c++ library with VS2019
my other projects see and use that library fine, they compile fine
but my library itself cannot be compiled as it complains about missing entry points for a depending lib
I want to add that .lib file, like I did for my other projects
but in the library project settings, there is no linker option, so I cannot tell it to use the missing .lib dependency
how do I resolve this ?
thanks for helping me on this
[edit] strangly enough SDL libraries dont cause any problem (becos of dll's ?) while nfd.lib has no dll
maybe the reason I cannot link my library with static libraries ?
image
but my library itself cannot be compiled as it complains about missing entry points for a depending lib
The library project can only be built but not compiled. After you finish building the .lib project, you will get a .lib file like this in the debug folder. And then you could add it to your C++ project.
According to your description, If you want to add a .lib to a .lib project? If so, I suggest you could try to add the name of the .lib file with its extension to Additional Dependencies. Librarian -> General -> Additional Dependencies. Here is the equivalent of linker input. And then add the path to the .lib file to the Additional Library Directories. Librarian -> General -> Additional Library Directories.Here is the equivalent of linker General.

Linking a .lib library to a project in Visual Studio

Somehow, even after going through a lot of materials, I could not understand one thing. I have a c++ library project in Visual Studio 2013. When I build it, it creates *.lib file. Now, there is another project where I need to link the .lib file. I created a folder called lib, dumped the .lib file in there and set the path of the lib folder to Properties->Linker->General->Additional Library Directories. Next I added the library file name *.lib in Properties->Linker->Input->Additional Dependencies. Now, building the project throws huge list of linker errors with message unresolved external symbol .... What is that I need to do more for linking the library?
I'm not sure what's the language your "another project" used, anyway, you should specifically set your *.lib file in Linker->Input->Additional Dependencies if you didn't explicitly load it in code.
If the both projects reside in the same solution the simplest option is to right-click of the project, go to to Properties, there go to Common Properties and in Framework and References add a reference to that library project. This will set the linker to link the .lib and adds a build dependency so the lib is always compiled before the executable.

C++ Using native dlls in the C++ project

I thought this should be straightforward but I am running into a bunch of linker errors like so:
Error 1 error LNK1104: cannot open file '...\Debug\Utils.lib' ...\LINK
where Utils is one of the C++ projects I want to keep as DLL.
If I change Configuration Properties->Configuration Type to Static Library(.lib) everything compiles and runs fine, but if I use .dll then its not working.
The whole solution is native C++ with the main project being a win32 console application.
Perhaps your library (Utils.lib) is not assembled as DLL and changes in console application project (that uses library) will not help.
Read carefully MSDN to see features of DLL's creation and usage.
It you try to link a .lib against another .lib, it doesn't really link. Instead, this instructs the final link to use both libraries.
For a DLL, this can't work, as the runtime linker cannot link the original .LIB. That means the link has to happen when the DLL is compiled.
As a result, a DLL project needs to have the .LIB directories set right.

Linking a lib statically in VC++

Probably I am missing here something but that is my first time on Windows that I need to link a lib statically so that the executable won't be dependent on a dll.I do it with LIBPNG.
I do it like this:
I added libpng headers : C/C++ -> Additional Include Directories
Added library directory to the linker: Linker - > General ->
Additional Library Directories
Added linker additional dependencies:Linker -> Input
Compile the exe ok.When calling it I am getting :
"The program can't start because libpng16.dll is missing from your
computer."
Which means libpng hasn't compiled into the executable.How do I fix that without reference the whole pnglib project code into my executable project?
On Windows a .lib is a library file, usually this simply contains code that loads a dll, looksup the exported functions and provides wrappers to them. But, you can build the lib differently so that instead of these wrapper stubs, it contains the actual binary code. The operation and structure of the .lib is the same - what code it contains depends on how its built.
So, if you've built libpng16 as a 'dynamic' lib/dll pair then you will need the dll part when you deploy it. If you built it as a 'static' lib only, then you'll get what you want.
The point is - you need to build the lib in the format you want in the first place. It is not possible to take a lib/dll pair and convert it into a static lib, nor is it possible to merge a dll into your executable. (well, not possible after you've built it - perfectly possibly if you change the lib's settings and recompile to produce in static lib form, of course)

Using .dll in Visual Studio 2010 C++

I have a problem. I place my .DLL and .LIB file in the same directory as my project, go to Properties -> Common Properties -> Framework and References -> Add New Reference. But the list comes up empty.
Is there something else I should be doing?
C++ is not C#. You don't include .dlls in C++ applications by adding "references". Unless it's C++/CLI, but that's not C++.
In C++, you would go, in the project configuration, to Linker->Input->Additional Dependencies. There, you would list the library name plus path to the .lib in question.
Normally, when you build a Windows C/C++ DLL, you also get a .lib. This is an import library; users of the library include (as stated above) that .lib in order to access the DLL. They generally do not load the .dll directly (though there are ways to do that).