Phillip B Oldham
Phillip B Oldham

Reputation: 19385

How can I wrap a c++ library (.h & .lib files) using Python?

I have a c++ library containing only .h and .lib files (no cpp files) for communicating with a piece of hardware and I need to use this library from Python. I don't have much experience with c/++, so it's all a little alien to me.

The .h files look something like this:

#define MSG_DLL_VERSION 10

typedef struct {
   ULONG ulDLLVersion;
   // vipmsg variables
   PMSGACCOUNTS pMsgAccounts;
   PMSGSEGMENT  pMsgSegment;
   USHORT       usMsgSegmentNum;
} MSGSTATICDATA, *PMSGSTATICDATA;

VOID msgGetDLLRedirections ( PMSGSTATICDATA *pData ); 

VOID msgSetDLLRedirections ( PMSGSTATICDATA pData ); 

Looking around, here's what I've found:

So, what would be the best approach?

Upvotes: 3

Views: 610

Answers (1)

David Heffernan
David Heffernan

Reputation: 613332

Boost.Python also requires .cpp files

No it does not. You can wrap your library with Boost.Python. The way you expose C++ code using Boost.Python is to write a shared library using the various macros provided by Boost.Python.

The documentation for Boost.Python demonstrates wrapping this C++ type:

struct World
{
    void set(std::string msg) { this->msg = msg; }
    std::string greet() { return msg; }
    std::string msg;
};

The wrapper looks like this:

#include <boost/python.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(hello)
{
    class_<World>("World")
        .def("greet", &World::greet)
        .def("set", &World::set)
    ;
}

You can do that with your library. You do need to write the wrapper. But the fact that the class and methods being wrapped are defined in a .lib library file rather than a .cpp source file is irrelevant.


Update

Looking at the sample code from the header file this seems much more like a C style library than C++. You could certainly use Boost.Python for that. SWIG would also be an option. Or ctypes.

Upvotes: 1

Related Questions