要用__declspec(dllexport)声明导出函数
1 #include <stdio.h> 2 __declspec(dllexport) int sum(int a,int b) 3 { 4 printf("Welcome to use DLL !\n"); 5 printf("input : %d %d\n",a,b); 6 return a + b; 7 }
GCC编译时要加上-shared参数
MSVC编译时可使用
cl /c SourceFileName
link /dll ObjectFileName
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <windows.h> 4 int main(int argc,char *argv[]) 5 { 6 int i; 7 for(i=0;i<argc;i++) 8 printf("argv[%d] : %s\n",i,argv[i]); 9 10 HINSTANCE hdll = LoadLibrary("dll.dll"); 11 int (*func)(int,int)= GetProcAddress(hdll,"sum"); 12 int ret; 13 ret=func(((int)argv[1][0])-48,((int)argv[2][0])-48); 14 printf("Return : %d",ret); 15 return 0; 16 }
1 from ctypes import cdll 2 import os 3 path = os.getcwd() + '/dll.dll' 4 fun = cdll.LoadLibrary(path) 5 a=int(input("Please input the first integrate : ")) 6 b=int(input("Please input the second integrate : ")) 7 print("output : ",fun.sum(a,b)) 8 input()