I'm writing a C++ programm that dynamically loads a dll at runtime and calls a function within that dll. Thats working fine but now i want to call a function defined in my C++ programm from within the dll.
My main.cpp looks like this:
#include <Windows.h>
#include <iostream>
typedef void(*callC)(int);
int main()
{
HINSTANCE dllHandle = LoadLibrary("D:\Libraries\lib.dll");
callC func = (callC)GetProcAddress(dllHandle, "callC");
func(42);
FreeLibrary(dllHandle);
}
// I want to call this function from my dll
void callableFromDll(){
}
The part of the dll thats accessed is writtin in C and looks like as follows:
#include <stdio.h>
void callC(int);
void callC(int i){
print(i);
// Call the C++ function
//callableFromDll();
}
I've read about the __declspec(dllimport)
and __declspec(dllexport)
attributes but im really new to C++ and not sure if these are the right thing to use and if so, how to use them.
In your C++ program:
In your DLL:
Returns the parent's executable handle.
Console output from the exe when LoadLibrary has loaded the DLL:
extern "C"
tells the compiler to not encode the function's name into a unique name. The compiler encodes names so that linkers can separate common function or variable names.See extern "C" and extern "C++" function declarations, Exporting from a DLL Using __declspec(dllexport) and Importing function calls using __declspec(dllimport).