Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a dll function from C++

Tags:

c++

dll

I have a function inside a dll that I would like to call from my C++ application. The dll is also made in C++ and has a def file which shows the functions present in the dll. I am using visual studio 2010 and I configured it to use the dll file by adding the directory of the dll in the linker "Additional Library directories" and then adding the DLLname.lib in the "input" of the linker. Now all the namespace inside the dll are available however the Functions that I need are not available because they are not under any namespace. How do I go about accessing those functions ? Those functions were declared as such in the dll

#include "stdafx.h"
#include <stdio.h>
__declspec(dllexport) int somefunction()
{
......
            return SomeValue
}

My question is how do I access somefunction in my C++ application through its dll.

like image 993
Casper_2211 Avatar asked Nov 30 '12 15:11

Casper_2211


People also ask

How do you call a DLL function?

Application can call a DLL function in two ways. Implicit Calling: When application or client uses its import table to call the external symbol/functions, it is called implicit call. In implicit call application use prototype header and import library to link an external symbol. #include "math.

How do you call a DLL function in C#?

In order to reference a dll, just add the compiler flag /r:PathToDll/NameOfTheDll to csc. In FileWhichIsReferencingTheDll. cs add using namespace AppropriateNameSpace; to access the functions (by calling class. functionName if static or by creating an object of the class and invoking the function on the object).


2 Answers

There seems to be some confusion here. Adding a file to the linker input is for statically-linked libraries (.lib on Windows). With statically-linked libraries, the code is just copied into your program at compile time. Dynamically-linked libraries (.dll in Windows) reside in different files (the DLLs) and are loaded by your program when you run it. To access a function in a dll, there's two main methods:

  • Use dllimport, similarly to how you exported the functions with dllexport

  • Load the DLL using LoadLibrary, then get a pointer to your function with GetProcAddress. If you're using this method, another thing you should note is that you should likely use extern "C" on functions you're exporting to avoid name mangling. If you're having issues finding the function with GetProcAddress, you can use Dependency Walker to examine the function names inside the DLL - they may be modified slightly depending on the calling convention used.

like image 98
Matt Kline Avatar answered Oct 06 '22 00:10

Matt Kline


I think the poster is asking for help on implicit linking. MSDN Linking Implicitly and Wikipedia Dynamic-link library Using DLL imports

like image 39
stefan Avatar answered Oct 06 '22 01:10

stefan