Sub. c
#include<windows.h>#include"sub.h"int WINAPI DllMain(_In_ HANDLE _HDllHandle, _In_ DWORD _Reason, _In_opt_ LPVOID _Reserved) { return TRUE; } EXPORT int sub(int a,int b){ return a-b;}EXPORT int add(int a,int b){ return a+b;}
Sub. h
#ifdef __cplusplus #define EXPORT extern "C" __declspec (dllexport) #else #define EXPORT __declspec (dllexport) #endif EXPORT int sub(int a,int b);EXPORT int add(int a,int b);
Then generate the solution:
You can see a sub. Lib, sub. dll in the debug/release directory.
Then we
You can package the sub. Lib, sub. dll, and sub. H files.
How to use it?
Method 1: copy the three files to the current directory and use the displayed link:
#include<windows.h>#include<stdio.h> #include"sub.h"#pragma comment(lib,"subDll.lib") int main(){ int a=4,b=1; int c;// printf("%d+%d=%d\n",a,b,add(a,b)); c=sub(a,b); printf("%d-%d=%d\n",a,b,c); return 0;}
Method 2:
Step 1:
Step 2:
#include<windows.h>#include<stdio.h> #include"sub.h"//#pragma comment(lib,"subDll.lib") int main(){ int a=4,b=1; int c;// printf("%d+%d=%d\n",a,b,add(a,b)); c=sub(a,b); printf("%d-%d=%d\n",a,b,c); return 0;}
Compiled and run successfully!
Compile your own DLL and export the Function