Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 1-Jun-18 22:58pm

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:
C++
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 :
C++
#define DLLexport  __declspec(dllexport)
so the functions look like this :
C++
extern "C"
{
DLLexport int MyFunction( ... )
{
}
} // extern C
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900