473,237 Members | 1,395 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,237 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 3968
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: HankC | last post by:
Greetings: I'm been programming Delphi for a while and am trying to get into the Python mindset. In Delphi, you can have a component class that has properties, methods, and events. Properties...
2
by: zapazap | last post by:
Dear Snake Charming Gurus, (Was: http://mail.python.org/pipermail/python-list/2004-January/204454.html) First, a thank you to Tim Golden, Thomas Heller, and Mark Hammond for your earlier help...
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...
19
by: Thomas Heller | last post by:
ctypes 0.9.2 released - Oct 28, 2004 ==================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
1
by: sjdevnull | last post by:
Hey, I'm trying to wrap GNU readline with ctypes (the Python readline library doesn't support the callback interface), but I can't figure out how to set values to a variable inside the library. ...
7
by: p.lavarre | last post by:
How do I vary the byte offset of a field of a ctypes.Structure? How do I "use the dynamic nature of Python, and (re-)define the data type after the required size is already known, on a case by...
6
by: Jack | last post by:
I'm not able to build IP2Location's Python interface so I'm trying to use ctypes to call its C interface. The functions return a pointer to the struct below. I haven't been able to figure out how...
0
by: Egor Zindy | last post by:
Egor Zindy wrote: #!/usr/bin/env python """ A generic chipid library based on ctypes This module handles most of the functions in FTChipID.dll
3
by: Andrew Lentvorski | last post by:
Basically, I'd like to use the ctypes module as a much more descriptive "struct" module. Is there a way to take a ctypes.Structure-based class and convert it to/from a binary string? Thanks,...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
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
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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...

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.