【python】引用c的dll

1.引用方法

网上有两种加载方式,介绍很多,不再赘述

stdcall调用约定

ctypes.windll.LoadLibrary("dllpath") 

ctypes.WinDLL("dllpath")  

cdecl调用约定

ctypes.cdll.LoadLibrary("dllpath")

ctypes.CDLL("dllpath")

ctypes数据类型          C数据类型
c_char                          char
c_short                         short
c_int                             int
c_long                          long
c_ulong                        unsign long
c_float                          float
c_double                      double
c_void_p                       void
对应的指针类型是在后面加上"_p",如int*是c_int_p等等。
在python中要实现c语言中的结构,需要用到类。

2.错误

调用报如下错误
enter q to quit:dll
Traceback (most recent call last):
  File "G:00 Work10 车牌号query est.py", line 47, in <module>
    yzm()
  File "G:00 Work10 车牌号query est.py", line 13, in yzm
    lm_dll = ctypes.WinDLL("./lm.dll")
  File "C:Python27libctypes\__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
WindowsError: [Error 193] %1 不是有效的 Win32

发现python是安装的64位,而dll是32位的。安装32位Python问题解决

好记性不如烂笔头
原文地址:https://www.cnblogs.com/inns/p/5634076.html