[python-win32] deviceiocontrol string format

John Machin sjmachin at lexicon.net
Fri May 26 02:07:37 CEST 2006

On 26/05/2006 4:43 AM, B.P.S.Thurin at city.ac.uk wrote:
> Dear All,
> I am new to python win32, and i cannot figure out how to format the data 
> string for deviceiocontrol code with win32file.
> I have the following c struct in my driver file:
> {
> 	UCHAR ucMemorySpace;		// 0: I/O Space, 1: Mem Space.
> 	ULONG ulPhysicalAddress;	// Dword-aligned offset to write to.
> 	ULONG ulItemSize;			// 1: Byte, 2: Word, 4: Dword.
> 	ULONG ulItemCount;			// Number of bytes, words or dwords.
> 	union{
> 		UCHAR vucDataBuf[1];
> 		USHORT vusDataBuf[1];
> 		ULONG vulDataBuf[1];
> 	}data;
> I tried to format my data string like this:
> data=struct.pack('BLLLH',2,1020,2,1,0)
> but got the following error:
> OverflowError: long int too large to convert to int

It would help if you showed the code that you actually executed and the 

There is no problem with the above pack call:

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on 
Type "help", "copyright", "credits" or "license" for more information.
 >>> import struct
 >>> data=struct.pack('BLLLH',2,1020,2,1,0)
 >>> data
 >>> hex(1020)

Perhaps you meant that the error is coming from deviceiocontrol.

You will notice that the first item is padded out with 3 bytes of \x00 
so that the 2nd item is aligned on a 32-bit boundary. This may be the 
cause of your problem. The error that you got is consistent with 
something trying to convert (positive) 0xFC000000L to a *signed* 32-bit 
integer. Why signed, I don't know.

What does &(mystruct.ulPhysicalAddress) - &(mystruct.ucMemorySpace)
[warning: my C is a little rusty!]
give you in C (using the compiler used for the driver)?

If the answer is 1, you need to lose the padding; read the fine print at 
the end of the struct.pack manual section. Note that the default 
alignment used is according to the C compiler used to compile Python 
(Microsoft), which may differ from the alignment used by the compiler 
used for the driver (gcc?).

If the answer is 4, then the problem is not with your usage of struct.pack.

> Apparentely the error is coming from 1020. However it is define as ulong 
> for both case the c struct and the python struct.pack.


More information about the Python-win32 mailing list