从Python调用c ++ dll [英] c++ dll called from Python

查看:226
本文介绍了从Python调用c ++ dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用一个C ++ dll使用MFC,我想从python调用它。
此dll在.h文件中包含此标题


LONG CommOpen(BYTE port,LONG baud_rate);


然后我在自由软件dllexp中看到我的函数被调用了?CommOpen @ CFIPcmd @@ QAEJEJ @ Z在二进制文件中,所以没有报告错误当我在python做的时候


import ctypes



lib = ctypes.WinDLL 'C:\Users\toto\FIProtocol.dll')



prototype = WINFUNCTYPE(c_long,c_byte,c_long)



testPt = ctypes.WINFUNCTYPE(prototype)



testApi = testPt(?CommOpen @ CFIPcmd @@ QAEJEJ @ Z,lib) / p>

直到它似乎工作,但是我想在Python中调用


Long l = CommOpen(5,115200);


没有找到知道如何继续。

解决方案

给出问题中提供的信息,解决方案是: / p>

  import ctypes 

lib = ctypes.CDLL(r'C:\Users\toto\\ \\ FIProtocol.dll')
CommOpen = getattr(lib,?CommOpen @ CFIPcmd @@ QAEJEJ @ Z)
CommOpen.argtypes = [c_byte,c_long]
CommOpen.restype = c_long

现在可以调用:

  l = CommOpen(5,115200)


  1. 使用 CDLL 而不是 WinDLL 因为该函数使用默认的 cdecl 调用约定。

  2. 使用 getattr 必须指定才能指定该名称。







一个更大的问题。上面写的是基于你的函数是一个非成员函数。这是一个合理的假设,因为 ctypes 要求函数为非成员或静态。



当我把你的托管功能名称放在一个demanger中(例如 http://pear.warosu.org/ c ++ filtjs / )看起来函数其实是:

  public:long __thiscall CFIPcmd :: CommOpen(unsigned char,long)

这是一个C ++对象的成员函数。无法从 ctypes 访问。您将需要创建一个简单的C样式包装器,或者找到一种不同的interop方法。


I must used a C++ dll using MFC and I would like to call it from python. This dll contains this header in the .h file

LONG CommOpen(BYTE port, LONG baud_rate);

Then I see in the free software dllexp that my function is called ?CommOpen@CFIPcmd@@QAEJEJ@Z in the binary file so no error is reported when I do in python

import ctypes

lib = ctypes.WinDLL('C:\Users\toto\FIProtocol.dll')

prototype = WINFUNCTYPE(c_long, c_byte, c_long)

testPt = ctypes.WINFUNCTYPE (prototype)

testApi = testPt (("?CommOpen@CFIPcmd@@QAEJEJ@Z", lib))

Until there it seems to work but then I would like to call the in Python the equivalent in C++ of

Long l= CommOpen(5 ,115200);

But I Didn't find know how to proceed. Any help would be really appreciated!!

解决方案

Given the information presented in the question, the solution is:

import ctypes

lib = ctypes.CDLL(r'C:\Users\toto\FIProtocol.dll')
CommOpen = getattr(lib, "?CommOpen@CFIPcmd@@QAEJEJ@Z")
CommOpen.argtypes = [c_byte, c_long]
CommOpen.restype = c_long

And now it is ready to call:

l = CommOpen(5 ,115200)

Some notes:

  1. Use CDLL rather than WinDLL because the function used the default cdecl calling convention.
  2. Use getattr to be able to specify the mangled name.
  3. It always pays to specify argtypes and restype explicitly.


However, it transpires that you have a much greater problem. The above was written on the basis that your function is a non-member function. Which is a reasonable assumption given that ctypes requires functions to be either non-member, or static.

However, when I put your managed function name into a demanger (for instance http://pear.warosu.org/c++filtjs/) it seems that the function is in fact:

public: long __thiscall CFIPcmd::CommOpen(unsigned char,long)

That is a member function of a C++ object. That cannot be accessed from ctypes. You'll need to create a plain C style wrapper, or find a different method of interop.

这篇关于从Python调用c ++ dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆