Niklas R
Niklas R

Reputation: 16890

Create a Python object of an already wrapped library in C/C++?

Is it possible to create a Python-wrapper from a C-pointer for an already wrapped library like pywin32 or PyOpenGL in C/C++?

Can you fill-out/correct that code snippet for me?

#include <windows.h>
#include <the_magical_pywin32_header.h>
PyObject* PyObject_fromHWND(HWND window) {
    // ...
}

Upvotes: 0

Views: 180

Answers (1)

user325117
user325117

Reputation:

#include <windows.h>
#include <Python.h>
#include <pythonwin/win32win.h> // Make sure this is in the include path

static PyObject *g_pModule = NULL;

PyObject* PyObject_fromHWND(HWND window)
{
    PyObject *pName, *pArgs, *pValue;
    if (g_pModule == NULL) {
        char name[] = "pythonwin/win32gui.py";  // Replace with the full path
        pName = PyString_FromString(name);
        g_pModule = PyImport_Import(pName);
        py_DECREF(pName);
        if (g_pModule == NULL) {
            // Report an error
        }
    }
    pArgs = PyTuple_New(1);
    pValue = PyInt_FromLong(static_cast<long>(window));
    PyTuple_SetItem(pArgs, 0, pValue);
    PyObject *pWindow = PyCWnd::CreateWindowFromHandle(g_pModule, pArgs);
    Py_DECREF(pValue);
    Py_DECREF(pArgs);
    return pWindow;
} 

Upvotes: 1

Related Questions