【发布时间】:2016-07-19 11:20:54
【问题描述】:
所以,我有 C++ 类,我用 C 包装,所以我可以使用 ctypes 在 Python 中使用它。 C++类声明:
// Test.h
class Test
{
public:
static double Add(double a, double b);
};
//Test.cpp
#include "stdafx.h"
#include "Test.h"
double Test::Add(double a, double b)
{
return a + b;
}
C 换行:
// cdll.h
#ifndef WRAPDLL_EXPORTS
#define WRAPDLL_API __declspec(dllexport)
#else
#define WRAPDLL_API __declspec(dllimport)
#endif
#include "Test.h"
extern "C"
{
WRAPDLL_API struct TestC;
WRAPDLL_API TestC* newTest();
WRAPDLL_API double AddC(TestC* pc, double a, double b);
}
//cdll.cpp
#include "stdafx.h"
#include "cdll.h"
TestC* newTest()
{
return (TestC*) new Test;
}
double AddC(TestC* pc, double a, double b)
{
return ((Test*)pc)->Add(a, b);
}
Python 脚本:
import ctypes
t = ctypes.cdll('../Debug/cdll.dll')
a = t.newTest()
t.AddC(a, 2, 3)
t.AddC(a, 2, 3) 的结果总是一些负整数。 指针有问题,但我不知道是什么问题。 有没有人有任何想法?
【问题讨论】:
-
向我们展示您的完整 C 和 C++ 代码。
-
我编辑了问题,现在有完整的C和C++代码