Calling dll function from C ++

I have a function inside a dll that I would like to call from my C ++ application. The DLL is also created in C ++ and has a def file that shows the functions present in the dll. I use visual studio 2010, and I configured it to use the dll file by adding the DLL to the "Additional Libraries" linker, and then adding the DLLname.lib to the "enter" linker. Now the entire namespace inside the dll is available, but the functions that I need are not available because they are not under any namespace. How do I access these features? These 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 some function in my C ++ application through its dll.

+4
source share
2 answers

There seems to be some kind of confusion here. Adding a file to linker input for statically linked libraries (.lib on Windows). In statically linked libraries, code is simply copied to your program at compile time. Dynamically linked libraries (DLLs in Windows) are located in different files (DLLs) and are loaded by your program when it starts. There are two main methods for accessing a function in a dll:

  • Use dllimport , similar to how you exported functions with dllexport

  • Download the DLL using LoadLibrary , then get a pointer to your function with GetProcAddress . If you use this method, one more thing you should note is that you must use extern "C" for the functions you export to avoid name mangling . If you are having trouble finding a function using GetProcAddress , you can use Dependency Walker to examine the names of functions inside the DLL - they may be slightly changed depending on the calling convention .

+11
source

I think the poster is asking for help via an implicit link. MSDN Communication Implicitly and Wikipedia Dynamic Link Library Using DLL Import

+1
source

All Articles