[Dream] A meta-wrapper module to interface any C dynamic library

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Francesco Bochicchio

    #1

    [Dream] A meta-wrapper module to interface any C dynamic library

    Hi all,

    I was wondering if it would be possible to make a module which allows to
    do something like that:

    import c_interface
    math_lib = c_interface.loa d('math.so', 'math.h' )
    sin_f = math_lib.get_fu nction('sin')
    print "sin(3.14) = %f" % sin_f (3.14)

    The idea would be to make a module that allows to use any C dynamic
    library ( I used an hypothetical math.so only as example ) without
    the need of a specific wrapper module.

    I've given some thought to this idea, but I was blocked by the theoric (?)
    impossibility to correctly call a C function without knowing its prototype
    at compile time.

    Did anybody ever attempted to do something like this?

    Ciao
    -----
    FB

  • Thomas Heller

    #2
    Re: [Dream] A meta-wrapper module to interface any C dynamiclibrary

    "Francesco Bochicchio" <bockman@virgil io.it> writes:
    [color=blue]
    > Hi all,
    >
    > I was wondering if it would be possible to make a module which allows to
    > do something like that:
    >
    > import c_interface
    > math_lib = c_interface.loa d('math.so', 'math.h' )
    > sin_f = math_lib.get_fu nction('sin')
    > print "sin(3.14) = %f" % sin_f (3.14)
    >
    > The idea would be to make a module that allows to use any C dynamic
    > library ( I used an hypothetical math.so only as example ) without
    > the need of a specific wrapper module.
    >
    > I've given some thought to this idea, but I was blocked by the theoric (?)
    > impossibility to correctly call a C function without knowing its prototype
    > at compile time.
    >
    > Did anybody ever attempted to do something like this?[/color]

    Yes, it already exists (to some degree).



    Thomas

    Comment

    • Simon Burton

      #3
      Re: [Dream] A meta-wrapper module to interface any C dynamic library

      On Fri, 04 Jul 2003 10:59:39 +0000, Francesco Bochicchio wrote:

      [color=blue]
      > Did anybody ever attempted to do something like this?
      >
      > Ciao
      > -----
      > FB[/color]

      yeah,



      it's hard :)

      Simon.

      Comment

      • Seo Sanghyeon

        #4
        Re: [Dream] A meta-wrapper module to interface any C dynamic library

        Use ctypes.



        To see is to believe. The following is an actual IDLE session on WinXP.
        However, ctypes is cross-platform -- it rusn on Windows, Linux, MacOS X.
        [color=blue][color=green][color=darkred]
        >>> import ctypes
        >>> loader = ctypes.cdll
        >>> dll = loader.msvcrt
        >>> sin = dll.sin
        >>> sin.argtypes = [ctypes.c_double]
        >>> sin.restype = ctypes.c_double
        >>> sin(3.14)[/color][/color][/color]
        0.0015926529164 868282[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Comment

        • Francesco Bochicchio

          #5
          Re: [Dream] A meta-wrapper module to interface any C dynamic library

          On Fri, 04 Jul 2003 13:33:27 +0200, Thomas Heller wrote:
          [color=blue]
          >
          > Yes, it already exists (to some degree).
          >
          > http://starship.python.net/crew/theller/ctypes/
          >
          > Thomas[/color]

          Thanks for the pointer. Is more or less what I had in mind, and lead me to
          find out about libffi, which is also quite interesting.

          This proves a theory of mine: whatever piece of software you can think of,
          there is always some developer in the world that already did it :-)

          Ciao
          -----
          FB

          Comment

          • Thomas Heller

            #6
            Re: [Dream] A meta-wrapper module to interface any C dynamiclibrary

            "Simon Burton" <simonb@webone. com.au> writes:
            [color=blue]
            > On Fri, 04 Jul 2003 10:59:39 +0000, Francesco Bochicchio wrote:
            >
            >[color=green]
            >> Did anybody ever attempted to do something like this?
            >>
            >> Ciao
            >> -----
            >> FB[/color]
            >
            > yeah,
            >
            > http://arrowtheory.com/software/python/index.html
            >
            > it's hard :)
            >[/color]
            I have tried cdecl.py to parse windows header files, and it does a
            pretty good job.

            I say this after also having tried PLY
            http://systems.cs.uchicago.edu/ply/ with a partial C grammer, and
            finally gave up because I'm not sure it is possible to parse ANSI C with
            a parser like this (at least its not possible for me).

            I found one problem in cdecl.py (it doesn't parse hex constants
            correctly), and I had to extend it somewhat for MS specific keywords
            like __stdcall or so. I also ran the header files through the MSVC
            preprocessor, and hand-edit them somewhat before throwing them at it.

            I'm not really sure if it's a good idea to automatically create ctypes
            wrappers from windows.h...

            But I finally gave up when I encountered some really strange errors...

            Thomas

            Comment

            • Thomas Heller

              #7
              Re: [Dream] A meta-wrapper module to interface any C dynamiclibrary

              hwlgw@hotmail.c om (Will Stuyvesant) writes:
              [color=blue][color=green]
              >> [Seo Sanghyeon]
              >> To see is to believe.[color=darkred]
              >> >>> import ctypes
              >> >>> loader = ctypes.cdll
              >> >>> dll = loader.msvcrt
              >> >>> sin = dll.sin
              >> >>> sin.argtypes = [ctypes.c_double]
              >> >>> sin.restype = ctypes.c_double
              >> >>> sin(3.14)[/color]
              >> 0.0015926529164 868282[/color]
              >
              > I love examples like this! And ctypes is very good, I downloaded the
              > .EXE installer and ran it and this example works "out of the box".
              >
              > How do you know what functions are available in "dll"? Any general
              > strategy?[/color]

              On windows, you use dependency walker, although it would also be
              possible to write something similar in ctypes ;-)

              depends,depends.exe,dependency walker,dependency,walker,dependencies,DLL,EXE,OCX,SYS,import,export,dynamic,implicit,explicit,function,GetProcAddress,LoadLibrary,DllMain,file,found,missing,unresolved,external,version,hook,dumpbin,quickview,quikview,full,path,module,library,procedure,entry,point,windows,win32,utility,tool,application,COM,OLE,VB,Visual,Basic,C++,mangled,decorated,PE,executable,link,compile,initialize,checksum,error,failure,show,reveal


              On unix like systems, I don't know. Use nm(1) ?

              Thomas

              Comment

              Working...