-1

我制作了一个包含代码的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 函数不返回 a+b 而是一个搞砸的值:2619340

请帮忙

4

2 回答 2

3

返回类型是double,所以你应该restype这样设置:

hllDll.add.restype = ctypes.c_double
hllDll.add.argtypes = [ctypes.c_double, ctypes.c_double]
于 2013-08-26T08:52:50.153 回答
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);
于 2019-12-12T18:32:12.187 回答