Re: [ctypes-users] Well (luckily?) placed print fixes access violation
Brought to you by:
theller
|
From: Thomas H. <th...@ct...> - 2008-10-09 20:44:48
|
[CC to ctypes-users, which I forgot on the first reply]
Matt Herbert (matherbe) schrieb:
> Thomas,
>
> Thanks a bunch for the help. I have a simple question for you
> (hopefully). It's not clear to me what I should be setting the argtypes
> to when my function declarations takes a PBTYE, and I am using the
> create_unicode_buffer() function to create the argument:
>
> # C CODE
> WINSETUPAPI BOOL WINAPI
> SetupDiGetDeviceRegistryProperty(
> IN HDEVINFO DeviceInfoSet,
> IN PSP_DEVINFO_DATA DeviceInfoData,
> IN DWORD Property,
> OUT PDWORD PropertyRegDataType, OPTIONAL
> OUT PBYTE PropertyBuffer,
> IN DWORD PropertyBufferSize,
> OUT PDWORD RequiredSize OPTIONAL
> );
>
> # PYTHONE CODE
> _setupapi = ctypes.windll.setupapi
> _setupapi.SetupDiGetDeviceRegistryPropertyW.argtypes = (\
> ctypes.wintypes.HANDLE,\
> _PSP_DEVINFO_DATA,\
> ctypes.DWORD,\
> ctypes.POINTER(ctypes.DWORD),\
> ctypes.POINTER(ctypes.wintypes.BYTE),\
> DWORD, ctypes.POINTER(ctypes.DWORD)\
> )
>
> buffer = ctypes.create_unicode_buffer(1024)
> _setupapi.SetupDiGetDeviceRegistryPropertyW(\
> hdevinfoset, pDeviceInfoData, property,
> None, buffer, ctypes.sizeof(buffer), None)
>
> ctypes.ArgumentError: argument 5: <type 'exceptions.TypeError'>:
> expected LP_c_byte instance instead of LP_c_wchar_Array_1024
>
> Should I cast the buffer to a pointer to a byte, or should I change the
> argtypes to something else (??) for this function call?
Well, there are two ways. The first one would be to declare the argtypes as you have
done, but then you must pass an array of BYTE, probably like this:
buffer = (ctypes.wintypes.BYTE * 1024)()
_setupapi.SetupDiGetDeviceRegistryPropertyW(\
hdevinfoset, pDeviceInfoData, property,
None, buffer, ctypes.sizeof(buffer), None)
The other way would be to use c_void_p as argtypes item 5, then you
can pass your unicode_buffer.
I guess it depends on what you would do with the data in the buffer.
>
>> -----Original Message-----
>> From: Thomas Heller [mailto:th...@ct...]
>> Sent: Thursday, October 09, 2008 3:43 PM
>> To: Matt Herbert (matherbe)
>> Subject: Re: [ctypes-users] Well (luckily?) placed print
>> fixes access violation
>>
>> Matt Herbert (matherbe) schrieb:
>> > Hey all,
>> >
>> > I'm using python 2.6 on a Vista 64 system. I've been
>> getting lots of
>> > exceptions like this:
>> >
>> > WindowsError: exception: access violation writing 0x000007FE003F800C
>> >
>> > Sometimes the exceptions are "automatically" ignored, other
>> times the
>> > exceptions are fatal. I tried and tried to figure out what
>> was causing
>> > theses access violations without luck. The same code works
>> perfectly
>> > on a 32 bit system.
>>
>> Well, 64-bit systems are much less forgiving than 32-bit
>> systems. The reason is that ctypes when calling functions
>> for example assumes a return type of int.
>> If the function returns a pointer instead, the value will be
>> ok on 32-bit systems but truncated on 64-bit systems. So,
>> you should set proper restype and argtypes attributes for all
>> the functions that you call.
>>
>> > During debugging of the problem, I discovered that for some really
>> > strange reason, when I put a print statement into one of my
>> > constructors, the access violations mysteriously disappear and
>> > everything works perfectly.
>> >
>> > I'm wondering if there are any known gotchas about this
>> kind of stuff.
>> > I'm also curious as to how a well placed print statement could
>> > possibly fix an access violation?
>>
>> Thomas
>>
--
Thanks,
Thomas
|