C# Embed QEMU Into panel - how to get/generate QEMU window handle?

852 views Asked by At

I am trying to embed QEMU into Panel in .NET 4.5.

...and I found Application panel example, which is using the WinAPI SetParent function.

Great, but how to get QEMU window's handle, or generate it?

I looked at (maybe discontinued?) QEMU Manager, and I found, that the Manager is executing the QEMU emulator with -hwnd parameter, and passing number (maybe int pointer?) after it.

I tried using Process.MainWindowHandle as window handle, and waiting for it with WaitForInputHandle() function, but I got exception, since QEMU is console app...

So, final question is: How to get/generate window handle of QEMU window?

...please keep in mind, that I need multiple instances of QEMU.

Thanks for any help,
Vít "VitekST" Staniček

2

There are 2 answers

0
user5154346 On

in VB:

Private Function WaitForMainWindowHandle(proc As Process, Optional maxIntent As Integer = Integer.MaxValue) As IntPtr
    Dim hwnd As IntPtr
    Do
        hwnd = proc.MainWindowHandle
        If hwnd <> IntPtr.Zero Then Return hwnd
        Application.DoEvents() // or:
        Threading.Thread.Sleep(25)
        maxIntent -= 1
    Loop While hwnd = IntPtr.Zero AndAlso maxIntent > 0
    '**********
    Return hwnd
End Function

Use:

    Dim hwnd As IntPtr = WaitForMainWindowHandle(procQEMU, 50)
    Dim message As New StringBuilder(256)
    SendMessage(hwnd, WM_GETTEXT, message.Capacity, message)
    Console.WriteLine(message)
0
Westonsupermare On

Translation of user5154346's answer for anyone who comes accross this:

private IntPtr WaitForMainWindowHandle(Process proc, int maxIntent = int.MaxValue)
{
    IntPtr hwnd;
    do
    {
        hwnd = proc.MainWindowHandle;
        if (hwnd != IntPtr.Zero)
            return hwnd;
        System.Threading.Thread.Sleep(25);
        maxIntent--;
    } while (hwind == IntPtr.Zero && maxIntent > 0);
    return hwnd;
}

Use:

IntPtr hwnd = WaitForMainWindowHandle(procQEMU, 50);
StringBuilder message = new StringBuilder(256);
SendMessage(hwnd, WM_GETTEXT, message.Capacity, message);
Console.WriteLine(message);