[Mono-dev] Embed: MainWindow

Robert Jordan robertj at gmx.net
Tue Feb 2 20:44:49 EST 2010

On 03.02.2010 00:39, Steve Baer wrote:
> Hi,
> I've been working on embedding mono in a C++ Windows application (and an OSX
> app.) My C++ code calls a C# function through mono which in turn calls
> System.Windows.Forms.MessageBox.Show("Hi from mono");
> This shows a message box, but it is not modal and my application crashes if
> I switch focus away from the message box. I think the problem is that mono
> doesn't know about the main window of my native application because I always
> get IntPtr.Zero when calling
> System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;

This property always returns zero because it's not implemented.
Under Unix/Mac OS it would be meaningless at best.

> Is there something like a function in the embedding API that let's me tell
> mono the main window handle for my application? Any clues no what to try
> next would be appreciated.

The embedding API does not cover the windowing environment at all.

Under Windows, the following might work:

1) get the value (IntPtr) of the field
    using the embedded API.

    This is the parent window handle of all SWF windows.

2) re-parent the foster window:

    SetParent(fosterParent, handleOfYourMainWindow);

    See SetParent on MSDN.

Note that you must perform these steps from the thread that has
created the message loop of your C++ app.

Another restriction: you can't call a form's Show() method.
Always use ShowDialog(), because you can't create another
message loop in the same thread.


More information about the Mono-devel-list mailing list