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

时间:2023-06-19 09:01:44 

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

标签:Python,VC++,动态链接库,DLL
0
投稿

猜你喜欢

  • asp如何动态生成WBMP?

    2009-11-18 20:36:00
  • 菜鸟大讲堂:如何查看mysql版本的四种方法

    2009-09-05 09:54:00
  • MySQL中连接查询和子查询的问题

    2024-01-19 04:27:32
  • Python version 2.7 required, which was not found in the registry

    2021-06-02 14:57:13
  • PHP中soap的用法实例

    2023-11-14 09:40:24
  • Golang自旋锁的相关介绍

    2024-05-09 14:58:22
  • Python pandas的八个生命周期总结

    2023-02-08 17:01:04
  • 对numpy中array和asarray的区别详解

    2022-06-26 04:40:12
  • CSS技巧之圆角背景与三角形

    2010-10-19 12:40:00
  • 对内联文字的疑惑

    2008-04-18 12:19:00
  • win2008下mysql8.0.11升级mysql8.0.17版本详细步骤

    2024-01-13 18:04:32
  • Python实现代码统计工具(终极篇)

    2021-10-21 03:01:47
  • Python实现Canny及Hough算法代码实例解析

    2022-10-15 14:23:37
  • 如何通过python画loss曲线的方法

    2021-01-04 09:01:18
  • Python使用Asyncio实现检查网站状态

    2023-01-23 18:56:39
  • javascript 网站常用的iframe分割

    2023-08-19 09:27:58
  • js判断文件是否为utf-8编码的方法

    2024-04-22 13:06:51
  • Python 可变类型和不可变类型及引用过程解析

    2021-07-13 00:04:27
  • PHP中SESSION使用中的一点经验总结

    2023-11-19 11:48:54
  • OpenCV-Python实现通用形态学函数

    2022-02-13 18:10:33
  • asp之家 网络编程 m.aspxhome.com