python - 在 C/C++ 中创建已包装库的 Python 对象?

标签 python c windows wrapper

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

你能帮我填写/更正那个代码片段吗?

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

最佳答案

#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;
} 

关于python - 在 C/C++ 中创建已包装库的 Python 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9084759/

相关文章:

python - 我可以在模板字符串中使用切片吗?

用于重命名/移动的 Python 重构工具

c - c 中的 shell : only respond to SIGTSTP after pressing enter on keyboard

c - 在同一个 C 程序中同时使用 putchar 和 printf ?

c - 在控制台应用程序中读/写特殊字符(如波形符、ñ、...) C

python - 如何使用 Google S2 几何执行搜索操作

Python 创建供本地使用的包

windows - 替换 Windows 文件名中的 URL 字符编码(批量)

python - "ImportError: No module named serial"- 安装 pyserial 之后

c - _popen 在同时具有 stdout 和 stdin 的 Windows 中