1, 动态链接库编译
使用VS2015创建“Win32项目”,选择应用程序类型为“DLL”,创建项目完成后,头文件中:
#ifdef EXT_DEC_EXPORTS
#define EXT_DEC_API __declspec(dllexport)
#else
#defineEXT_DEC_API __declspec(dllimport)
#endif
// 此类是从 EXT_DEC.dll 导出的
class EXT_DEC_API CEXT_DEC {
public:
CEXT_DEC(void);
// TODO: 在此添加您的方法。
};
//extern EXT_DEC_API int nEXT_DEC;
extern "C" {
EXT_DEC_API int fnEXT_DEC(void);
EXT_DEC_API int load_data(void);
EXT_DEC_API int FreeMem(void);
//EXT_DEC_API int ExtIPLocation3(char* IP);
}
在cpp文件中添加:
// 这是导出函数的一个示例。
EXT_DEC_API int fnEXT_DEC(void)
{
return 42;
}
选择生成Release X64版本解决方案,便可以在项目文件夹下找到生成的DLL(EXT_DEC\x64\Release)
2,python中调用
#coding = utf-8
from ctypes import *
import os
CUR_PATH=os.path.dirname(__file__)
dllPath=os.path.join(CUR_PATH,"EXT_DEC.dll")
print dllPath
#mydll=ctypes.cdll.LoadLibrary(dllPath)
#print mydll
#pDll=ctypes.WinDLL(dllPath)
pDll=cdll.LoadLibrary(dllPath)
#pDll=ctypes.CDLL(dllPath)
print pDll
#加载数据
pResutl= pDll.load_data()
#手机解析
pDll.ExtMobileLocation.restype = c_char_p#设置返回的数据类型
print pDll.ExtMobileLocation("18797962122")