Call C functions from Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Java and Swing

    #1

    Call C functions from Python

    Is there some other way, besides SWIG, which will allow me to call
    functions inside an Ansi C DLL?

    Example (C):

    defs.h
    -------
    typedef unsigned long MY_DIGIT;

    myapp.c
    -----------------
    #include "defs.h"

    char *DoSomeStuff(ch ar *input, MY_DIGIT *digits) {
    ...
    }


    ...thats an example of something I would like to call from Python.

  • Grant Edwards

    #2
    Re: Call C functions from Python

    On 2005-10-04, Java and Swing <codecraig@gmai l.com> wrote:
    [color=blue]
    > Is there some other way, besides SWIG, which will allow me to call
    > functions inside an Ansi C DLL?[/color]

    ctypes

    --
    Grant Edwards grante Yow! Now KEN and BARBIE
    at are PERMANENTLY ADDICTED to
    visi.com MIND-ALTERING DRUGS...

    Comment

    • Java and Swing

      #3
      Re: Call C functions from Python

      ok i got ctypes...now i try
      [color=blue][color=green]
      >> from ctypes import *
      >> myApp = CDLL("C:\\myapp .dll")[/color][/color]

      ...now how can I call functions on in myapp.dll? From the tutorial I am
      not sure..i try, dir(cdll.myApp) and dir(myApp)..but don't see my
      functions listed.

      thanks

      Grant Edwards wrote:[color=blue]
      > On 2005-10-04, Java and Swing <codecraig@gmai l.com> wrote:
      >[color=green]
      > > Is there some other way, besides SWIG, which will allow me to call
      > > functions inside an Ansi C DLL?[/color]
      >
      > ctypes
      >
      > --
      > Grant Edwards grante Yow! Now KEN and BARBIE
      > at are PERMANENTLY ADDICTED to
      > visi.com MIND-ALTERING DRUGS...[/color]

      Comment

      • Grant Edwards

        #4
        Re: Call C functions from Python

        On 2005-10-04, Java and Swing <codecraig@gmai l.com> wrote:[color=blue]
        > ok i got ctypes...now i try
        >[color=green][color=darkred]
        >>> from ctypes import *
        >>> myApp = CDLL("C:\\myapp .dll")[/color][/color][/color]

        I've never seen that sort of usage before. I don't know what
        CDLL does, and I can't find it in the docs anywhere.

        Based on my reading of the tutorial, I would have tried eitehr

        myApp = cdll.myapp
        or
        myApp = cdll.LoadLibrar y("C:/myapp.dll")
        [color=blue]
        > ..now how can I call functions on in myapp.dll? From the
        > tutorial I am not sure..[/color]

        Assuming CDLL did something equivalent to cdll.LoadLibrar y(),
        I'd try:

        myApp.FuncName( )

        I've always done it the way it's done in the tutorial:

        mylib = windll.Lib_Name
        mylib.myFuncNam e()
        [color=blue]
        > i try, dir(cdll.myApp) and dir(myApp)..but don't see my
        > functions listed.[/color]

        I don't think dir() works on dll's like that. I certainly
        don't see it mentioned in the tutorial. What happened when you
        tried calling the function the way the tutorial does?

        myapp = cdll.myapp
        myapp.MyFunc()

        --
        Grant Edwards grante Yow! Yow! Is my fallout
        at shelter termite proof?
        visi.com

        Comment

        • Martin v. Löwis

          #5
          Re: Call C functions from Python

          Java and Swing wrote:[color=blue]
          > Is there some other way, besides SWIG, which will allow me to call
          > functions inside an Ansi C DLL?[/color]

          You could write an extension module. See Modules/xxmodule.c in
          the Python source tree, as well as



          In the specific case, if it weren't for the digits argument,
          the wrapper function would read

          static PyObject*
          Py_DoSomeStuff( PyObject*unused , PyObject* args)
          {
          char *input;
          if (!PyArg_ParseTu ple("s:DoSomeSt uff", &input))
          return NULL;
          return PyString_FromSt ring(DoSomeStuf f(input));
          }

          I cannot extend this to digits, as I don't know how
          the digits are represented if there is more than one
          (i.e. how does DoSomeStuff know how many digits are
          being passed?)

          If DoSomeStuff returns NULL on errors, additional
          exception throwing is necessary. If DoSomeStuff returns
          memory that the caller needs to release, you need to
          do so before returning from Py_DoSomeStuff.

          Regards,
          Martin

          Comment

          • Java and Swing

            #6
            Re: Call C functions from Python

            I used, myApp = CDLL("C:...") ...as I saw it in one of the ctypes
            samples.

            Anyhow, I tried...

            myApp = cdll.LoadLibrar y("C:\\myapp.dl l")
            myApp.AddNumber s(1, 4)

            ...I get an error...

            AttributeError: function 'AddNumbers' not found

            ....myapp certainly has AddNumbers.

            Grant Edwards wrote:[color=blue]
            > On 2005-10-04, Java and Swing <codecraig@gmai l.com> wrote:[color=green]
            > > ok i got ctypes...now i try
            > >[color=darkred]
            > >>> from ctypes import *
            > >>> myApp = CDLL("C:\\myapp .dll")[/color][/color]
            >
            > I've never seen that sort of usage before. I don't know what
            > CDLL does, and I can't find it in the docs anywhere.
            >
            > Based on my reading of the tutorial, I would have tried eitehr
            >
            > myApp = cdll.myapp
            > or
            > myApp = cdll.LoadLibrar y("C:/myapp.dll")
            >[color=green]
            > > ..now how can I call functions on in myapp.dll? From the
            > > tutorial I am not sure..[/color]
            >
            > Assuming CDLL did something equivalent to cdll.LoadLibrar y(),
            > I'd try:
            >
            > myApp.FuncName( )
            >
            > I've always done it the way it's done in the tutorial:
            >
            > mylib = windll.Lib_Name
            > mylib.myFuncNam e()
            >[color=green]
            > > i try, dir(cdll.myApp) and dir(myApp)..but don't see my
            > > functions listed.[/color]
            >
            > I don't think dir() works on dll's like that. I certainly
            > don't see it mentioned in the tutorial. What happened when you
            > tried calling the function the way the tutorial does?
            >
            > myapp = cdll.myapp
            > myapp.MyFunc()
            >
            > --
            > Grant Edwards grante Yow! Yow! Is my fallout
            > at shelter termite proof?
            > visi.com[/color]

            Comment

            • Fredrik Lundh

              #7
              Re: Call C functions from Python

              Java and Swing wrote:
              [color=blue]
              >I used, myApp = CDLL("C:...") ...as I saw it in one of the ctypes
              > samples.
              >
              > Anyhow, I tried...
              >
              > myApp = cdll.LoadLibrar y("C:\\myapp.dl l")
              > myApp.AddNumber s(1, 4)
              >
              > ..I get an error...
              >
              > AttributeError: function 'AddNumbers' not found
              >
              > ...myapp certainly has AddNumbers.[/color]

              properly exported? what does

              dumpbin /exports myapp.pyd

              say?

              </F>



              Comment

              • Java and Swing

                #8
                Re: Call C functions from Python

                i tried...
                [color=blue][color=green]
                >> from ctypes import *
                >> myapp = cdll.LoadLibrar y("c:\\myapp.dl l")
                >> dumpbin /exports myapp.pyd[/color][/color]

                i get, SyntaxError: invalid syntax with it pointing at the first "p" in
                myapp.pyd.

                Comment

                • Grant Edwards

                  #9
                  Re: Call C functions from Python

                  On 2005-10-05, Java and Swing <codecraig@gmai l.com> wrote:[color=blue]
                  > i tried...
                  >[color=green][color=darkred]
                  >>> from ctypes import *
                  >>> myapp = cdll.LoadLibrar y("c:\\myapp.dl l")
                  >>> dumpbin /exports myapp.pyd[/color][/color]
                  >
                  > i get, SyntaxError: invalid syntax with it pointing at the first "p" in
                  > myapp.pyd.[/color]

                  Um, just a guess, but I don't think that was python code.

                  Try it at a command prompt.

                  --
                  Grant Edwards grante Yow! The entire CHINESE
                  at WOMEN'S VOLLEYBALL TEAM all
                  visi.com share ONE personality --
                  and have since BIRTH!!

                  Comment

                  • Fredrik Lundh

                    #10
                    Re: Call C functions from Python

                    "Java and Swing" wrote:
                    [color=blue][color=green][color=darkred]
                    >>> from ctypes import *
                    >>> myapp = cdll.LoadLibrar y("c:\\myapp.dl l")
                    >>> dumpbin /exports myapp.pyd[/color][/color]
                    >
                    > i get, SyntaxError: invalid syntax with it pointing at the first "p" in
                    > myapp.pyd.[/color]

                    dumpbin is a command-line utillity, usually included in the compiler
                    toolsuite...

                    </F>



                    Comment

                    • Java and Swing

                      #11
                      Re: Call C functions from Python

                      i dont have a myapp.pyd ...i have myapp.c, or are u suggesting I dump
                      the dll? or the swig generated python file?

                      the swig generated python file only has .py and .pyc.

                      Comment

                      • Fredrik Lundh

                        #12
                        Re: Call C functions from Python

                        "Java and Swing" wrote:
                        [color=blue]
                        >i dont have a myapp.pyd ...i have myapp.c, or are u suggesting I dump
                        > the dll?[/color]

                        if that's what you're trying to import, yes.
                        [color=blue]
                        > or the swig generated python file?
                        > the swig generated python file only has .py and .pyc.[/color]

                        huh? if you have a swig-generated python file, why are you using ctypes?

                        </F>



                        Comment

                        Working...