very simple shared dll with ctypes

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

    #1

    very simple shared dll with ctypes

    I have created a simple windows small_dll.dll exporting a function that does
    a computation using C and C++ classes which i wish to call from Python. I
    have read lots of ctypes documentation but I still dont quite understand how
    to call the function.

    As a test, I have written a much simpler mathematical function below to try
    to get it to work from Python.

    I am trying to call this from python by:
    >>from ctypes import *
    >>print cdll.small_dll. adder(c_double( 5.343534),c_dou ble(3.4432))
    >>2223968
    Can someone give me a few tips to get going!
    The DLL declaration is below.



    #include<window s.h>

    #if defined(_MSC_VE R)

    #define DLL extern "C" __declspec(dlle xport)

    #else

    #define DLL

    #endif


    DLL double adder(double a, double b){

    double c;

    c=a+b;

    return c;

    }




  • Thomas Heller

    #2
    Re: very simple shared dll with ctypes

    mclaugb schrieb:
    I have created a simple windows small_dll.dll exporting a function that does
    a computation using C and C++ classes which i wish to call from Python. I
    have read lots of ctypes documentation but I still dont quite understand how
    to call the function.
    >
    As a test, I have written a much simpler mathematical function below to try
    to get it to work from Python.
    >
    I am trying to call this from python by:
    >
    >>>from ctypes import *
    >>>print cdll.small_dll. adder(c_double( 5.343534),c_dou ble(3.4432))
    >>>2223968
    >
    Can someone give me a few tips to get going!
    You should assign the .restype and the .argtypes attributes to your function.
    That gives the the proper result, and you don't have to pack the arguments into
    c_double yourself.
    The DLL declaration is below.
    >
    #include<window s.h>
    #if defined(_MSC_VE R)
    #define DLL extern "C" __declspec(dlle xport)
    #else
    #define DLL
    #endif
    >
    DLL double adder(double a, double b){
    double c;
    c=a+b;
    return c;
    }
    >
    from ctypes import *
    adder = cdll.small_dll. adder
    adder.restype = c_double
    adder.argtypes = c_double, c_double

    print adder(5.343534, 3.4432)


    Thomas

    Comment

    • mclaugb

      #3
      Re: very simple shared dll with ctypes

      Assigining restype and argtypes works!

      "Thomas Heller" <theller@ctypes .orgwrote in message
      news:mailman.92 28.1182256599.3 2031.python-list@python.org ...
      mclaugb schrieb:
      >I have created a simple windows small_dll.dll exporting a function that
      >does
      >a computation using C and C++ classes which i wish to call from Python.
      >I
      >have read lots of ctypes documentation but I still dont quite understand
      >how
      >to call the function.
      >>
      >As a test, I have written a much simpler mathematical function below to
      >try
      >to get it to work from Python.
      >>
      >I am trying to call this from python by:
      >>
      >>>>from ctypes import *
      >>>>print cdll.small_dll. adder(c_double( 5.343534),c_dou ble(3.4432))
      >>>>2223968
      >>
      >Can someone give me a few tips to get going!
      >
      You should assign the .restype and the .argtypes attributes to your
      function.
      That gives the the proper result, and you don't have to pack the arguments
      into
      c_double yourself.
      >
      >The DLL declaration is below.
      >>
      >#include<windo ws.h>
      >#if defined(_MSC_VE R)
      >#define DLL extern "C" __declspec(dlle xport)
      >#else
      >#define DLL
      >#endif
      >>
      >DLL double adder(double a, double b){
      >double c;
      >c=a+b;
      >return c;
      >}
      >>
      >
      from ctypes import *
      adder = cdll.small_dll. adder
      adder.restype = c_double
      adder.argtypes = c_double, c_double
      >
      print adder(5.343534, 3.4432)
      >
      >
      Thomas
      >

      Comment

      Working...