My structure contains all unsigned char elements
typedef struct
{
unsigned char bE;
unsigned char cH;
unsigned char cL;
unsigned char EId1;
u
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))