65,186
社区成员




#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#ifdef DLL_TRAINING_API
#else
#define DLL_TRAINING_API _declspec(dllimport)
#endif
class DLL_TRAINING_API arithmetic_operation
{
public:
static arithmetic_operation* createApi();
double Add(double a,double b);
double Sub(double a,double b);
double Multi(double a,double b);
double Div(double a,double b);
};
int DLL_TRAINING_API export333();
#ifdef __cplusplus
}
#endif
#include <iostream>
#include "selfTrainingDll.h"
#include <windows.h>
using namespace std;
int main()
{
// 定义函数地址
typedef int(*_print)();
// 定义static成员的函数指针
typedef arithmetic_operation* (*_createApi)() ;
// 加载DLL
HINSTANCE hDll = LoadLibrary(L"DllTest.dll");
// 成功了
_print export333 = (_print)GetProcAddress(hDll, "export333");
cout << export333() << endl;
// 返回0 --- 失败了
_createApi createObj = (_createApi)GetProcAddress(hDll,"createApi");
// 无法调用 -- 失败了
arithmetic_operation* objPtr = createObj();
// 释放DLL
FreeLibrary(hDll);
}