1. 先新建一个名为 hello.cpp 的 C++ 源文件:
#include
#define DLLEXPORT extern "C" __declspec(dllexport)
DLLEXPORT int __stdcall hello()
{
printf("Hello world!\n");
return 0;
}
2. 编译成 dll 文件:
cl /LD hello.cpp
注意, 这里的参数是 /LD, 而不是 /DL。
3. 编写一个名为 hello.py 的 python 文件:
# coding: utf-8
import os
import ctypes
CUR_PATH = os.path.dirname(__file__)
if __name__ == '__main__':
print 'starting...'
dll = ctypes.WinDLL(os.path.join(CUR_PATH, 'hello.dll'))
dll.hello()
4. 输出为:
starting...
Hello world!
需要注意的地方:
1. C++ 的 dll 中的接口函数必须以 extern "C" __declspec(dllexport) 为前缀, C 的以 __declspec(dllexport) 为前缀。
否则会报错:
<