1.dll文件:
1 #include <Windows.h> 2 3 _declspec(dllexport) void message_hello() 4 { 5 MessageBoxA(0, "hello", "hello", 0); 6 } 7 8 _declspec(dllexport) double add(double num1, double num2) 9 { 10 return num1 + num2; 11 }
2.调用dll文件
#include <stdlib.h> #include <Windows.h> void main() { HMODULE mod = LoadLibraryA("工具.dll"); if (mod == NULL) { printf("load error "); exit(0); } //获取函数地址 void(*message_hello)() = (void(*)())GetProcAddress(mod, "message_hello"); double(*add)(double, double) = (double(*)(double, double))GetProcAddress(mod, "add"); //调用函数 if (message_hello == NULL) { printf("find error "); } else { message_hello(); } if (add == NULL) { printf("find error "); } else { printf("%f",add(1,3)); } system("pause"); }
运行截图: