且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python天天美味(29) - 调用VC++的动态链接库(DLL)

更新时间:2022-09-06 13:46:43

1. 首先VC++的DLL的导出函数定义成标准C的导出函数:


Python天天美味(29) - 调用VC++的动态链接库(DLL)
#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);

//+ 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");
}
Python天天美味(29) - 调用VC++的动态链接库(DLL)


2. Python中调用如下:


Python天天美味(29) - 调用VC++的动态链接库(DLL)
from ctypes import *

fileName
="LRDllTest.dll"
func
=cdll.LoadLibrary(fileName)
str 
= create_string_buffer(20)
= func.Sum(23)
func.GetString(str)

print n
print str.raw
Python天天美味(29) - 调用VC++的动态链接库(DLL)


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

3. 输出结果:


5
Hello DLL


 

Python 天天美味系列(总)

Python 天天美味(27) - 网络编程起步(Socket发送消息)  

Python 天天美味(28) - urlopen    

Python 天天美味(29) - 调用VC++的动态链接库(DLL) 

Python 天天美味(30) - python数据结构与算法之快速排序 

Python 天天美味(31) - python数据结构与算法之插入排序 

...



本文转自CoderZh博客园博客,原文链接:http://www.cnblogs.com/coderzh/archive/2008/07/23/1249919.html,如需转载请自行联系原作者