python ctype初始化结构 [英] python ctype initialising a structure
问题描述
我的结构包含所有未签名的char元素
typedef struct
{
unsigned char bE;
未签名的字符cH;
无符号字符cL;
未签名的char EId1;
unsigned char EId0;
个未签名的字符SId1;
无符号字符SId0;
未签名的字符DLC;
无符号字符D0;
无符号字符D1;
无符号字符D2;
无符号字符D3;
无符号字符D4;
无符号字符D5;
无符号字符D6;
无符号字符D7;
} CMsg;
以下函数调用结构
extern int WriteCMessage(HANDLE hDev,CMsg * pMsg);
我将此结构转换为python ctype
class CMsg(Structure):
_fields_ = [('bE',c_char),
('cH',c_char),
('cL ',c_char),
('EId1',c_char),
('EId0',c_char),
('SId1',c_char),
('SId0', c_char),
('DLC',c_char),
('D0',c_char),
('D1',c_char),
('D2',c_char) ,
('D3',c_char),
('D4',c_char),
('D5',c_char),
('D6',c_char),
('D7',c_char)]
pmsg = CMsg('\x00','\x00','\x00','\x00','\x00',' \x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\ x00','\x00')
然后我加载了dll文件
hllDll.WriteCANMessage(handle,pmsg)
但这会导致错误
错误:访问冲突为0x00000000
您通过值传递了 pmsg
,但是该函数需要一个指针。由于您已初始化为全零,因此该函数最终将取消引用 NULL
指针。然后,ctypes使用Windows SEH将访问冲突路由到Python异常。
您需要使用 byref(pmsg)
传递引用。另外,定义函数的 argtypes
以确保在64位系统上正确处理指针。
from ctypes import *
from ctypes.wintypes import *
class CMsg(Structure):
_fields_ = [
('bE', c_ubyte),
('cH',c_ubyte),
('cL',c_ubyte),
('EId1',c_ubyte),
('EId0',c_ubyte) ,
('SId1',c_ubyte),
('SId0',c_ubyte),
('DLC',c_ubyte),
('D0',c_ubyte),
('D1',c_ubyte),
('D2',c_ubyte),
('D3',c_ubyte),
('D4',c_ubyte),
('D5',c_ubyte),
('D6',c_ubyte),
('D7',c_ubyte)]
hllDll = cdll ...
hllDll.WriteCANMessage.argtypes = [HANDLE,POINTER(CMsg)]
句柄= ...
pmsg = CMsg()#初始设置为{0}
hllDll。 WriteCANMessage(句柄,byref(pmsg))
My structure contains all unsigned char elements
typedef struct
{
unsigned char bE;
unsigned char cH;
unsigned char cL;
unsigned char EId1;
unsigned char EId0;
unsigned char SId1;
unsigned char SId0;
unsigned char DLC;
unsigned char D0;
unsigned char D1;
unsigned char D2;
unsigned char D3;
unsigned char D4;
unsigned char D5;
unsigned char D6;
unsigned char D7;
} CMsg;
The below function calls the structure
extern int WriteCMessage(HANDLE hDev,CMsg* pMsg);
I converted this structure to python ctype
class CMsg(Structure):
_fields_ = [('bE', c_char),
('cH', c_char),
('cL', c_char),
('EId1', c_char),
('EId0', c_char),
('SId1', c_char),
('SId0', c_char),
('DLC', c_char),
('D0', c_char),
('D1', c_char),
('D2', c_char),
('D3', c_char),
('D4', c_char),
('D5', c_char),
('D6', c_char),
('D7', c_char)]
pmsg = CMsg('\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00','\x00')
Then i loaded the dll file
hllDll.WriteCANMessage(handle, pmsg)
But this gives error
Error: Access violation at 0x00000000
You passed pmsg
by value, but the function expects a pointer. Since you've initialized to all zeros, the function ends up dereferencing a NULL
pointer. Then ctypes uses Windows SEH to route the access violation to a Python exception.
You need to use byref(pmsg)
to pass a reference. Also, define the function's argtypes
to ensure proper handling of the pointer on 64-bit systems.
from ctypes import *
from ctypes.wintypes import *
class CMsg(Structure):
_fields_ = [
('bE', c_ubyte),
('cH', c_ubyte),
('cL', c_ubyte),
('EId1', c_ubyte),
('EId0', c_ubyte),
('SId1', c_ubyte),
('SId0', c_ubyte),
('DLC', c_ubyte),
('D0', c_ubyte),
('D1', c_ubyte),
('D2', c_ubyte),
('D3', c_ubyte),
('D4', c_ubyte),
('D5', c_ubyte),
('D6', c_ubyte),
('D7', c_ubyte)]
hllDll = cdll...
hllDll.WriteCANMessage.argtypes = [HANDLE, POINTER(CMsg)]
handle = ...
pmsg = CMsg() # initially memset to {0}
hllDll.WriteCANMessage(handle, byref(pmsg))
这篇关于python ctype初始化结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!