2012-01-31 53 views
1

是否有可能通过C指针创建一个Python包装器,用于C/C++中已包装的库,如pywin32PyOpenGL在C/C++中创建已包装库的Python对象?

您可以填写/更正我的代码片段吗?

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

回答

1
#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; 
} 
+0

谢谢。 :)这工作正常。 – 2012-02-09 18:11:04