Driven form as a child of unmanaged HWND

I need to show my System.Windows.Forms.Form as a child window of an unmanaged C ++ HWND. This is the C # SDK code that retrieves the NativeWindow:

public static NativeWindow MainWindow()
{
  Diagnostics.Process process = Diagnostics.Process.GetCurrentProcess();
  if (null == process)
    return null;
  IntPtr handle = process.MainWindowHandle;
  if (IntPtr.Zero == handle)
    return null;

  NativeWindow wnd = new NativeWindow();
  wnd.AssignHandle(handle);

  return wnd;
}

      

This is how it is implemented in the plugin:

IWin32Window rh_wnd = Rhino.RhinoApp.MainWindow();
DocEditor.Show(rh_wnd);

      

It works ... most of the time. But it also often fails the first time this code is called:

HWND error http://www.freeimagehosting.net/uploads/f29bc27823.png

Call again, everything works fine. What's happening?!?

+2


a source to share


1 answer


Perhaps because rh_wnd is NULL? There are at least 2 cases where you are returning null from MainWindow (). It might be a good idea to check

IWin32Window rh_wnd = Rhino.RhinoApp.MainWindow();
if ( rh_wnd != null )
   DocEditor.Show(rh_wnd);

      



And if the above stops errors, you can check which of the above conditions is returning null, and from there.

Hope it helps.

+2


a source







All Articles