473,757 Members | 8,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(cdConte xt contextH, cdPermission,
cdError* err);
typedef void cdSTDCALL cdSClose(cdCont ext contextH, cdError* err);
typedef void cdSTDCALL cdSRead(cdConte xt contextH, void* buf,
cdUInt32* bufsize, cdError* err);
typedef void cdSTDCALL cdSWrite(cdCont ext contextH, const void *buf,
cdUInt32* bufsize, cdError *err);
typedef void cdSTDCALL cdSSeek(cdConte xt contextH, cdWhence, cdInt32
offset, cdError* err);
typedef cdInt32 cdSTDCALL cdSTell(cdConte xt contextH, cdError* err);
typedef void cdSTDCALL cdSProgress(cdC ontext 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 CDGetReleasedDa ta(
cdHSource hSource,
cdProgressCallb ackFunction * pCallbackFunc,
cdContext Context,
cdProgressOptio n ProgressOption,
cdReleaseImageI nfo* 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 (CDGetReleasedD ata) 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 cdReleaseImageI nfo(Structure):
_fields_ = [("SequenceID ", c_uint),
("DataType", c_uint),
("Format", c_ubyte),
("DataSize", c_uint),
("Filename", c_char * 2)]
class cdStream(Struct ure):
_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(Str ucture):
_fields_ = [("Type", c_uint),
("u", memunion)]


# command:

datainfo = cdReleaseImageI nfo()
data = cdStgMedium()
errorcode = cdsdk.CDGetRele asedData(device handle, byref(cbfunct),
c_uint(1), c_uint(1), byref(datainfo) , byref(data))

------------------------------\CODE----------------------------------------
The function "cdsdk.CDGetRel easedData" 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 7348
Matt <mr*****@gmx.at wrote:
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 (CDGetReleasedD ata) 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 cdReleaseImageI nfo(Structure):
_fields_ = [("SequenceID ", c_uint),
("DataType", c_uint),
("Format", c_ubyte),
("DataSize", c_uint),
("Filename", c_char * 2)]
class cdStream(Struct ure):
_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(cdStrea m)
class cdStgMedium(Str ucture):
_fields_ = [("Type", c_uint),
("u", memunion)]


# command:

datainfo = cdReleaseImageI nfo()
data = cdStgMedium()
errorcode = cdsdk.CDGetRele asedData(device handle, byref(cbfunct),
c_uint(1), c_uint(1), byref(datainfo) , byref(data))

The function "cdsdk.CDGetRel easedData" 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(Struct ure):
_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(Str ucture):
_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_uin t, c_ushort, c_uint)

def pystreamclose (contextH, pErr):
pass

cstreamclose = CFUNCTYPE(c_uin t, c_uint)

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

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

def pystreamtell (contextH, pErr):
pass

cstreamtell = CFUNCTYPE(c_uin t, c_uint)

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

cstreamwrite = CFUNCTYPE(c_uin t, 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(pys treamopen)
data.u.pStream. close = cstreamclose(py streamclose)
data.u.pStream. write = cstreamwrite(py streamwrite)
data.u.pStream. tell = cstreamtell(pys treamtell)
data.u.pStream. read = cstreamread(pys treamread)

------------------------------\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.at wrote:
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(Struct ure):
_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_uin t, 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(pys treamopen)
[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(Struct ure):
_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(pys treamopen) )

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 datastreamhandl er.

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 tagMemStreamDat a
{
cdChar mode;
cdInt32 lPos;
cdUInt32 dwVisibleSize;
cdUInt32 dwBufferSize;
cdChar *cpBuffer;
}MemStreamData;

typedef struct tagFilStreamDat a
{
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 CreateMyFilStre am( 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)pFil Strm;
pStream->close = _CloseMyFilStre am;
pStream->open = _OpenMyFilStrea m;
pStream->read = _ReadMyFilStrea m;
pStream->seek = _SeekMyFilStrea m;
pStream->tell = _TellMyFilStrea m;
pStream->write = _WriteMyFilStre am;

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

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

class MemStreamData(S tructure):
_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 = cdReleaseImageI nfo()

databuf = c_char * 100000
cbuffer=MemStre amData()
cbuffer.mode = c_byte(0)
cbuffer.lPos = c_uint(0)
cbuffer.dwVisib leSize = c_uint(100000)
cbuffer.dwBuffe rSize = c_uint(100000)

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

cpointer = cast(cbuffer, POINTER(c_uint) )
stream = cdStream()
stream.contextH =cpointer
stream.open = cstreamopen(pys treamopen)
stream.close = cstreamclose(py streamclose)
stream.write = cstreamwrite(py streamwrite)
stream.tell = cstreamtell(pys treamtell)
stream.read = cstreamread(pys treamread)

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

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

Does anyone see where the errors are?

Best regards and thanks a lot,
Matt


Jun 27 '08 #5
Matt <mr*****@gmx.at wrote:
class MemStreamData(S tructure):
_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 = cdReleaseImageI nfo()

databuf = c_char * 100000
cbuffer=MemStre amData()
cbuffer.mode = c_byte(0)
cbuffer.lPos = c_uint(0)
cbuffer.dwVisib leSize = c_uint(100000)
cbuffer.dwBuffe rSize = c_uint(100000)

#this line does not work, wrong datatype!?
cbuffer.cpBuffe r = 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(S tructure):
... _fields_ = [("cpBuffer", POINTER(c_char) )]
...
>>cbuffer=MemSt reamData()
databuf = c_char * 100000
cbuffer.cpBuf fer = POINTER(databuf )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected LP_c_char instance, got _ctypes.Pointer Type
>>databuftype = c_char * 100000
databuf = databuftype()
cbuffer.cpBuf fer = databuf
databuf
<__main__.c_cha r_Array_100000 object at 0xb7d04dac>

There is a shorthand for exactly this though as it is a common operation
>>databuf = create_string_b uffer(1000)
cbuffer.cpBuf fer = 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(S tructure):
_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_uin t, c_ushort, c_uint)

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

cstreamclose = CFUNCTYPE(c_uin t, c_uint)

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

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

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

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

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

cstreamwrite = CFUNCTYPE(c_uin t, c_void_p, c_uint, c_uint)
class cdStream(Struct ure):
_fields_ = [("contextH", POINTER(MemStre amData)),
("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_b uffer(100000)
cbuffer=MemStre amData()
cbuffer.mode = c_byte(0)
cbuffer.lPos = c_uint(0)
cbuffer.dwVisib leSize = 100000
cbuffer.dwBuffe rSize = 100000
cbuffer.cpBuffe r = databuf

stream = cdStream()
stream.contextH = POINTER(MemStre amData)(cbuffer )
stream.open = cstreamopen(pys treamopen)
stream.close = cstreamclose(py streamclose)
stream.write = cstreamwrite(py streamwrite)
stream.tell = cstreamtell(pys treamtell)
stream.read = cstreamread(pys treamread)
data = cdStgMedium()
data.Type = c_uint(1) # 0...FilStream 1...MemStream
data.u.pStream = POINTER(cdStrea m)(stream)

errorcode = cdsdk.CDGetRele asedData(device handle, byref(cbfunct),
c_uint(0), c_uint(0), byref(datainfo) , POINTER(cdStgMe dium)(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(POINT ER(MemStreamDat a), c_ushort, POINTER(c_uint) )

unfortunately that throws an error when I try to:

stream.open = cstreamopen(pys treamopen)
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\pyt hon\Modules\_ct ypes\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.at wrote:
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(S tructure):
_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_uin t, c_ushort, c_uint)

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

cstreamclose = CFUNCTYPE(c_uin t, c_uint)

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

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

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

cstreamtell = CFUNCTYPE(c_uin t, c_uint)

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

cstreamseek = CFUNCTYPE(c_uin t, c_uint, c_uint, c_uint)

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

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

class cdStream(Struct ure):
_fields_ = [("contextH", POINTER(MemStre amData)),
("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_b uffer(100000)
cbuffer=MemStre amData()
cbuffer.mode = c_byte(0)
cbuffer.lPos = c_uint(0)
cbuffer.dwVisib leSize = 100000
cbuffer.dwBuffe rSize = 100000
cbuffer.cpBuffe r = databuf

stream = cdStream()
stream.contextH = POINTER(MemStre amData)(cbuffer )
stream.open = cstreamopen(pys treamopen)
stream.close = cstreamclose(py streamclose)
stream.write = cstreamwrite(py streamwrite)
stream.tell = cstreamtell(pys treamtell)
stream.read = cstreamread(pys treamread)

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

errorcode = cdsdk.CDGetRele asedData(device handle, byref(cbfunct),
c_uint(0), c_uint(0), byref(datainfo) , POINTER(cdStgMe dium)(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(POINT ER(MemStreamDat a), c_ushort, POINTER(c_uint) )

unfortunately that throws an error when I try to:

stream.open = cstreamopen(pys treamopen)

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\pyt hon\Modules\_ct ypes\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(resty pe, *argtypes), so:

cstreamopen = CFUNCTYPE(c_uin t, 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(resty pe, *argtypes), so:

cstreamopen = CFUNCTYPE(c_uin t, 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.conten ts.dwBufferSize #just to check the structure
print mode #tells about what the DLL wants to do with this stream
contextH.conten ts.mode = c_byte(5) #5=Permission to read and write
contextH.conten ts.lPos = c_uint(0) #start position

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

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

cstreamclose = CFUNCTYPE(None, POINTER(MemStre amData), POINTER(c_uint) )

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

cstreamread = CFUNCTYPE(None, POINTER(MemStre amData), c_uint, c_uint,
POINTER(c_uint) )

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

..
..
..etc...

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

and the function call:

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

errorcode = cdsdk.CDGetRele asedData(device handle, byref(cbfunct),
c_uint(0), c_uint(0), byref(datainfo) , POINTER(cdStgMe dium)(data))

--------------------------/CODE--------------------------------------------
while it's running my program prints the following and then
exits/terminates:
>>opening...990 001
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,
dwCreationDispo sition, 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, dwNumberOfBytes ToWrite, pBufsize, NULL );

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

Thanks and best wishes,
Matt
Jun 27 '08 #9
Matt <mr*****@gmx.at wrote:
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.conten ts.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.conten ts.mode = c_byte(5) #5=Permission to read and write
contextH.conten ts.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,
dwCreationDispo sition,
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
4030
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 = function(x: Integer): Integer; stdcall;
1
2588
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 functions exposed from dlls/shared libraries and has extensive facilities to create, access and manipulate
3
5546
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 Simple MAPI module was Ian's completed version of an example I had posted in an earlier message. In it I had to set some pointer fields in a C structure to NULL. I was not happy with the solution I used so I made an inquiry on the ctypes-users...
3
5005
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 the status of a digital port of analog digital interface card. I got this function from Dask.h which came with the card. The relevant lines concerning this function are the following:
2
2329
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 : the class Id of objects owning attributes to
12
10125
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, ctypes.c_void_p).value != None) Yes?
5
3499
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= cdll.LoadLibrary("myStringDLL.dll");
2
4434
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 * len(id) # id is a list of strings Id_dat=StringVector() for i in range(len(Id)): ....Id_dat=id
3
2692
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 script below), you're passed in a pam_response** pointer. You're supposed to allocate an array of pam_response's and set
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.