Re: [ctypes-users] calling function with a string
Brought to you by:
theller
|
From: Mark T. <met...@gm...> - 2009-10-17 01:23:01
|
<jas...@ma...> wrote in message
news:8CC...@we......
> Hi Users
> Am trying out the ctypes module.
> I have a dll with a function expecting the following.
> double priceFromYield(
> double Yield, // number of percentage points
> double Coupon, // assumed face value = 100
> double compfreq, // compounding frequency of yield
> int settleDays, // number of settlement days
> string TodaysDate, // today's date - trade date
> string DatedDate, // dated date - issue date
> string FirstCpnDate, // first coupon date
> string LastCpnDate, // last coupon date prior to maturity
> string MaturityDate, // maturity date
> string Tenor, // coupon tenor: "6m", "1y"
> string DayCount, // day count conventions
> string RollConv, // roll conventions of business days
> string Calendar, // holiday calendar
> string CouponTpye // coupon type
> );
"string" is a C++ object. ctypes knows nothing about it. Also, a C++
function looks "different"...declare it as a "C" function.
If you control the source code, use "const char *" instead of "string" and
make sure to declare the function extern "C", e.g.:
extern "C" __declspec(dllexport) double priceFromYield(
double Yield, // number of percentage points
double Coupon, // assumed face value = 100
double compfreq, // compounding frequency of yield
int settleDays, // number of settlement days
const char* TodaysDate, // today's date - trade date
const char* DatedDate, // dated date - issue date
const char* FirstCpnDate, // first coupon date
const char* LastCpnDate, // last coupon date prior to
maturity
const char* MaturityDate, // maturity date
const char* Tenor, // coupon tenor: "6m", "1y"
const char* DayCount, // day count conventions
const char* RollConv, // roll conventions of business days
const char* Calendar, // holiday calendar
const char* CouponTpye // coupon type
)
{
...code...
}
The Python code needs to declare the result type and arguments:
from ctypes import *
dll=CDLL('mydll')
dll.priceFromYield.restype = c_double
dll.priceFromYield.argtypes = [
c_double,c_double,c_double,c_int,
c_char_p,c_char_p,c_char_p,c_char_p,c_char_p,
c_char_p,c_char_p,c_char_p,c_char_p,c_char_p
]
dll.priceFromYield(1,1,1,1,'a','b','c','d','e','f','g','h','i','j')
I posted this quickly...ask if anything is unclear.
-Mark
|