如何使用从DLL导出的C ++类 [英] How to use a C++ class, exported from a DLL
问题描述
我有一个用c ++解析文件的逻辑。它实现为一个类似logReader。
我想要实现的是,从文件中导出这个类并在Python中使用它。
所以创建Dll我所做的是创建2个函数并从dll中导出。
它看起来像这样
#ifndef DLL_EXPORT_IMPORT
#define DLL_EXPORT_IMPORT externC__ declspec(dllexport)
#else
#define DLL_EXPORT_IMPORT externC__declspec(dllimport)
#endif
typedef mudpLog *(* fpGetMudp)();
typedef void(* fpDeleteMudp)(LogReader *& pObj);
DLL_EXPORT_IMPORT LogReader * GetLogPtr();
DLL_EXPORT_IMPORT void DeleteLogReader(LogReader *& mudpObj);
和python中我正在使用这样的东西
来自ctypes import *;
class Sample(object):
def __init __(self,object):
mudp_data =(c_ubyte * 32008)();
self._lib = CDLL(object);
self._handle = self._lib.GetLogPtr;
=> self._handle.restype = pointer(); ##在这里,我知道应该给我什么
self._handle.argtype = None;
=> self._handle.restype = pointer(); ##在这里我知道应该给我什么
print self._handle.Open(E:\\log_20180406_141933_001.txt);#
#self._handle.ReadNext( _数据); ##用类对象指针调用一个函数
lib = Sample(mudp_library.dll);
是否有规定这样做?我有很多关于c函数的文章。我还在研究,并发现了一些其他文章,如BOOST_PYTHON_MODULE& SWIG,Cython。
什么是最好的使用,除了使用任何这些包装外,还有其他任何相同的规定。
我的尝试:
通过互联网进行研究并继续
我无法准确地告诉你Python需要什么,但是我已经编写了许多接口来使用LoadLibrary调用以脚本语言使用C ++类。我所做的是将所有导出的函数包装在其中:extern C
{
// 导出的函数实现在这里
}并且每个函数前面都有一个__declspec(dllexport)标记。这对我来说非常有效。我使用宏作为导出标记。类似于:
#define DLLexport __declspec(dllexport)
所以函数如下所示:
extern C
{
DLLexport int MyFunction(...)
{
}
} // extern C
参见 1。使用C或C ++扩展Python - Python 2.7.15文档 [ ^ ]。
Hi,
I have a logic to parse a file in c++. It is implemented as a class say logReader.
What I am trying to achieve is, export this class from a file and use that in Python.
so to create the Dll what i did is created 2 functions and exported that from the dll.
it looks something like this
#ifndef DLL_EXPORT_IMPORT #define DLL_EXPORT_IMPORT extern "C" __declspec (dllexport) #else #define DLL_EXPORT_IMPORT extern "C" __declspec (dllimport) #endif typedef mudpLog*(*fpGetMudp)(); typedef void (*fpDeleteMudp)( LogReader*& pObj); DLL_EXPORT_IMPORT LogReader* GetLogPtr(); DLL_EXPORT_IMPORT void DeleteLogReader( LogReader*& mudpObj);
and in the python end i am using something like this
from ctypes import *; class Sample(object): def __init__(self, object): mudp_data = (c_ubyte*32008)(); self._lib = CDLL(object); self._handle = self._lib.GetLogPtr; => self._handle.restype = pointer(); ##Here i done knw what i should give self._handle.argtype = None; => self._handle.restype = pointer(); ##Here i done knw what i should give print self._handle.Open( "E:\\log_20180406_141933_001.txt" );# # self._handle.ReadNext(_data); ## Invoke a function with the class object pointer lib = Sample("mudp_library.dll");
Is there a provision to do this? I got so many articles mentioning about c functions. I am still researching and found some other articles like BOOST_PYTHON_MODULE & SWIG, Cython.
What will be the best to use and is there any other provision to do the same other than using any of these wrappers.
What I have tried:
Research over the internet and still continuing
I can't tell you exactly what Python needs but I have written many interfaces to use C++ classes in a script language using LoadLibrary calls. What I did was wrap all of the exported functions in this:extern "C" { // exported function implementation goes here }and each function had a "__declspec( dllexport )" tag in front of it. This has worked quite well for me. I used a macro for the export tag. Something like :
#define DLLexport __declspec(dllexport)
so the functions look like this :
extern "C" { DLLexport int MyFunction( ... ) { } } // extern C
See 1. Extending Python with C or C++ — Python 2.7.15 documentation[^].
这篇关于如何使用从DLL导出的C ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!