当前位置: 移动技术网 > IT编程>脚本编程>Python > Python 调用VC++的动态链接库(DLL)

Python 调用VC++的动态链接库(DLL)

2019年05月25日  | 移动技术网IT编程  | 我要评论

微录客福利,nospot,愿者上钩姬泱

1. 首先vc++的dll的导出函数定义成标准c的导出函数:
复制代码 代码如下:

#ifdef lrdlltest_exports
#define lrdlltest_api __declspec(dllexport)
#else
#define lrdlltest_api __declspec(dllimport)
#endif

extern "c" lrdlltest_api int sum(int a , int b);
extern "c" lrdlltest_api void getstring(char* pchar);

//a + b
lrdlltest_api int sum(int a , int b)
{
return a + b;
}

//get a string
lrdlltest_api void getstring(char* pchar)
{
strcpy(pchar, "hello dll");
}


2. python中调用如下:
复制代码 代码如下:

from ctypes import *

filename="lrdlltest.dll"
func=cdll.loadlibrary(filename)
str = create_string_buffer(20)
n = func.sum(2, 3)
func.getstring(str)

print n
print str.raw

关于c语言中的一些参数类型详见:http://www.python.org/doc/2.5/lib/node454.html

3. 输出结果:
5
hello dll

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网