473,287 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

ctypes, function pointers and a lot of trouble

Hi friends,

Okay so well, I have quite a problem right now with a file stream. What
I am doing is to use the Cannon SDK dlls to get control over my old
Cannon A60 Camera for some surveillance useage.

By using ctypes it all worked well until now. I am able to load the
dlls, use a lot of functions, am able to connect to the camera, read out
some params, send commands etc... but now I am stuck with reading the
picture data.

In C the code looks as follows (found in an supplemental *.h SDK file:

------------------------------CODE-----------------------------------------

#define cdSTDCALL __stdcall

typedef void cdSTDCALL cdSOpen(cdContext contextH, cdPermission,
cdError* err);
typedef void cdSTDCALL cdSClose(cdContext contextH, cdError* err);
typedef void cdSTDCALL cdSRead(cdContext contextH, void* buf,
cdUInt32* bufsize, cdError* err);
typedef void cdSTDCALL cdSWrite(cdContext contextH, const void *buf,
cdUInt32* bufsize, cdError *err);
typedef void cdSTDCALL cdSSeek(cdContext contextH, cdWhence, cdInt32
offset, cdError* err);
typedef cdInt32 cdSTDCALL cdSTell(cdContext contextH, cdError* err);
typedef void cdSTDCALL cdSProgress(cdContext contextH, cdUInt16
percentDone, cdError* err);

/* cdStream
*/
typedef struct {
cdContext contextH;
/* stream I/O function pointers */
cdSOpen* open;
cdSClose* close;
cdSRead* read;
cdSWrite* write;
cdSSeek* seek;
cdSTell* tell;
} cdStream;

/* cdStgMedium
*/
typedef struct {
cdMemType Type; /* Type of the medium (u). */
union {
cdChar* lpszFileName;
cdStream* pStream;
#ifdef macintosh
cdFSSpec* pFSSpec;
#endif
}u; /* Union of all transfer medium */
} cdStgMedium;

------------------------------\CODE----------------------------------------

and this is the function definition that should give me access to the
data stream (available via DLL):

------------------------------CODE-----------------------------------------

cdCAPI CDGetReleasedData(
cdHSource hSource,
cdProgressCallbackFunction * pCallbackFunc,
cdContext Context,
cdProgressOption ProgressOption,
cdReleaseImageInfo* pInfo,
cdStgMedium* pStgMedium
);

------------------------------\CODE----------------------------------------

So, since I'm no C-Professional, I can only guess what that code does.
With some previous commands I tell the camera to make a picture. This
picture is then automatically moved to the PCs RAM and with the function
above (CDGetReleasedData) I should be able to access this stream. Now I
havn't accessed any stream with ctypes yet so I have only a rough idea
how it could work.

The following are the relevant parts of my code that don't work:

------------------------------CODE-----------------------------------------

# Definitions:

class cdReleaseImageInfo(Structure):
_fields_ = [("SequenceID", c_uint),
("DataType", c_uint),
("Format", c_ubyte),
("DataSize", c_uint),
("Filename", c_char * 2)]
class cdStream(Structure):
_fields_ = [("contextH", c_uint),
("open", c_uint),
("close", c_uint),
("read", c_uint),
("write", c_uint),
("seek", c_uint),
("tell", c_uint)]

class memunion(Union):
_fields_ = [("lpszFileName", c_char),
("pStream", cdStream)]
class cdStgMedium(Structure):
_fields_ = [("Type", c_uint),
("u", memunion)]


# command:

datainfo = cdReleaseImageInfo()
data = cdStgMedium()
errorcode = cdsdk.CDGetReleasedData(devicehandle, byref(cbfunct),
c_uint(1), c_uint(1), byref(datainfo), byref(data))

------------------------------\CODE----------------------------------------
The function "cdsdk.CDGetReleasedData" itself gets executed correctly,
but returns an "Invalid Parameter" errorcode. there's also no useful
data whereas datainfo gets written correctly. I know that my cdStream
can't work, facing the C-code, but what'd be the right cdStream class?
What can I do? Any ideas?

Best regards and thanks,
Matt

Jun 27 '08 #1
9 7263
Matt <mr*****@gmx.atwrote:
Okay so well, I have quite a problem right now with a file stream. What
I am doing is to use the Cannon SDK dlls to get control over my old
Cannon A60 Camera for some surveillance useage.

By using ctypes it all worked well until now. I am able to load the
dlls, use a lot of functions, am able to connect to the camera, read out
some params, send commands etc... but now I am stuck with reading the
picture data.

In C the code looks as follows (found in an supplemental *.h SDK file:
[snip]
So, since I'm no C-Professional, I can only guess what that code does.
With some previous commands I tell the camera to make a picture. This
picture is then automatically moved to the PCs RAM and with the function
above (CDGetReleasedData) I should be able to access this stream. Now I
havn't accessed any stream with ctypes yet so I have only a rough idea
how it could work.

The following are the relevant parts of my code that don't work:
# Definitions:

class cdReleaseImageInfo(Structure):
_fields_ = [("SequenceID", c_uint),
("DataType", c_uint),
("Format", c_ubyte),
("DataSize", c_uint),
("Filename", c_char * 2)]
class cdStream(Structure):
_fields_ = [("contextH", c_uint),
("open", c_uint),
("close", c_uint),
("read", c_uint),
("write", c_uint),
("seek", c_uint),
("tell", c_uint)]
These c_uints would be better as c_void_p at minimum

Howerver I suspect you are going to have to implement these callback
functions - read this section of the manual

http://docs.python.org/lib/ctypes-ca...functions.html

I suspect it will call back your functions for the given
functionality, eg "open", "close" etc...
class memunion(Union):
_fields_ = [("lpszFileName", c_char),
This should be c_char_p I suspect...
("pStream", cdStream)]
and this should be POINTER(cdStream)
class cdStgMedium(Structure):
_fields_ = [("Type", c_uint),
("u", memunion)]


# command:

datainfo = cdReleaseImageInfo()
data = cdStgMedium()
errorcode = cdsdk.CDGetReleasedData(devicehandle, byref(cbfunct),
c_uint(1), c_uint(1), byref(datainfo), byref(data))

The function "cdsdk.CDGetReleasedData" itself gets executed correctly,
but returns an "Invalid Parameter" errorcode. there's also no useful
data whereas datainfo gets written correctly. I know that my cdStream
can't work, facing the C-code, but what'd be the right cdStream class?
What can I do? Any ideas?
I've noted some obvious problems above.

To get this to work will require some C knowledge. If they supply
some example C code I'd work through that translating it line by line
to python+ctypes.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #2
Okay, thanks a lot for your reply Nick, I think you pushed me back on
the right way.

Now I started with trying to implement the callback functions and am
stuck at the following point:

I define my classes/structures/unions:

------------------------------CODE-----------------------------------------

class cdStream(Structure):
_fields_ = [("contextH", c_uint),
("open", c_void_p),
("close", c_void_p),
("read", c_void_p),
("write", c_void_p),
("seek", c_void_p),
("tell", c_void_p)]

class memunion(Union):
_fields_ = [("lpszFileName", c_char_p),
("pStream", cdStream)]
class cdStgMedium(Structure):
_fields_ = [("Type", c_uint),
("u", memunion)]

------------------------------\CODE----------------------------------------

then i define my functions (right now I do just nothing) and at the same
time I define the datatypes like they're listed in my C sample program:
------------------------------CODE-----------------------------------------

def pystreamopen (contextH, mode, pErr):
pass

cstreamopen = CFUNCTYPE(c_uint, c_ushort, c_uint)

def pystreamclose (contextH, pErr):
pass

cstreamclose = CFUNCTYPE(c_uint, c_uint)

def pystreamread (contextH, pBuf, pBufsize, pErr):
pass

cstreamread = CFUNCTYPE(c_uint, c_void_p, c_uint, c_uint)

def pystreamtell (contextH, pErr):
pass

cstreamtell = CFUNCTYPE(c_uint, c_uint)

def pystreamwrite (contextH, origin, offset, pErr):
print "writing..."
pass

cstreamwrite = CFUNCTYPE(c_uint, c_void_p, c_uint, c_uint)

------------------------------\CODE----------------------------------------

and now the problem starts: i want the pointers in the cdStream
Structure point at my functions and tried to do it the following way:
------------------------------CODE-----------------------------------------

data = cdStgMedium()
data.type = 0
data.u.pStream.contextH = c_uint(3) #must be some kind of identifier.
data.u.pStream.open = cstreamopen(pystreamopen)
data.u.pStream.close = cstreamclose(pystreamclose)
data.u.pStream.write = cstreamwrite(pystreamwrite)
data.u.pStream.tell = cstreamtell(pystreamtell)
data.u.pStream.read = cstreamread(pystreamread)

------------------------------\CODE----------------------------------------

unfortunately that doesn't work because Python returns a TypeError:
incompatible types, CFunctionType instance instead of c_void_p instance

Any ideas/help (please)?

Best regards,
Matt
Jun 27 '08 #3
Matt <mr*****@gmx.atwrote:
Okay, thanks a lot for your reply Nick, I think you pushed me back on
the right way.
Good!
Now I started with trying to implement the callback functions and am
stuck at the following point:

I define my classes/structures/unions:

class cdStream(Structure):
_fields_ = [("contextH", c_uint),
("open", c_void_p),
[snip]
then i define my functions (right now I do just nothing) and at the same
time I define the datatypes like they're listed in my C sample program:

def pystreamopen (contextH, mode, pErr):
pass

cstreamopen = CFUNCTYPE(c_uint, c_ushort, c_uint)
[snip]
and now the problem starts: i want the pointers in the cdStream
Structure point at my functions and tried to do it the following way:

data = cdStgMedium()
data.type = 0
data.u.pStream.contextH = c_uint(3) #must be some kind of identifier.
data.u.pStream.open = cstreamopen(pystreamopen)
[snip]
unfortunately that doesn't work because Python returns a TypeError:
incompatible types, CFunctionType instance instead of c_void_p instance

Any ideas/help (please)?
Probably the best thing is if you define the union with the types of
the pointers to your functions instead of c_void_p, eg

class cdStream(Structure):
_fields_ = [("contextH", c_uint),
("open", cstreamopen),
("close", cstreamclose), # etc...

This will involve you re-ordering your definitions.

Or alternatively, you could cast the function pointer to a c_void_p
first, eg

data.u.pStream.open = c_void_p( cstreamopen(pystreamopen) )

which should work but is less typesafe.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #4
Hi,

okay, thanks now my DLL function seems to accept the functions given,
but instead of running my program keeps crashing (with windows address
violation errors). I did some further investigations on that and figured
out that the contextH variable is not just an identifier as I thought,
but a quite complicated datastreamhandler.

Now to get it to work I additionally need to implement the following c
Structures:

------------------------------CODE-----------------------------------------

/* The internal data structure object of a stream */
typedef struct tagMemStreamData
{
cdChar mode;
cdInt32 lPos;
cdUInt32 dwVisibleSize;
cdUInt32 dwBufferSize;
cdChar *cpBuffer;
}MemStreamData;

typedef struct tagFilStreamData
{
cdChar szFileName[MAX_PATH];
HANDLE hFile;
}FilStreamData;

------------------------------\CODE----------------------------------------

This function just creates the pStream filestream (which is what I have
to do too)

------------------------------CODE-----------------------------------------

BOOL CreateMyFilStream( cdStream *pStream,
cdChar *szFileName )
{
FilStreamData *pFilStrm;

/* The domain for data is secured. */
pFilStrm = new FilStreamData;
if( pFilStrm == NULL )
{
return FALSE;
}

/* Data is changed the first stage. */
pFilStrm->hFile = INVALID_HANDLE_VALUE;
strcpy( pFilStrm->szFileName, szFileName );

pStream->contextH = (cdContext)pFilStrm;
pStream->close = _CloseMyFilStream;
pStream->open = _OpenMyFilStream;
pStream->read = _ReadMyFilStream;
pStream->seek = _SeekMyFilStream;
pStream->tell = _TellMyFilStream;
pStream->write = _WriteMyFilStream;

return TRUE;
}
------------------------------\CODE----------------------------------------

Now my solution is the following:
------------------------------CODE-----------------------------------------

class MemStreamData(Structure):
_fields_ = [("mode", c_byte),
("lPos", c_uint),
("dwVisibleSize", c_uint),
("dwBufferSize", c_uint),
("cpBuffer", POINTER(c_char))]

class FilStreamData (Structure):
_fields_ = [("szFileName", c_char * 30),
("hFile", c_uint)]
------------------------------\CODE----------------------------------------

and the code to fill all that with the right data is the following:

------------------------------CODE-----------------------------------------

datainfo = cdReleaseImageInfo()

databuf = c_char * 100000
cbuffer=MemStreamData()
cbuffer.mode = c_byte(0)
cbuffer.lPos = c_uint(0)
cbuffer.dwVisibleSize = c_uint(100000)
cbuffer.dwBufferSize = c_uint(100000)

#this line does not work, wrong datatype!?
cbuffer.cpBuffer = POINTER(databuf)

cpointer = cast(cbuffer, POINTER(c_uint))
stream = cdStream()
stream.contextH=cpointer
stream.open = cstreamopen(pystreamopen)
stream.close = cstreamclose(pystreamclose)
stream.write = cstreamwrite(pystreamwrite)
stream.tell = cstreamtell(pystreamtell)
stream.read = cstreamread(pystreamread)

data = cdStgMedium()
data.Type = c_uint(1)
data.u.pStream = POINTER(cdStream)(stream)

------------------------------\CODE----------------------------------------

Does anyone see where the errors are?

Best regards and thanks a lot,
Matt


Jun 27 '08 #5
Matt <mr*****@gmx.atwrote:
class MemStreamData(Structure):
_fields_ = [("mode", c_byte),
("lPos", c_uint),
("dwVisibleSize", c_uint),
("dwBufferSize", c_uint),
("cpBuffer", POINTER(c_char))]

class FilStreamData (Structure):
_fields_ = [("szFileName", c_char * 30),
("hFile", c_uint)]

and the code to fill all that with the right data is the following:
datainfo = cdReleaseImageInfo()

databuf = c_char * 100000
cbuffer=MemStreamData()
cbuffer.mode = c_byte(0)
cbuffer.lPos = c_uint(0)
cbuffer.dwVisibleSize = c_uint(100000)
cbuffer.dwBufferSize = c_uint(100000)

#this line does not work, wrong datatype!?
cbuffer.cpBuffer = POINTER(databuf)
[snip]
Does anyone see where the errors are?
databuf = c_char * 100000

Is a type not an instance...

You need to instantiate it, eg
>>from ctypes import *
class MemStreamData(Structure):
... _fields_ = [("cpBuffer", POINTER(c_char))]
...
>>cbuffer=MemStreamData()
databuf = c_char * 100000
cbuffer.cpBuffer = POINTER(databuf)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected LP_c_char instance, got _ctypes.PointerType
>>databuftype = c_char * 100000
databuf = databuftype()
cbuffer.cpBuffer = databuf
databuf
<__main__.c_char_Array_100000 object at 0xb7d04dac>

There is a shorthand for exactly this though as it is a common operation
>>databuf = create_string_buffer(1000)
cbuffer.cpBuffer = databuf
databuf
<ctypes.c_char_Array_1000 object at 0xb7d129bc>
--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #6
Hello!

ouch, I should have seen that c_char... :S Well, I guess I just prove
that it's useless to go to work and do some programming while having a
headache like I had yesterday...

okay well, back to topic:

The DLL function seems to accept my parameters now, but unfortunately
Python terminates after the DLL gets the result from the "open" callback
function (at least its printouts are the last I get before it
terminates). So without any kind of error message it's getting more
difficult now.

Well, since I didn't invest much time into my callback functions I
suspect the error must be somewhere in there.

This is my code:

------------------------------CODE-----------------------------------------

class MemStreamData(Structure):
_fields_ = [("mode", c_byte),
("lPos", c_uint),
("dwVisibleSize", c_uint),
("dwBufferSize", c_uint),
("cpBuffer", POINTER(c_char))]

class FilStreamData (Structure):
_fields_ = [("szFileName", c_char * 30),
("hFile", c_uint)]


def pystreamopen (contextH, mode, pErr=0):
print "opening..."
print contextH
print mode
print pErr
return 0

cstreamopen = CFUNCTYPE(c_uint, c_ushort, c_uint)

def pystreamclose (contextH, pErr):
print "closing..."
return 0

cstreamclose = CFUNCTYPE(c_uint, c_uint)

def pystreamread (contextH, pBuf, pBufsize, pErr):
print "reading..."
return 0

cstreamread = CFUNCTYPE(c_uint, c_void_p, c_uint, c_uint)

def pystreamtell (contextH, pErr):
print "telling..."
return 0

cstreamtell = CFUNCTYPE(c_uint, c_uint)
def pystreamseek (contextH, origin, offset, pErr):
print "seeking..."
return 0

cstreamseek = CFUNCTYPE(c_uint, c_uint, c_uint, c_uint)
def pystreamwrite (contextH, origin, offset, pErr):
print "writing..."
return 0

cstreamwrite = CFUNCTYPE(c_uint, c_void_p, c_uint, c_uint)
class cdStream(Structure):
_fields_ = [("contextH", POINTER(MemStreamData)),
("open", cstreamopen),
("close", cstreamclose),
("read", cstreamread),
("write", cstreamwrite),
("seek", cstreamseek),
("tell", cstreamtell)]

-----------------------------/CODE-----------------------------------------

This is the way I create the vars:


------------------------------CODE-----------------------------------------
databuf = create_string_buffer(100000)
cbuffer=MemStreamData()
cbuffer.mode = c_byte(0)
cbuffer.lPos = c_uint(0)
cbuffer.dwVisibleSize = 100000
cbuffer.dwBufferSize = 100000
cbuffer.cpBuffer = databuf

stream = cdStream()
stream.contextH = POINTER(MemStreamData)(cbuffer)
stream.open = cstreamopen(pystreamopen)
stream.close = cstreamclose(pystreamclose)
stream.write = cstreamwrite(pystreamwrite)
stream.tell = cstreamtell(pystreamtell)
stream.read = cstreamread(pystreamread)
data = cdStgMedium()
data.Type = c_uint(1) # 0...FilStream 1...MemStream
data.u.pStream = POINTER(cdStream)(stream)

errorcode = cdsdk.CDGetReleasedData(devicehandle, byref(cbfunct),
c_uint(0), c_uint(0), byref(datainfo), POINTER(cdStgMedium)(data))

------------------------------/CODE-----------------------------------------

Now I have two problems:

1st: since contextH is not a c_uint (and pErr is a pointer) as I thought
earlier, I tried to change my definition of the open function to:

cstreamopen = CFUNCTYPE(POINTER(MemStreamData), c_ushort, POINTER(c_uint))

unfortunately that throws an error when I try to:

stream.open = cstreamopen(pystreamopen)
2nd: as may saw, I defined "def pystreamopen (contextH, mode, pErr=0)".
The pErr variable should be a pointer to a c_uint where my function can
tell the DLL that opening the stream went well (or give some errorcode).

When I do not define pErr=0 and simply say pErr, I get the following error:

Traceback (most recent call last):
File "\loewis\25\python\Modules\_ctypes\callbacks.c ", line 206, in
'calling callback function'
TypeError: pystreamopen() takes exactly 3 arguments (2 given)

At first I thought okay, maybe there's no pErr and there's some error in
the C-Code, but when I do "def pystreamopen (contextH, mode)" I get the
same Error with:
TypeError: pystreamopen() takes exactly 3 arguments (2 given)
Any ideas?
And another question: my callback functions are all defined as void...
in C. That means that there shouldn't be anything returned. I tried this
by using the pass statement, but got an error that returntype int was
expected. Also "return" or "return None" don't work. Why?

Puh, long mail again... hope you're so kind again and take the time to
help me out.

Best regards from Austria,
Matt

Jun 27 '08 #7
On Jun 3, 11:22 am, Matt <mr.e...@gmx.atwrote:
Hello!

ouch, I should have seen that c_char... :S Well, I guess I just prove
that it's useless to go to work and do some programming while having a
headache like I had yesterday...

okay well, back to topic:

The DLL function seems to accept my parameters now, but unfortunately
Python terminates after the DLL gets the result from the "open" callback
function (at least its printouts are the last I get before it
terminates). So without any kind of error message it's getting more
difficult now.

Well, since I didn't invest much time into my callback functions I
suspect the error must be somewhere in there.

This is my code:

------------------------------CODE-----------------------------------------

class MemStreamData(Structure):
_fields_ = [("mode", c_byte),
("lPos", c_uint),
("dwVisibleSize", c_uint),
("dwBufferSize", c_uint),
("cpBuffer", POINTER(c_char))]

class FilStreamData (Structure):
_fields_ = [("szFileName", c_char * 30),
("hFile", c_uint)]

def pystreamopen (contextH, mode, pErr=0):
print "opening..."
print contextH
print mode
print pErr
return 0

cstreamopen = CFUNCTYPE(c_uint, c_ushort, c_uint)

def pystreamclose (contextH, pErr):
print "closing..."
return 0

cstreamclose = CFUNCTYPE(c_uint, c_uint)

def pystreamread (contextH, pBuf, pBufsize, pErr):
print "reading..."
return 0

cstreamread = CFUNCTYPE(c_uint, c_void_p, c_uint, c_uint)

def pystreamtell (contextH, pErr):
print "telling..."
return 0

cstreamtell = CFUNCTYPE(c_uint, c_uint)

def pystreamseek (contextH, origin, offset, pErr):
print "seeking..."
return 0

cstreamseek = CFUNCTYPE(c_uint, c_uint, c_uint, c_uint)

def pystreamwrite (contextH, origin, offset, pErr):
print "writing..."
return 0

cstreamwrite = CFUNCTYPE(c_uint, c_void_p, c_uint, c_uint)

class cdStream(Structure):
_fields_ = [("contextH", POINTER(MemStreamData)),
("open", cstreamopen),
("close", cstreamclose),
("read", cstreamread),
("write", cstreamwrite),
("seek", cstreamseek),
("tell", cstreamtell)]

-----------------------------/CODE-----------------------------------------

This is the way I create the vars:

------------------------------CODE-----------------------------------------

databuf = create_string_buffer(100000)
cbuffer=MemStreamData()
cbuffer.mode = c_byte(0)
cbuffer.lPos = c_uint(0)
cbuffer.dwVisibleSize = 100000
cbuffer.dwBufferSize = 100000
cbuffer.cpBuffer = databuf

stream = cdStream()
stream.contextH = POINTER(MemStreamData)(cbuffer)
stream.open = cstreamopen(pystreamopen)
stream.close = cstreamclose(pystreamclose)
stream.write = cstreamwrite(pystreamwrite)
stream.tell = cstreamtell(pystreamtell)
stream.read = cstreamread(pystreamread)

data = cdStgMedium()
data.Type = c_uint(1) # 0...FilStream 1...MemStream
data.u.pStream = POINTER(cdStream)(stream)

errorcode = cdsdk.CDGetReleasedData(devicehandle, byref(cbfunct),
c_uint(0), c_uint(0), byref(datainfo), POINTER(cdStgMedium)(data))

------------------------------/CODE-----------------------------------------

Now I have two problems:

1st: since contextH is not a c_uint (and pErr is a pointer) as I thought
earlier, I tried to change my definition of the open function to:

cstreamopen = CFUNCTYPE(POINTER(MemStreamData), c_ushort, POINTER(c_uint))

unfortunately that throws an error when I try to:

stream.open = cstreamopen(pystreamopen)

2nd: as may saw, I defined "def pystreamopen (contextH, mode, pErr=0)".
The pErr variable should be a pointer to a c_uint where my function can
tell the DLL that opening the stream went well (or give some errorcode).

When I do not define pErr=0 and simply say pErr, I get the following error:

Traceback (most recent call last):
File "\loewis\25\python\Modules\_ctypes\callbacks.c ", line 206, in
'calling callback function'
TypeError: pystreamopen() takes exactly 3 arguments (2 given)

At first I thought okay, maybe there's no pErr and there's some error in
the C-Code, but when I do "def pystreamopen (contextH, mode)" I get the
same Error with:
TypeError: pystreamopen() takes exactly 3 arguments (2 given)

Any ideas?

And another question: my callback functions are all defined as void...
in C. That means that there shouldn't be anything returned. I tried this
by using the pass statement, but got an error that returntype int was
expected. Also "return" or "return None" don't work. Why?

Puh, long mail again... hope you're so kind again and take the time to
help me out.
The docs say CFUNCTYPE(restype, *argtypes), so:

cstreamopen = CFUNCTYPE(c_uint, c_ushort, c_uint)

is saying that the result type is c_uint, not void. I think you need:

cstreamopen = CFUNCTYPE(None, c_uint, c_ushort, c_uint)

instead.
Jun 27 '08 #8
The docs say CFUNCTYPE(restype, *argtypes), so:

cstreamopen = CFUNCTYPE(c_uint, c_ushort, c_uint)

is saying that the result type is c_uint, not void. I think you need:

cstreamopen = CFUNCTYPE(None, c_uint, c_ushort, c_uint)

instead.
Hm, thanks, now I can access my data in the functions and also write
them but the program keeps terminating right at the point when the
"open" function finishes. Unfortunately everything closes and I get no
error messages.

I did some additional work in the meantime and changed my code so it has
the correct datatypes now:

--------------------------CODE--------------------------------------------

def pystreamopen (contextH, mode, pErr):
print "opening..."
print contextH.contents.dwBufferSize #just to check the structure
print mode #tells about what the DLL wants to do with this stream
contextH.contents.mode = c_byte(5) #5=Permission to read and write
contextH.contents.lPos = c_uint(0) #start position

print pErr.contents
pErr.contents = c_uint(0)
cstreamopen = CFUNCTYPE(None, POINTER(MemStreamData), c_ushort,
POINTER(c_uint))

def pystreamclose (contextH, pErr):
print "closing..."
pass

cstreamclose = CFUNCTYPE(None, POINTER(MemStreamData), POINTER(c_uint))

def pystreamread (contextH, pBuf, pBufsize, pErr):
print "reading..."
pass

cstreamread = CFUNCTYPE(None, POINTER(MemStreamData), c_uint, c_uint,
POINTER(c_uint))

def pystreamtell (contextH, pErr):
print "telling...

..
..
..etc...

--------------------------/CODE--------------------------------------------

and the function call:

--------------------------CODE--------------------------------------------

errorcode = cdsdk.CDGetReleasedData(devicehandle, byref(cbfunct),
c_uint(0), c_uint(0), byref(datainfo), POINTER(cdStgMedium)(data))

--------------------------/CODE--------------------------------------------
while it's running my program prints the following and then
exits/terminates:
>>opening...990001
2
c_ulong(0L)
Script terminated. ##This is a message by my editor (SPE)
I also tried different editors, but get the same result...

Anyway, meanwhile decided to try a different approach. Maybe I have
more luck by having the function write the data directly into a file on
the HDD.
Doe anyone know how to translate the following into Python/ctypes?

I googled quite a lot before but all topic-related I found was my own
posting here in this NG :S

--------------------------CODE--------------------------------------------

pFilStrm->hFile = CreateFile( pFilStrm->szFileName,
dwDesiredAccess, dwShareMode, NULL,
dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL );
--------------------------/CODE--------------------------------------------

This function is obviously a default C-function that generates a file
and returns a c-handle to that file. In my case I'd need exactly such a
handle to get things working...

Furthermore there's a writefile function I'd need to translate too:

--------------------------CODE--------------------------------------------

WriteFile( pFilStrm->hFile, pBuf, dwNumberOfBytesToWrite, pBufsize, NULL );

--------------------------/CODE--------------------------------------------
Ideas?

Thanks and best wishes,
Matt
Jun 27 '08 #9
Matt <mr*****@gmx.atwrote:
Hm, thanks, now I can access my data in the functions and also write
them but the program keeps terminating right at the point when the
"open" function finishes. Unfortunately everything closes and I get no
error messages.

I did some additional work in the meantime and changed my code so it has
the correct datatypes now:
def pystreamopen (contextH, mode, pErr):
print "opening..."
print contextH.contents.dwBufferSize #just to check the structure
print mode #tells about what the DLL wants to do with this stream
You program is crashing somewhere after here since mode is printed but
nothing else is
contextH.contents.mode = c_byte(5) #5=Permission to read and write
contextH.contents.lPos = c_uint(0) #start position

print pErr.contents
pErr.contents = c_uint(0)
Try commenting out these lines and see if it works, then uncomment one
at a time.

Also is that supposed to be returning something?
Anyway, meanwhile decided to try a different approach. Maybe I have
more luck by having the function write the data directly into a file on
the HDD.
Doe anyone know how to translate the following into Python/ctypes?

I googled quite a lot before but all topic-related I found was my own
posting here in this NG :S

pFilStrm->hFile = CreateFile( pFilStrm->szFileName,
dwDesiredAccess, dwShareMode, NULL,
dwCreationDisposition,
FILE_ATTRIBUTE_NORMAL,
NULL );
use os.read os.write and os.open which will give you OS handles rather
than python file objects, ie I think these are a fairly direct
interface to CreatFile etc (but I could be wrong - I'm not a windows
expert!)

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: achrist | last post by:
I'm aving some trouble getting callbacks from a Delphi DLL back to python working through ctypes. The call from python to the DLL works fine. In the Delphi(5) code: type TCallbackFunc =...
1
by: Thomas Heller | last post by:
ctypes 0.9.1 released - Sept 14, 2004 ===================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
3
by: Lenard Lindstrom | last post by:
Posted in a previous thread was some Python code for accessing Window's Simple MAPI api using the ctypes module. http://groups-beta.google.com/group/comp.lang.python/msg/56fa74cdba9b7be9 This...
3
by: rubbishemail | last post by:
Hello, i've got a problem with pointers in the following function which i want to use: I16 __stdcall DO_ReadPort (U16 CardNumber, U16 Port, U32 *Value) The function is supposed to read out...
2
by: cantankerousoldgit | last post by:
I am trying to call a DLL with a function like this: """DESCRIPTION: Get a list of objects attributes matching attribute values ARGUMENTS: session : the current session classId :...
12
by: p.lavarre | last post by:
Q: The C idea of (pv != NULL) is said most directly in Python ctypes how? A: We are of course supposed to write something like: def c_not_null(pv): return (ctypes.cast(pv,...
5
by: tkondal | last post by:
Hi all. I just started looking at Python's ctypes lib and I am having trouble using it for a function. For starters, here's my Python code: from ctypes import*; myStringDLL=...
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
3
by: Chris AtLee | last post by:
Sorry for the repeat post...I'm not sure if my first post (on May 30th) went through or not. I've been trying to write a PAM module using ctypes. In the conversation function (my_conv in the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.