Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling functions in a DLL from C++

I have a solution in VS 2008 with 2 projects in it. One is a DLL written in C++ and the other is a simple C++ console application created from a blank project. I would like know how to call the functions in the DLL from the application.

Assume I am starting with a blank C++ project and that I want to call a function called int IsolatedFunction(int someParam)

How do I call it?

like image 659
QueueHammer Avatar asked Feb 11 '09 23:02

QueueHammer


People also ask

How do I get all functions in a DLL?

If a DLL is written in one of the . NET languages and if you only want to view what functions, there is a reference to this DLL in the project. Then doubleclick the DLL in the references folder and then you will see what functions it has in the OBJECT EXPLORER window.

How do I call C++ DLL from C++?

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.

Can a DLL call an EXE?

Thanks for the advice. The information's in these links are for the cases where the DLL is loaded by the EXE and hence the DLL can call the exported functions of the EXE.

Can a DLL have a main function?

It is just code and does not have an execution thread of its own. The thread execution starts in the program with main() (or WinMain). The program can choose to do all sorts of things, like loading a DLL and calling functions in it, or spawning other threads, or starting other programs and so on.


2 Answers

There are many ways to do this but I think one of the easiest options is to link the application to the DLL at link time and then use a definition file to define the symbols to be exported from the DLL.

CAVEAT: The definition file approach works bests for undecorated symbol names. If you want to export decorated symbols then it is probably better to NOT USE the definition file approach.

Here is an simple example on how this is done.

Step 1: Define the function in the export.h file.

int WINAPI IsolatedFunction(const char *title, const char *test); 

Step 2: Define the function in the export.cpp file.

#include <windows.h>  int WINAPI IsolatedFunction(const char *title, const char *test) {     MessageBox(0, title, test, MB_OK);     return 1; } 

Step 3: Define the function as an export in the export.def defintion file.

EXPORTS    IsolatedFunction          @1 

Step 4: Create a DLL project and add the export.cpp and export.def files to this project. Building this project will create an export.dll and an export.lib file.

The following two steps link to the DLL at link time. If you don't want to define the entry points at link time, ignore the next two steps and use the LoadLibrary and GetProcAddress to load the function entry point at runtime.

Step 5: Create a Test application project to use the dll by adding the export.lib file to the project. Copy the export.dll file to ths same location as the Test console executable.

Step 6: Call the IsolatedFunction function from within the Test application as shown below.

#include "stdafx.h"  // get the function prototype of the imported function #include "../export/export.h"  int APIENTRY WinMain(HINSTANCE hInstance,                      HINSTANCE hPrevInstance,                      LPSTR     lpCmdLine,                      int       nCmdShow) {     // call the imported function found in the dll     int result = IsolatedFunction("hello", "world");      return 0; } 
like image 93
jussij Avatar answered Oct 05 '22 22:10

jussij


Can also export functions from dll and import from the exe, it is more tricky at first but in the end is much easier than calling LoadLibrary/GetProcAddress. See MSDN.

When creating the project with the VS wizard there's a check box in the dll that let you export functions.

Then, in the exe application you only have to #include a header from the dll with the proper definitions, and add the dll project as a dependency to the exe application.

Check this other question if you want to investigate this point further Exporting functions from a DLL with dllexport.

like image 21
4 revs, 3 users 67% Avatar answered Oct 05 '22 21:10

4 revs, 3 users 67%