标签:style type har 函数地址 两种 extern dllimport col pre
第一种 导出方式
extern "C" _declspec(dllexport) int Plus(int x, int y); extern "C" _declspec(dllexport) int Sub(int x, int y); extern "C" _declspec(dllexport) int Mul(int x, int y); extern "C" _declspec(dllexport) int Div(int x, int y); int Plus(int x, int y) { return x + y; } int Sub(int x, int y) { return x - y; } int Mul(int x, int y) { return x * y; } int Div(int x, int y) { return x / y; }
第二种 导出方式
在项目上添加一个def文件
// def文件里面
EXPORTS Plus @12 Sub @17 Mul @15 NONAME // (此种方式只导出序号) Div @16
// CPP文件里面
int Plus(int x, int y) { return x + y; } int Sub(int x, int y) { return x - y; } int Mul(int x, int y) { return x * y; } int Div(int x, int y) { return x / y; }
DLL使用
第一种 隐式链接
// 先把TestDll.lib 和 TestDll.dll放在main.cpp 同一目录下
#include <stdio.h> #pragma comment(lib, "TestDll.lib") extern "C" _declspec(dllimport) int Plus(int x, int y); extern "C" _declspec(dllimport) int Sub(int x, int y); extern "C" _declspec(dllimport) int Mul(int x, int y); extern "C" _declspec(dllimport) int Div(int x, int y); int main() { int d = Plus(2, 3); printf("%d", d); getchar(); return 0; }
第二种 显示链接
#include <stdio.h> #include <windows.h> int main() { // 定义函数指针 typedef int(*lpPlus)(int, int); typedef int(*lpSub)(int, int); typedef int(*lpMul)(int, int); typedef int(*lpDiv)(int, int); // 获取模块句柄 HMODULE hMoudle = LoadLibrary(L"TestDll.dll"); // 获取函数地址 lpPlus MyPlus = (lpPlus)GetProcAddress(hMoudle, "Plus"); lpSub MySub = (lpSub)GetProcAddress(hMoudle, "Sub"); lpMul MyMul = (lpMul)GetProcAddress(hMoudle, "Mul"); lpDiv Mydiv = (lpDiv)GetProcAddress(hMoudle, "Div"); // 调用 int d = MyPlus(2, 3); printf("%d", d); getchar(); return 0; }
标签:style type har 函数地址 两种 extern dllimport col pre
原文地址:https://www.cnblogs.com/duxie/p/10859314.html