handing qwidget.winid to DLL in Python
<p>I have a dll, which use OpenGl to draw on window. Dll get window by HWMD.</p>

<p>DLL:</p>

<pre><code>extern "C" __declspec(dllexport) int Init(HWND hWnd);
extern "C" __declspec(dllexport) void Resize(HWND hWnd, int w, int h);
extern "C" __declspec(dllexport) void Paint(HWND hWnd);
</code></pre>

<p>The c++ qt application is work properly.</p>

<pre><code>#include "windows.h"
#include &lt;QApplication&gt;
#include &lt;QWidget&gt;
#include &lt;QGLWidget&gt;
#include &lt;QMessageBox&gt;
#include &lt;QSplitter&gt;
#include &lt;QLibrary&gt;

typedef void (*InitPrototype)(HWND);
typedef void (*PaintPrototype)(HWND);
typedef void (*ResizePrototype)(HWND, int, int);

InitPrototype c_Init;
PaintPrototype c_Paint;
ResizePrototype c_Resize;

bool load_opengl_library(){
    QLibrary lib("engine3d");
    lib.load();
    c_Init = (InitPrototype)lib.resolve("Init");
    c_Paint = (PaintPrototype)lib.resolve("Paint");
    c_Resize = (ResizePrototype)lib.resolve("Resize");
    return true;
}

class MyGlWidget: public QGLWidget {
public:
    MyGlWidget(QWidget *parent = 0): QGLWidget(parent){}
    void showEvent(QShowEvent* event){
        c_Init((HWND)(this-&gt;winId()));
    }
    void paintEvent(QPaintEvent* event){
        c_Paint((HWND)this-&gt;winId());
    }
    void resizeEvent(QResizeEvent* event){
        c_Resize((HWND)this-&gt;winId(), this-&gt;width(), this-&gt;height());
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    load_opengl_library();
    MyGlWidget w;
    w.show();
    return a.exec();
}
</code></pre>

<p>But how do the same in python? My program crushes on sending widget.winId().</p>

<pre><code># coding=utf-8
import ctypes
from PyQt5 import QtWidgets, QtOpenGL
app = QtWidgets.QApplication([])

e3d = ctypes.CDLL(r"engine3d.dll")
init = lambda hwnd: e3d.Init(hwnd)
paint = lambda hwnd: e3d.Paint(hwnd)
resize = lambda hwnd, w, h: e3d.Paint(hwnd, w, h)

class MyGLWidget(QtOpenGL.QGLWidget):
    def __init__(self):
        super().__init__()
    def showEvent(self, ev):
        init(self.winId())
    def paintEvent(self, ev):
        paint(self.winId())
    def resizeEvent(self, ev):
        resize(self.winId(), self.width(), self.height())

w = MyGLWidget()
w.show()
app.exec_()
</code></pre>

<p>print(self.winId()) is </p>

<p>It'd be easier to check having the dll, so I'll speak out the blue. In your c++ code you got this initialization code:</p>

<pre><code>bool load_opengl_library(){
    QLibrary lib("engine3d");
    lib.load();
    c_Init = (InitPrototype)lib.resolve("Init");
    c_Paint = (PaintPrototype)lib.resolve("Paint");
    c_Resize = (ResizePrototype)lib.resolve("Resize");
    return true;
}

...

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    load_opengl_library();
    MyGlWidget w;
    w.show();
    return a.exec();
}
</code></pre>

<p>Where you create your engine instance <code>lib</code>and using <code>load</code>method but in python you're not doing anything of that:</p>

<pre><code>w = MyGLWidget()
w.show()
app.exec_()
</code></pre>

<p>But before creating the <code>MyGLWidget</code> you're not initializing your engine as its c++ counterpart, question is, why not?</p>

python dll ctypes hwnd