先新建DLL项目:
然后新建dll.h和dll.cpp。
dll.h:
#ifndef DLL_H #define DLL_H extern "C" int __declspec(dllexport)add(int a, int b); #endif
#include "dll.h" int add(int a, int b) { return a + b; }
接着新建控制台项目,来调用dll.dll,新建main.cpp:
#include <windows.h> #include <stdio.h> typedef int(*lpAddFun)(int, int); int main() { HINSTANCE hDll; lpAddFun add; hDll = LoadLibrary("..\\Release\\dll.dll"); if (hDll != NULL) { add = (lpAddFun)GetProcAddress(hDll, "add"); if (add != NULL) { int a = 2; int b = 3; printf("%d + %d = %d\n", a, b, add(a, b)); } FreeLibrary(hDll); } return 0; }