python调用c++的dll的问题

来源:互联网 发布:监控小孩上网的软件 编辑:程序博客网 时间:2024/05/19 09:10

import ctypesdll=<span style="font-family: Arial, Helvetica, sans-serif;">ctypes.</span><span style="font-family: Arial, Helvetica, sans-serif;">WinDll(dllpath)<span style="white-space:pre"></span>#载入dll库</span>dll.afunction()<span style="white-space:pre"></span>#调用dll中的文件

1、调用dll库的问题

如果dll中的函数采用调用约定是__cdecll,那么可以采用如下方式

<pre name="code" class="python">dll=ctypes.CDLL(dllpath)或dll=ctypes.cdll.<span style="font-family: Arial, Helvetica, sans-serif;">LoadLibrary</span><span style="font-family: Arial, Helvetica, sans-serif;">(dllpath)</span>

<pre name="code" class="python"><span style="font-family: Arial, Helvetica, sans-serif;"><span></span>如果</span><span style="font-family: Arial, Helvetica, sans-serif;">dll的函数采用调用方式是__stdcall,那么要采用如下方式</span>

<pre name="code" class="python">dll=ctypes.WinDLL(dllpath)或dll=ctypes.windll.LoadLibrary(dllpath)


2、如果调用的函数参数用到结构体

c++中的头文件

int ORDERCALL Functiona(const STUA* pStua);

结构体定义为

typedef struct STUA{     char        m_sza[10]     int           m_szb}STUA;

那么调用函数时代码如下

class STUA(ctypes.Structrue):      _fields_=[        ('m_sza',ctypes.c_char*10)        ('m_szb',ctypes.c_int)]#设置参数pStua=STUA()pStua.m_sza='参数'pStua.m_szb=1#调用函数dll.afunction(ctypes.pointer(pStua))



0 0