Embed the main window of a qt/C++ application in a wpf/mvvm application


I need to embed a qt/c++ application in a wpf mvvm application. The window of this qt/C++ has to be integrated in a wpf page displayed in a tab page.

The page Control is the following :

<page x:Class="Wpf_HostExe.Page1"
        <Border x:Name="HostUi" Background="White"
            VerticalAlignment="Stretch"  />

I start the app to be hosted with the instructions below :

public static Process StartProcess()
    System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo();
    processStartInfo.WorkingDirectory = "C:\\ChildApp\\";
    processStartInfo.fileName = "ChildApp.exe";
    processStartInfo.Arguments = "";

    System.Diagnostics.Process proc = System.Diagnostics.Process.Start(processStartInfo);

    return proc;

The external window is reparented by the following instructions :

public class ApplicationHost : HwndHost
    private const uint LBS_NOTIFY = 0x00000001;
    private const uint WS_BORDER = 0x00800000;
    private const int SWP_NO_ACTIVATE = 0x0010;
    private const int GWL_STYLE = -16;
    private const int WS_CAPTION = 0x00C00000;
    private const int WS_THICKFRAME = 0x00040000;
    private const uint WS_VISIBLE = 0x10000000;

    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll"), SetLastError = true]
    private static extern int GetWindowLong(IntPtr hwnd, int nIndex);

    private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);

    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

    public IntPtr ApplicationHandle { get; set; }
    protected override HandleRef BuildWindowCore (HandleRef hwndParent)
        var result = new HandleRef(this, ApplicationHandle);
        if (ApplicationHandle.ToInt32() == 0)
            return result;

        var oldParent = SetParent(ApplicationHandle, hwndParent.Handle);

        var styles = GetWindowLong(ApplicationHandle, GWL_STYLE);
        styles &= ~WS_CAPTION;
        SetWindowLong32(ApplicationHandle, GWL_STYLE, styles);

        return result;

I also tried the following version :

    protected override HandleRef BuildWindowCore (HandleRef hwndParent)
        SetParent(ApplicationHandle, hwndParent.Handle);

        int style = GetWindowLong(ApplicationHandle, GWL_STYLE);
        style = style & ~WS_CAPTION & ~WS_THICKFRAME;
        SetWindowLong(ApplicationHandle, GWL_STYLE, style);

The process of the app to be hosted start without problem but the SetParent doesn't seem to work and I get the following message while the method BuildWindowCore has been passed whatever the way the setParent is done :

"An unhandled exception of type 'System.InvalidOperationException' occured in PresentationFramework.dll"
Additional Information : Hosted HWND must be a child window of the specified parent. "

I tried a lot of things found on stackoverflow but I still have this same InvalidOperationException thrown and have no clue on how to fix that.

Could you please help me ?


asked on Stack Overflow Feb 13, 2020 by Fabien FORT • edited Feb 13, 2020 by Fabien FORT

1 Answer


I presume that you have the source to the Qt application. In that code, you must create a function that will render() the widget on a QImage. Call that function from WPF, and copy the image from QImage's data() to a WriteableBitmap, and show that using a control. You can also forward the mouse and keyboard clicks in the opposite direction, and have a function that then synthesizes the QEvents to interact with the widget.

answered on Stack Overflow Feb 14, 2020 by Reinstate Monica

User contributions licensed under CC BY-SA 3.0