C pointers from python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itajit
    New Member
    • Feb 2007
    • 8

    C pointers from python

    Hi, How to call pointer variable of c++ in python or how to call referance variables?
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by itajit
    Hi, How to call pointer variable of c++ in python or how to call referance variables?
    Are you using swig or the ctype module? The ctypes module is so much easier (that's why I ask).

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by itajit
      Hi, How to call pointer variable of c++ in python or how to call referance variables?
      If you use better titles, then, even if you leave the specifics out of the question, we'll know better how to help you. And I wont have to change them later.

      Comment

      • dshimer
        Recognized Expert New Member
        • Dec 2006
        • 136

        #4
        I'm not clear on what you need to do or what modules you are using but let me post a couple of lines that I used this week while working with an external DLL that needed C pointers. I only barely understand it at this point because it is new to me, from a friend, and I haven't worked in C in 10 years, but it worked and may lead you in the right direction.

        The DLL expected C pointers for input and output to it's functions.
        from ctypes import *
        Because he's using ctypes.

        ExternalLib = windll.LoadLibr ary("LibName.dl l")
        To give me access to the DLL functions.

        tmpStr = create_string_b uffer(256)
        I'm assuming this is a ctypes function that creates a C string pointer.

        inx = c_double(1000.0 0)
        This DLL works with coordinates, I'm going to pass a double pointer into one of the functions so must declare it as such.

        ExternalLib.SetXIn(inx)
        SetXIn() is one of the libraries functions so I'm passing in the c double pointer which I set above.

        ExternalLib.GetXOut.restyp e = c_double
        As I understand this (which I really don't) The GetXOut will be returning a pointer to a C double so I have to declare it as such.

        outx = ExternalLib.GetXOut()
        Then make the call which now give me a regular double stored in python.

        I also read a bit of the python Ctypes Tutorial, though only enough to get a basic understanding for now (which is probably obvious).

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by dshimer
          I'm not clear on what you need to do or what modules you are using but let me post a couple of lines that I used this week while working with an external DLL that needed C pointers. I only barely understand it at this point because it is new to me, from a friend, and I haven't worked in C in 10 years, but it worked and may lead you in the right direction.

          The DLL expected C pointers for input and output to it's functions.
          from ctypes import *
          Because he's using ctypes.

          ExternalLib = windll.LoadLibr ary("LibName.dl l")
          To give me access to the DLL functions.

          tmpStr = create_string_b uffer(256)
          I'm assuming this is a ctypes function that creates a C string pointer.

          inx = c_double(1000.0 0)
          This DLL works with coordinates, I'm going to pass a double pointer into one of the functions so must declare it as such.

          ExternalLib.SetXIn(inx)
          SetXIn() is one of the libraries functions so I'm passing in the c double pointer which I set above.

          ExternalLib.GetXOut.restyp e = c_double
          As I understand this (which I really don't) The GetXOut will be returning a pointer to a C double so I have to declare it as such.

          outx = ExternalLib.GetXOut()
          Then make the call which now give me a regular double stored in python.

          I also read a bit of the python Ctypes Tutorial, though only enough to get a basic understanding for now (which is probably obvious).
          Awesome, D. I'll move a copy of your post to the new Articles sub section.

          Comment

          Working...