1 //example.cpp
2 #define EXPORT_EXAMPLE_DLL
3 #include "example.h"
4
5 EXAMPLE_API int max(int a, int b) {
6 return a > b ? a : b;
7 }
8
9 EXAMPLE_API int min(int a, int b) {
10 return a < b ? a : b;
11 }
关于__declspec(dllimport)的作用可以参考这篇博文:http://blog.csdn.net/mniwc/article/details/7993361
注意extern "c"是必须的,如果按照C++编译的话会有意想不到的问题发生,提示如下:
Traceback (most recent call last):
mx = dlllearning.max(a, b)
File "E:\Python34\lib\ctypes\__init__.py", line 364, in __getattr__
func = self.__getitem__(name)
File "E:\Python34\lib\ctypes\__init__.py", line 369, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'max' not found
[Finished in 0.2s]
编译生成dll文件,我们用python调用一下看看。
1 from ctypes import *
2 dlllearning = cdll.LoadLibrary('dlllearning.dll')
3 a = 413
4 b = 52
5 mx = dlllearning.max(a, b)
6 mn = dlllearning.min(a, b)
7 print(mx, mn)