RegisterClass error in Unity Editor when using Mono.WinForms

3.6k Views Asked by At

I'm using Mono.WinForms to create external windows in Unity and it works great except that I get this error message each time I play the scene (but never the first time when Unity is freshly started):

Could not register the window class, win32 error 0.

I figured that it was because that the form classes are not unregistered when I stop the scene. They do get unregistered when I restart Unity and I don't have the problem when I make a build, which makes sense.

I get this popup error twice and it's caused by:

  1. myForm = new Form1();
  2. myForm.Show();

I went through the code of the XplatUIWin32.cs file and found the RegisterWindowClass method. When Mono registers a class it does this:

class_name = string
         .Format("Mono.WinForms.{0}.{1}", 
           System.Threading.Thread.GetDomainID ().ToString (), classStyle);

So I managed to get rid of the second error by doing this:

UnregisterClass("Mono.WinForms.1.0", IntPtr.Zero);

So my question is why does it only work for the 2nd popup error and how do I get rid of the 1st one? I tried to force the garbage collection but it didn't help. I tried to find another registered class name but no luck here neither.

For info I use the mono version of System.Windows.Forms.dll as a reference, compile my code in Visual Studio and import the produced DLL in Unity. It works great apart from those popup errors.

1

There are 1 best solutions below

0
skyyearxp On
class_name = string.Format ("Mono.WinForms.{0}.{1}", System.Threading.Thread.GetDomainID ().ToString (), classStyle);

Two class name should be:

  • Mono.WinForms.1.0
  • Mono.WinForms.1.40

I use dnspy patch the System.Windows.Forms.dll to see the MessageBox info with class_name shown.

I think the classStyle may not fixed, better try through different numbers.

BUT!! I still get the messagebox shown with “Mono.WinForms.1.40” info