我是一個初學者python和ctypes,我正在寫一個腳本來控制一個PCMCIA設備,供應商提供的驅動程序是一個dll文件。我只是想調用它的功能,但我只是不知道爲什麼我一直得到一個[「0x7c9108d3」的指令引用「0xfffffff8」的內存。內存不能被「讀取」]。下面是文件中稱:Python ctypes - 調用DLL中的函數時發生內存讀取錯誤
//declaration
XLstatus xlGetDriverConfig(XLdriverConfig *pDriverConfig)
typedef struct s_xl_driver_config {
unsigned int dllVersion;
unsigned int channelCount;
unsigned int reserved[10];
XLchannelConfig channel[XL_CONFIG_MAX_CHANNELS];
} XLdriverConfig;
//types
typedef struct s_xl_channel_config {
char name [XL_MAX_LENGTH + 1];
unsigned char hwType;
unsigned char hwIndex;
unsigned char hwChannel;
unsigned short transceiverType;
unsigned int transceiverState;
unsigned char channelIndex;
XLuint64 channelMask; //here
unsigned int channelCapabilities;
unsigned int channelBusCapabilities;
unsigned char isOnBus;
unsigned int connectedBusType;
XLbusParams busParams;
unsigned int driverVersion;
unsigned int interfaceVersion;
unsigned int raw_data[10];
unsigned int serialNumber;
unsigned int articleNumber;
char transceiverName [XL_MAX_LENGTH + 1];
unsigned int specialCabFlags;
unsigned int dominantTimeout;
unsigned int reserved[8];
} XLchannelConfig;
typedef unsigned __int64 XLuint64;
typedef struct {
unsigned int busType;
union {
struct {
unsigned int bitRate;
unsigned char sjw;
unsigned char tseg1;
unsigned char tseg2;
unsigned char sam; // 1 or 3
unsigned char outputMode;
} can;
struct {
unsigned int activeSpeedGrade;
unsigned int compatibleSpeedGrade;
} most;
unsigned char raw[32];
}data;
} XLbusParams;
下面有我的Python腳本:
from ctypes import *
vxlapi = WinDLL("vxlapi.dll")
PyxlGetDriverConfig = vxlapi.xlGetDriverConfig
class PyXLchannelConfig(Structure):
_fields_ = [("Pyname",c_char*32),
("PyhwType",c_ubyte),
("PyhwIndex",c_ubyte),
("PyhwChannel",c_ubyte),
("PytransceiverType",c_ushort),
("PytransceiverState",c_ushort),
("PyconfigError",c_ushort),
("PychannelIndex",c_ubyte),
("PychannelMask",c_longlong),
("PychannelCapabilities",c_uint),
("PychannelBusCapabilities",c_uint),
("PyisOnBus",c_ubyte),
("PyconnectedBusType",c_uint),
("PybusParams",c_uint),
("PydriverVersion",c_uint),
("PyinterfaceVersion",c_uint),
("Pyraw_data",c_uint*10),
("PyserialNumber",c_uint),
("PyarticleNumber",c_uint),
("PytransceiverName",c_char*32),
("PyspecialCabFlags",c_uint),
("PydominantTimeout",c_uint),
("PydominantRecessiveDelay",c_ubyte),
("PyrecessiveDominantDelay",c_ubyte),
("PyconnectionInfo",c_ubyte),
("PycurrentlyAvailableTimestamps",c_ubyte),
("PyminimalSupplyVoltage",c_ubyte),
("PymaximalSupplyVoltage",c_ubyte),
("PymaximalBaudrate",c_uint),
("PyfpgaCoreCapabilities",c_ubyte),
("PyspecialDeviceStatus",c_ubyte),
("PychannelBusActiveCapabilities",c_ushort),
("PybreakOffset",c_ushort),
("PydelimiterOffset",c_ushort),
("Pyreserved",c_uint*3)
]
class PyXLdriverConfig(Structure):
_fields_ = [("PydllVersion",c_uint),
("PychannelCount",c_uint),
("Pyreserved",c_uint*10),
("Pychannel",PyXLchannelConfig*64)
]
if __name__ == "__main__":
drivercfg = PyXLdriverConfig()
PyxlGetDriverConfig(byref(drivercfg))
你能不能幫我出這一點,非常感謝你!
謝謝,修改後,腳本正常運行 – edward 2012-03-27 07:50:58