[Q] ctypes callbacks with Delphi

A

achrist

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;

var
Callback1: TCallbackFunc;
CallbackB: TCallbackFunc;


In the Python code:

cFunctionType = ctypes.WINFUNCTYPE(ctypes.c_int,ctypes.c_int)

pyCallback1 = cFunctionType(pyFunc1)

myDLL.SetCallback1(pyCallback1)

Then I call the DLL, eg:

myDLL.BigFunc()

And that runs fine, and the callback works fine until it returns.

At that point the python callback function executes:

answer = 0
return answer

or:
answer = 1
return answer

At this point, the enterprise fails, with an error message of
"Access violation at address 00959D2C. Write of address 00000000",
or "Access violation at address 00959D2C. Write of address 00000001"
where the 8-digit number at the end of the message is the same as
the value returned by the callback function.

I think that I'm close, but I don't have a clue.

TIA for any ideas.


Al
 
A

achrist

I'm aving some trouble getting callbacks from a Delphi DLL back to
python working through ctypes.

... see previous message for details


Same problem persists if I try using cdecl convention throughout.

I've tried not returning a value from the python callback (this gives a
python message about illegal access at 00000FEB) and I've
tried ignoring the value returned to the Delphi DLL (still gives
illegal write of address 00000000 or 00000001.) No success.

I see that there are some updates to the C source for ctypes that
are just a little bit newer than the version 0.6.2a that I am using.
I rebuilt _ctypes.pyd from the CVS snapshot of yesterday, but that
still gives the same problem.

Any ideas about this?

TIA,


Al
 
A

Andrew MacIntyre

I'm aving some trouble getting callbacks from a Delphi DLL back to
python working through ctypes.

I suggest that this question will be more visible on the ctypes mailing
list. I vaguely recall a question along these lines some time ago, so
viewing the list archives might be useful.
 
A

achrist

Andrew said:
I suggest that this question will be more visible on the ctypes mailing
list. I vaguely recall a question along these lines some time ago, so
viewing the list archives might be useful.

I've been over those ASPN list archives as best I can, and I don't
see anything that solves this problem. However, trying to
search the lists is a little tricky. I did a search on 'callback'
and it shows me results 1-20, which don't show me anything that
I think will help, and clicking on '21-40' gives me advertising and
no more messages. IDK.

When I search for 'Delphi', nothing comes up. This can't be a Delphi
problem, can it? Delphi can't be that broken, can it?

When I search for the terms in the error message that I get, 'Access
Violation' and 'write address', I don't see a solution obviously
anywhere. The ASPN pages load very slowly on my machine over dial-up,
so it's no fun hunting around for something that you don't know is
there.

I see that I have to subscribe to the list to post a message. So, I
try to subscribe and go to the ASPN page to subscribe, and they list
about 40 groups, but no ctypes-users. Looks like subscriptions
to this one are not currently available to such as me.

The ctypes tutorial makes this look very easy, and I think that I'm
doing what it shows, and it almost works, but not 100%. IDK.

The unit tests that come with ctypes have a test for callbacks from
a DLL, so I try to look at that to see what goes on in the DLL to
see if I can write my DLL the same way. But they don't include source
for the DLL. They don't even call a real DLL. They test ctypes with
a synthetic DLL of some kind created by a ctypes CDLL macro. So the
unit test only shows that ctypes can talk to itself, not maybe a real
DLL. IDK.

If anyone is using ctypes with called-back functions written in python
returning (simple integer) values back to a DLL written in C or Delphi,
such DLL called from python through ctypes, I'd sure like to see
what the python and C or Delphi code that work together look like --
how the identity of the called-back function is passed to the DLL and
how the called-back function sends back its return value. I'm
trying to follow the tutorial, which looks very easy, but I've not got
it right.

TIA and thanks for your help,


Al
 
A

achrist

I've tracked this down to some kind of an interaction between
ctypes and the GUI'S.

Python calls Delphi through ctypes. Delphi opens a form on the
screen. When Delphi calls back to python through ctypes, the
Delphi form is still open.

If the called-back function in python uses its own dialog to
get the callback answers (I'm using wxPython), then the return
value back to the Delphi dll gets messed up and everything crashes
as previously described. If the called back function figures
out a value without doing anything GUI, it works fine.

I suppose that the source of this problem is something way deep
down in ctypes where it is handling threads or something, IDK.

Tomorrow I'll try to figure out if exception handling code on the
Delphi side can at least catch the error. If so, I'll communicate
the called-back's return value back to Delphi through a
callcallbackback function and everything might fly. If Delphi
can't catch and recover from the error, I'm hurting.

Too late to figure this out now. Any guesses or alternative
contingency plans???


Al
 
A

Andrew MacIntyre

I see that I have to subscribe to the list to post a message. So, I
try to subscribe and go to the ASPN page to subscribe, and they list
about 40 groups, but no ctypes-users. Looks like subscriptions
to this one are not currently available to such as me.

Hmmm, the ctypes-users list is hosted at SF, and you should be able to
subscribe via the ctypes project page - at least that's what I remember
doing. I didn't go anywhere near ASPN.

If most of your GUI is from the Delphi end, and you just want the odd
dialog from Python, writing Python/ctypes/Win32 API modal dialogs isn't
hard and would save the wxPython baggage.

Regards,
Andrew.
 
A

achrist

Andrew said:
Hmmm, the ctypes-users list is hosted at SF, and you should be able to
subscribe via the ctypes project page - at least that's what I remember
doing. I didn't go anywhere near ASPN.

From what I saw, the ctypes list at Sourceforge has only 3 messages
in the archive, and it is concerning devvelopment of ctypes, not
use of ctypes. Is there another one?


Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top