1. In the VC new Win32 Console project, the project calls the dynamic link library My.dll in the function add, implementation of the addition, the code is as follows
#include <stdio.h> #include <windows.h>typedef int (*lpaddfun) (int, int);//macro defines function pointer type int main (int argc, Char *argv[]) { hinstance hdll;//dll handle lpaddfun addfun;//function pointer hdll = LoadLibrary ("My.dll");//Load Library if (hdll ! = NULL) { Addfun = (lpaddfun) GetProcAddress (hDLL, "add");//Get the Add function pointer if (addfun! = NULL) { int result = Addfun (2, 3); printf ("%d\n", result); } FreeLibrary (hDLL); } return 0;}
2. Create a new dynamic link library in VC My, which has an addition function add, the code is as follows
extern "C" __declspec (dllexport) int add (int, int);//export library function addint Add (int a, int b) {return a + B;}
This DLL can be called by putting the compiled DLL of the project into the same directory as the console project in 1.
Windows writes dynamic-link library DLLs