python dll返回值是错误的

时间:2013-08-26 08:49:01

标签: python dll ctypes

我创建了一个包含代码的dll:

#pragma once

extern double __declspec(dllexport) add(double a, double b);
extern double __declspec(dllexport) dif(double a, double b);

#include "testdll.h"

double add(double a, double b)
{
    return a + b;
}

double dif(double a, double b)
{
    return a - b;
} 

调用add函数的python代码:

    a = ctypes.c_double(1);
    b = ctypes.c_double(2);
    mydll = ctypes.CDLL('testDll.dll');
    mydll.restype = ctypes.c_double;
    ret  = mydll.AddNumbers(a, b);

问题是add函数不会返回+ b但是返回值:2619340

请帮忙

2 个答案:

答案 0 :(得分:2)

返回类型为double,因此您应该像这样设置restype

hllDll.add.restype = ctypes.c_double
hllDll.add.argtypes = [ctypes.c_double, ctypes.c_double]

答案 1 :(得分:1)

a = ctypes.c_double(1);
b = ctypes.c_double(2);
mydll = ctypes.CDLL('testDll.dll');
mydll.AddNumbers.restype = ctypes.c_double;
ret  = mydll.AddNumbers(a, b);