473,385 Members | 1,888 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,385 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 3985
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.