【问题标题】:Create a Python object of an already wrapped library in C/C++?在 C/C++ 中创建一个已经包装好的库的 Python 对象?
【发布时间】:2012-01-31 18:16:49
【问题描述】:

是否可以从 C 指针为已包装的库(如 C/C++ 中的 pywin32PyOpenGL)创建 Python 包装器?

您能为我填写/更正该代码 sn-p 吗?

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

【问题讨论】:

    标签: python c windows wrapper


    【解决方案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;
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      • 2020-06-01
      相关资源
      最近更新 更多