python中调用动态链接库(C++,DLL)

来源:互联网 发布:网络视听节目许可证 编辑:程序博客网 时间:2024/05/16 17:06


1,动态链接库编译

使用VS2015创建“Win32项目”,选择应用程序类型为“DLL”,创建项目完成后,头文件中:

#ifdefEXT_DEC_EXPORTS

#defineEXT_DEC_API__declspec(dllexport)

#else

#defineEXT_DEC_API__declspec(dllimport)

#endif

//此类是从 EXT_DEC.dll导出的

classEXT_DEC_APICEXT_DEC {

public:

   CEXT_DEC(void);

   // TODO: 在此添加您的方法。

};

//extern EXT_DEC_API int nEXT_DEC;

extern"C" {

 

   EXT_DEC_APIint fnEXT_DEC(void);

   EXT_DEC_APIint load_data(void);

   EXT_DEC_APIint FreeMem(void);

   //EXT_DEC_API int ExtIPLocation3(char* IP);

}

 

cpp文件中添加:

//这是导出函数的一个示例。

EXT_DEC_APIint fnEXT_DEC(void)

{

   return 42;

}

 

选择生成Release X64版本解决方案,便可以在项目文件夹下找到生成的DLLEXT_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")

原创粉丝点击