ctypes help

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

    ctypes help

    Hy,
    I need help about use dll with ctypes. I've compiled my dll C library
    and I copied it in c:\windows\syst em32. When I load it with
    "myDLL=cdll.Loa dLibrary(find_l ibrary("myDLL.d ll"))" It seem all OK but
    python don't see the dll function. with dir(myDLL) I've only this:
    ['_FuncPtr', '__class__', '__delattr__', '__dict__', '__doc__',
    '__getattr__', '__getattribute __', '__getitem__', '__hash__',
    '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__' ,
    '__repr__', '__setattr__', '__str__', '__weakref__', '_handle',
    '_name']

    Could anybody help me

    Thanks

    Gianluca
  • sturlamolden

    #2
    Re: ctypes help

    On May 23, 12:33 am, gianluca <geonom...@gmai l.comwrote:
    Hy,
    I need help about use dll with ctypes. I've compiled my dll C library
    and I copied it in c:\windows\syst em32. When I load it with
    "myDLL=cdll.Loa dLibrary(find_l ibrary("myDLL.d ll"))" It seem all OK but
    python don't see the dll function. with dir(myDLL) I've only this:
    ['_FuncPtr', '__class__', '__delattr__', '__dict__', '__doc__',
    '__getattr__', '__getattribute __', '__getitem__', '__hash__',
    '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__' ,
    '__repr__', '__setattr__', '__str__', '__weakref__', '_handle',
    '_name']
    I see nothing wrong here.






    Comment

    • Larry Bates

      #3
      Re: ctypes help

      gianluca wrote:
      Hy,
      I need help about use dll with ctypes. I've compiled my dll C library
      and I copied it in c:\windows\syst em32. When I load it with
      "myDLL=cdll.Loa dLibrary(find_l ibrary("myDLL.d ll"))" It seem all OK but
      python don't see the dll function. with dir(myDLL) I've only this:
      ['_FuncPtr', '__class__', '__delattr__', '__dict__', '__doc__',
      '__getattr__', '__getattribute __', '__getitem__', '__hash__',
      '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__' ,
      '__repr__', '__setattr__', '__str__', '__weakref__', '_handle',
      '_name']
      >
      Could anybody help me
      >
      Thanks
      >
      Gianluca
      Since it isn't python, dir can'd do much introspection on the object to produce
      the type of output you desire. You have to know the functions (methods) that
      you want to call in your dll and call them.

      -Larry Bates

      Comment

      • gianluca

        #4
        Re: ctypes help

        On 23 Mag, 01:14, Larry Bates <larry.ba...@we bsafe.com`wrote :
        gianluca wrote:
        Hy,
        I need help about use dll with ctypes. I've compiled my dll C library
        and I copied it in c:\windows\syst em32. When I load it with
        "myDLL=cdll.Loa dLibrary(find_l ibrary("myDLL.d ll"))" It seem all OK but
        python don't see the dll function. with dir(myDLL) I've only this:
        ['_FuncPtr', '__class__', '__delattr__', '__dict__', '__doc__',
        '__getattr__', '__getattribute __', '__getitem__', '__hash__',
        '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__' ,
        '__repr__', '__setattr__', '__str__', '__weakref__', '_handle',
        '_name']
        >
        Could anybody help me
        >
        Thanks
        >
        Gianluca
        >
        Since it isn't python, dir can'd do much introspection on the object to produce
        the type of output you desire. You have to know the functions (methods) that
        you want to call in your dll and call them.
        >
        -Larry Bates

        Yes, I know it but when I load a function (a=myDLL.myFUNC T()) I've an
        exception like this:

        Traceback (most recent call last):
        File "<pyshell#1 8>", line 1, in <module>
        myDLL.myFUNCT()
        File "C:\Python25\li b\ctypes\__init __.py", line 353, in __getattr__
        func = self.__getitem_ _(name)
        File "C:\Python25\li b\ctypes\__init __.py", line 358, in __getitem__
        func = self._FuncPtr(( name_or_ordinal , self))
        AttributeError: function 'myFUNCT' not found

        Thanks for help

        Comment

        • Marc 'BlackJack' Rintsch

          #5
          Re: ctypes help

          On Thu, 22 May 2008 21:55:41 -0700, gianluca wrote:
          Yes, I know it but when I load a function (a=myDLL.myFUNC T()) I've an
          exception like this:
          >
          Traceback (most recent call last):
          File "<pyshell#1 8>", line 1, in <module>
          myDLL.myFUNCT()
          File "C:\Python25\li b\ctypes\__init __.py", line 353, in __getattr__
          func = self.__getitem_ _(name)
          File "C:\Python25\li b\ctypes\__init __.py", line 358, in __getitem__
          func = self._FuncPtr(( name_or_ordinal , self))
          AttributeError: function 'myFUNCT' not found
          Then maybe the DLL doesn't contain a function called `myFUNCT`. Any
          chance you compiled your C as C++ and name mangling kicked in?

          Can you show a minimal C source for a DLL, how you compiled it, what you
          did on the Python side to call it, and how it fails?

          Ciao,
          Marc 'BlackJack' Rintsch

          Comment

          • gianluca

            #6
            Re: ctypes help

            On 23 Mag, 07:48, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
            On Thu, 22 May 2008 21:55:41 -0700, gianluca wrote:
            Yes, I know it but when I load a function (a=myDLL.myFUNC T()) I've an
            exception like this:
            >
            Traceback (most recent call last):
            File "<pyshell#1 8>", line 1, in <module>
            myDLL.myFUNCT()
            File "C:\Python25\li b\ctypes\__init __.py", line 353, in __getattr__
            func = self.__getitem_ _(name)
            File "C:\Python25\li b\ctypes\__init __.py", line 358, in __getitem__
            func = self._FuncPtr(( name_or_ordinal , self))
            AttributeError: function 'myFUNCT' not found
            >
            Then maybe the DLL doesn't contain a function called `myFUNCT`. Any
            chance you compiled your C as C++ and name mangling kicked in?
            >
            Can you show a minimal C source for a DLL, how you compiled it, what you
            did on the Python side to call it, and how it fails?
            >
            Ciao,
            Marc 'BlackJack' Rintsch
            I've located my dll in c:\windows\syst em32 (in linux I aven't any
            problem) and I compiled it with dev-c++. The source code is C standard
            ANSII and is quite havy. If you like I can send it via mail (you can
            realy at geonomica@gmail .com) . I've tryed to build a wrape with swig
            olso with same code and I can access at all the function.
            thanks
            Gianluca

            Comment

            • Nick Craig-Wood

              #7
              Re: ctypes help

              gianluca <geonomica@gmai l.comwrote:
              On 23 Mag, 07:48, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
              On Thu, 22 May 2008 21:55:41 -0700, gianluca wrote:
              Yes, I know it but when I load a function (a=myDLL.myFUNC T()) I've an
              exception like this:
              Traceback (most recent call last):
              File "<pyshell#1 8>", line 1, in <module>
              myDLL.myFUNCT()
              File "C:\Python25\li b\ctypes\__init __.py", line 353, in __getattr__
              func = self.__getitem_ _(name)
              File "C:\Python25\li b\ctypes\__init __.py", line 358, in __getitem__
              func = self._FuncPtr(( name_or_ordinal , self))
              AttributeError: function 'myFUNCT' not found
              Then maybe the DLL doesn't contain a function called `myFUNCT`. Any
              chance you compiled your C as C++ and name mangling kicked in?

              Can you show a minimal C source for a DLL, how you compiled it, what you
              did on the Python side to call it, and how it fails?

              Ciao,
              Marc 'BlackJack' Rintsch
              >
              I've located my dll in c:\windows\syst em32 (in linux I aven't any
              problem) and I compiled it with dev-c++. The source code is C standard
              ANSII and is quite havy. If you like I can send it via mail (you can
              realy at geonomica@gmail .com) . I've tryed to build a wrape with swig
              olso with same code and I can access at all the function.
              You can use objdump from mingw or cygwin to look inside the dll and
              see exactly what it is exporting, eg

              objdump -x OurSharedCodeLi brary.dll

              This prints a great deal of stuff! In the middle you'll see

              [snip]
              The Export Tables (interpreted .rdata section contents)
              [snip]
              Ordinal/Name Pointer] Table
              [ 0] OSCL_DEBUG_fake InitComplete
              [ 1] OSCL_close
              [ 2] OSCL_displayIde nt
              [ 3] OSCL_getCurrent DynamicParams
              [ 4] OSCL_getCurrent StaticParams
              [ 5] OSCL_getErrorSt ring
              [ 6] OSCL_getIdent
              [snip]

              This is a dll we used in a project, and those names exactly worked
              with ctypes, eg some snips from the ctypes code

              self.dll = cdll.LoadLibrar y("OurSharedCod eLibrary")
              self.dll.OSCL_g etErrorString.r estype = c_char_p

              def getErrorString( self, status):
              return self.dll.OSCL_g etErrorString(c _int(status))

              --
              Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

              Comment

              • gianluca

                #8
                Re: ctypes help

                On 23 Mag, 12:30, Nick Craig-Wood <n...@craig-wood.comwrote:
                gianluca <geonom...@gmai l.comwrote:
                On 23 Mag, 07:48, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
                On Thu, 22 May 2008 21:55:41 -0700, gianluca wrote:
                Yes, I know it but when I load a function (a=myDLL.myFUNC T()) I've an
                exception like this:
                >
                Traceback (most recent call last):
                File "<pyshell#1 8>", line 1, in <module>
                myDLL.myFUNCT()
                File "C:\Python25\li b\ctypes\__init __.py", line 353, in __getattr__
                func = self.__getitem_ _(name)
                File "C:\Python25\li b\ctypes\__init __.py", line 358, in __getitem__
                func = self._FuncPtr(( name_or_ordinal , self))
                AttributeError: function 'myFUNCT' not found
                >
                Then maybe the DLL doesn't contain a function called `myFUNCT`. Any
                chance you compiled your C as C++ and name mangling kicked in?
                >
                Can you show a minimal C source for a DLL, how you compiled it, what you
                did on the Python side to call it, and how it fails?
                >
                Ciao,
                Marc 'BlackJack' Rintsch
                >
                I've located my dll in c:\windows\syst em32 (in linux I aven't any
                problem) and I compiled it with dev-c++. The source code is C standard
                ANSII and is quite havy. If you like I can send it via mail (you can
                realy at geonom...@gmail .com) . I've tryed to build a wrape with swig
                olso with same code and I can access at all the function.
                >
                You can use objdump from mingw or cygwin to look inside the dll and
                see exactly what it is exporting, eg
                >
                objdump -x OurSharedCodeLi brary.dll
                >
                This prints a great deal of stuff! In the middle you'll see
                >
                [snip]
                The Export Tables (interpreted .rdata section contents)
                [snip]
                Ordinal/Name Pointer] Table
                [ 0] OSCL_DEBUG_fake InitComplete
                [ 1] OSCL_close
                [ 2] OSCL_displayIde nt
                [ 3] OSCL_getCurrent DynamicParams
                [ 4] OSCL_getCurrent StaticParams
                [ 5] OSCL_getErrorSt ring
                [ 6] OSCL_getIdent
                [snip]
                >
                This is a dll we used in a project, and those names exactly worked
                with ctypes, eg some snips from the ctypes code
                >
                self.dll = cdll.LoadLibrar y("OurSharedCod eLibrary")
                self.dll.OSCL_g etErrorString.r estype = c_char_p
                >
                def getErrorString( self, status):
                return self.dll.OSCL_g etErrorString(c _int(status))
                >
                --
                Nick Craig-Wood <n...@craig-wood.com--http://www.craig-wood.com/nick
                Thank you for help!
                I've done all you suggest and I've a objdump output like this:
                ....
                [ 86](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00003755 _IsCoverRelD
                [ 87](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00003657 _IsCoverRelA
                [ 88](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x000034f5 _IsCoverDX
                [ 89](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x0000341b _IsCoverA
                [ 90](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00003072 _CoreRelD
                [ 91](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00002f5e _CoreRelA
                ....
                my funtions are the __IsCoverRelD, __IsCoverRelA, etc but when I call
                it (myLIB.__IsCove rRelA or myLIB.__IsCover RelA()) I've the same
                problem.
                Hy
                gianluca

                Comment

                • Terry Reedy

                  #9
                  Re: ctypes help


                  "gianluca" <geonomica@gmai l.comwrote in message
                  news:ce293364-7aef-4c43-8504-5ae353c49406@e3 9g2000hsf.googl egroups.com...
                  | Thank you for help!
                  | I've done all you suggest and I've a objdump output like this:
                  | ...
                  | [ 86](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00003755 _IsCoverRelD
                  | [ 87](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00003657 _IsCoverRelA
                  | [ 88](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x000034f5 _IsCoverDX
                  | [ 89](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x0000341b _IsCoverA
                  | [ 90](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00003072 _CoreRelD
                  | [ 91](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00002f5e _CoreRelA

                  The name above have one underscore.


                  | my funtions are the __IsCoverRelD, __IsCoverRelA, etc but when I call
                  | it (myLIB.__IsCove rRelA or myLIB.__IsCover RelA()) I've the same
                  | problem.

                  These all have two. So if that is what you actually did....

                  tjr



                  Comment

                  Working...