471,319 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

[Q] ctypes callbacks with Delphi

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
Jul 18 '05 #1
8 3791
ac*****@easystreet.com wrote:

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
Jul 18 '05 #2
On Fri, 17 Oct 2003 ac*****@easystreet.com wrote:
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.

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: an*****@bullseye.apana.org.au (pref) | Snail: PO Box 370
an*****@pcug.org.au (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia

Jul 18 '05 #3
Andrew MacIntyre wrote:

On Fri, 17 Oct 2003 ac*****@easystreet.com wrote:
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.


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
Jul 18 '05 #4
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
Jul 18 '05 #5
ac*****@easystreet.com writes:
[...]
about 40 groups, but no ctypes-users. Looks like subscriptions
to this one are not currently available to such as me.


Googling for "<mailing list name> info page" (no quotes) finds mailman
lists.

http://lists.sourceforge.net/lists/l...o/ctypes-users
John
Jul 18 '05 #6
On Sat, 18 Oct 2003 ac*****@easystreet.com wrote:
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.

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: an*****@bullseye.apana.org.au (pref) | Snail: PO Box 370
an*****@pcug.org.au (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia

Jul 18 '05 #7
Andrew MacIntyre wrote:


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
Jul 18 '05 #8
"John J. Lee" wrote:

Googling for "<mailing list name> info page" (no quotes) finds mailman
lists.

http://lists.sourceforge.net/lists/l...o/ctypes-users


Thanks. The sourceforge page I saw only listed a ctypes
developers group. I guess that the ASPN archive might just
be a copy of that list.
Al
Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by HankC | last post: by
1 post views Thread by Thomas Heller | last post: by
19 posts views Thread by Thomas Heller | last post: by
1 post views Thread by sjdevnull | last post: by
6 posts views Thread by Jack | last post: by
3 posts views Thread by Andrew Lentvorski | last post: by
reply views Thread by rosydwin | last post: by

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.