mmmaaaggg 2016-09-23 03:36 采纳率: 0%
浏览 1211
已结题

SWIG 3.0+Python 2.7 报错:TypeError in method ... 求指点

最近需要用Python调用一个C++写的DLL接口,总是出问题于是做了一个小测试,发现有一个类初始化始终报错。
Python调用Dll接口中一个CreateEsunnyQuotClient 初始化方法 传递参数 IEsunnyQuotNotify * 的时候,出现错误,看起来是要传递一个指针变量,但是Python没有指针啊?直接使用ctypes 应该也不对吧,实在不解了。
TypeError: in method 'CreateEsunnyQuotClient', argument 1 of type 'IEsunnyQuotNotify * '

具体错误信息如图:
图片说明

下面是我做的一些测试用的文件:头文件 example.h、C++文件 example.cpp、SWIG接口文件 example.i、distutils安装文件 setup.py、测试文件 TestExample.py、
我用的环境是Python 2.7 32位、VS2015(2010也可以)
希望高人指点,已经忙了3天3夜了还是没有结果。十分感谢。


/* example.h */

 class IEsunnyQuotNotify
{
public:
    /**
    *登录反馈回调函数
    * @param err 错误号 0表示登录成功,否则失败
    * @param errtext 错误信息
    * @return 0表示成功,否则失败
    */
    virtual int OnRspLogin(int err,const char *errtext)=0;
};

//创建接口指针(使用前调用)
IEsunnyQuotClient * CreateEsunnyQuotClient(IEsunnyQuotNotify * notify);

//释放接口指针(使用后调用)
void DelEsunnyQuotClient(IEsunnyQuotClient * client);

/* example.cpp */

 int IEsunnyQuotClient::Connect(const char *ip, int port) {
    return 0;
}

void IEsunnyQuotClient::DisConnect() {
    return;
}

class EsunnyQuotClient : public IEsunnyQuotClient {
    int Connect(const char *ip, int port) {
        return 0;
    };
    void DisConnect() {
        return ;
    };
};

int IEsunnyQuotNotify::OnRspLogin(int err, const char *errtext) {
    return 0;
}

IEsunnyQuotClient * CreateEsunnyQuotClient(IEsunnyQuotNotify * notify) {
    return &EsunnyQuotClient();
}

/* example.i*/

 /* File : example.i */
%module example

%{
#include "example.h"
%}

class Shape {
public:
  Shape() {
    nshapes++;
  }
  virtual ~Shape() {
    nshapes--;
  }
  double  x, y;
  void    move(double dx, double dy);
  virtual double area() = 0;
  virtual double perimeter() = 0;
  static  int nshapes;
};

class Circle : public Shape {
private:
  double radius;
public:
  Circle(double r) : radius(r) { }
  virtual double area();
  virtual double perimeter();
};

class Square : public Shape {
private:
  double width;
public:
  Square(double w) : width(w) { }
  virtual double area();
  virtual double perimeter();
};

class IEsunnyQuotClient
{
public:
    /**
    *连接行情数据服务器IP及端口
    * @param ip 服务器的IP
    * @param port 服务器的端口
    * @return 0表示连接成功,否则失败
    */
    virtual int Connect(const char *ip,int port)=0;

    /**
    *断开当前连接
    */
    virtual void DisConnect()=0;
};

class IEsunnyQuotNotify
{
public:
    /**
    *登录反馈回调函数
    * @param err 错误号 0表示登录成功,否则失败
    * @param errtext 错误信息
    * @return 0表示成功,否则失败
    */
    virtual int OnRspLogin(int err,const char *errtext)=0;
};

//创建接口指针(使用前调用)
IEsunnyQuotClient * CreateEsunnyQuotClient(IEsunnyQuotNotify * notify);


/* setup.py */

from distutils.core import setup, Extension


example_module = Extension('_example',
                           libraries = ['example'],
                           sources=['example_wrap.cxx'],
                           )

setup (name = 'example',
       version = '0.1',
       author      = "SWIG Docs",
       description = """example wrap with swig""",
       ext_modules = [example_module],
       py_modules = ["example"],
       )

/* TestExample.py */

 #from EsunnyQuot import IEsunnyQuotClient, IEsunnyQuotNotify, CreateEsunnyQuotClient, DelEsunnyQuotClient
from example import IEsunnyQuotClient, IEsunnyQuotNotify, CreateEsunnyQuotClient

class EsunnyQuotNoify(IEsunnyQuotNotify):

    def __init__(self, *args, **kwargs):
        pass


class EsunnyQuotClient(IEsunnyQuotClient):

    def __init__(self):
        pass

client = None
try:
    notify = EsunnyQuotNoify() 
    print type(notify)
    client = CreateEsunnyQuotClient(notify)
finally:
    if client is not None:
        #DelEsunnyQuotClient(client)
        pass
  • 写回答

2条回答

  • hijack00 2016-09-23 03:54
    关注

    python调用Windows下的DLL文件标准做法就是使用ctypes库,功能很强大。主要是注意一下DLL的类型(cdll,windll,oledll)以及python类型与c类型的对应关系。函数传参时如果形参为指针,则可以使用byref获取某个变量的指针

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧