从动态加载的dll内部调用C ++函数

我正在编写一个C ++程序,该程序在运行时动态加载dll并在该dll中调用一个函数。 那没问题,但是现在我想从dll内调用C ++程序中定义的函数。

我的main.cpp看起来像这样:

#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(){
}

被访问的dll部分用C编写,如下所示:

#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.

评论