[Q] ctypes callbacks with Delphi

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • achrist@easystreet.com

    [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.WINFUNCT YPE(ctypes.c_in t,ctypes.c_int)

    pyCallback1 = cFunctionType(p yFunc1)

    myDLL.SetCallba ck1(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
  • achrist@easystreet.com

    #2
    Re: [Q] ctypes callbacks with Delphi

    achrist@easystr eet.com wrote:[color=blue]
    >
    > I'm aving some trouble getting callbacks from a Delphi DLL back to
    > python working through ctypes.
    >
    > ... see previous message for details[/color]


    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

    Comment

    • Andrew MacIntyre

      #3
      Re: [Q] ctypes callbacks with Delphi

      On Fri, 17 Oct 2003 achrist@easystr eet.com wrote:
      [color=blue]
      > I'm aving some trouble getting callbacks from a Delphi DLL back to
      > python working through ctypes.[/color]

      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: andymac@bullsey e.apana.org.au (pref) | Snail: PO Box 370
      andymac@pcug.or g.au (alt) | Belconnen ACT 2616
      Web: http://www.andymac.org/ | Australia

      Comment

      • achrist@easystreet.com

        #4
        Re: [Q] ctypes callbacks with Delphi

        Andrew MacIntyre wrote:[color=blue]
        >
        > On Fri, 17 Oct 2003 achrist@easystr eet.com wrote:
        >[color=green]
        > > I'm aving some trouble getting callbacks from a Delphi DLL back to
        > > python working through ctypes.[/color]
        >
        > 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.
        >[/color]

        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

        Comment

        • achrist@easystreet.com

          #5
          Re: [Q] ctypes callbacks with Delphi

          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
          callcallbackbac k 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

          Comment

          • John J. Lee

            #6
            Re: [Q] ctypes callbacks with Delphi

            achrist@easystr eet.com writes:
            [...][color=blue]
            > about 40 groups, but no ctypes-users. Looks like subscriptions
            > to this one are not currently available to such as me.[/color]

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

            ctypes is a Python module allowing to create and manipulate C data types in Python. These can then be passed to C-functions loaded from dynamic link…



            John

            Comment

            • Andrew MacIntyre

              #7
              Re: [Q] ctypes callbacks with Delphi

              On Sat, 18 Oct 2003 achrist@easystr eet.com wrote:
              [color=blue]
              > 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.[/color]

              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: andymac@bullsey e.apana.org.au (pref) | Snail: PO Box 370
              andymac@pcug.or g.au (alt) | Belconnen ACT 2616
              Web: http://www.andymac.org/ | Australia

              Comment

              • achrist@easystreet.com

                #8
                Re: [Q] ctypes callbacks with Delphi

                Andrew MacIntyre wrote:[color=blue]
                >
                >
                > 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.
                >[/color]

                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

                Comment

                • achrist@easystreet.com

                  #9
                  Re: [Q] ctypes callbacks with Delphi

                  "John J. Lee" wrote:[color=blue]
                  >
                  > Googling for "<mailing list name> info page" (no quotes) finds mailman
                  > lists.
                  >
                  > http://lists.sourceforge.net/lists/l...o/ctypes-users
                  >[/color]

                  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

                  Comment

                  Working...