1.dll 文件:
- #include <Windows.h>
- _declspec(dllexport) void message_hello()
- {
- MessageBoxA(0, "hello", "hello", 0);
- }
- _declspec(dllexport) double add(double num1, double num2)
- {
- return num1 + num2;
- }
2. 调用 dll 文件
- #include <stdlib.h>
- #include <Windows.h>
- void main()
- {
- HMODULE mod = LoadLibraryA("工具.dll");
- if (mod == NULL)
- {
- printf("load error\n");
- 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\n");
- }
- else
- {
- message_hello();
- }
- if (add == NULL)
- {
- printf("find error\n");
- }
- else
- {
- printf("%f",add(1,3));
- }
- system("pause");
- }
运行截图:
来源: http://www.bubuko.com/infodetail-2449522.html