Using windll with complex data types

Ben C benc1 at today.com.au
Sat Nov 2 21:58:01 EST 2002


Hello once more,

thanks you for the previous example ... I am no getting some results
... but not quite what I expected ...

I have a function definition from a header file to a dll which takes a
pointer to a long such as below

API short int __stdcall GetVersion( VERSION *lpVersion );

So I did the following ... which gave results I was not expecting;

# Created A Handle To The dll
dll_handle = windll.module( package_name_dll )

# Allocate Space For The Version Number - 4 bytes for long
version_number = windll.cstring('', 4)

# Call Function Using Version Number Address
dll_handle.GetVersion( version_number.address() ) 

# Read Out Of Memory The Version Number 
version_number.read()

# This Returns
'\x03\x00\x02\x00'

# If I Feed This Into The Builtin Struct Function
struct.unpack( 'l', '\x03\x00\x02\x00' )

# I End Up With A Nonsense Number
(131075,)

Any ideas?

thanks (again)

Ben



Robin Becker <robin at jessikat.fsnet.co.uk> wrote in message news:<0qxMNUAyh+w9EwJw at jessikat.fsnet.co.uk>...
> In article <8beb91f5.0211020556.30107507 at posting.google.com>, Ben C <benc1 at today.com.au> writes
> ....
> I use the structob module to create structured things as for pointers
> they are the address of something.
> 
> so for example a lasterror function looks like
> 
> def _lastErrorMessage(n=None):
>         msg = windll.cstring('',512)
>         kernel32.FormatMessageA(
>                         FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
>                         0,
>                         n is None and GetLastError() or n,
>                         0,
>                         msg.address(),
>                         len(msg)-1,
>                         0
>                         )
>         return repr(msg)[1:-1]
> 
> we create the buffer and pass its address to the FormatMessage function.
> 
> If you really need pointer to pointer you need to create a buffer that holds the real address
> 
> eg
> class pointer(structob.struct_object):
>         oracle = structob.Oracle (
>                 'pointer',
>                 'Nl',
>                 ('ref',)
>                 )
> 
> then you can do
> 
> msg = windll.cstring('',512)
> p=pointer()
> p.ref = msg.address
> 
> so p points to msg and p.address() is a pointer to a pointer
> >
> >How does one pass reference to a pointer in a function?
> >
> >How does one retrieve data back from that pointer?
> >
> >How does one retrieve data back from aggregate data types such as
> >structures and arrays?
> >
> >Thanks in advance
> >
> >Ben



More information about the Python-list mailing list