How can I change WM_CLASS for a WinForms application on Linux?

.net, unity-container, winforms, x11

Solution

There seems to full sample project here on bitbucket.org/hindlemail/settingwmclass:

Example project showing how to set the WM_CLASS X11 property for a mono winform application running on Linux. This makes mono winforms applications behave better with the unity + gnome3 window managers.

    // Managed struct of XSetClassHint classHint.
    public struct XClassHint
    {
        public IntPtr res_name; 
        public IntPtr res_class;
    }       


    [DllImport ("libX11", EntryPoint="XSetClassHint", CharSet=CharSet.Ansi)]
    public extern static int XSetClassHint(IntPtr display, IntPtr window, IntPtr classHint);


    public static void SetWmClass(string name, string @class, IntPtr handle)
    {           
        var a = new NativeX11Methods.XClassHint { 
                        res_name = Marshal.StringToCoTaskMemAnsi(name), 
                        res_class = Marshal.StringToCoTaskMemAnsi(@class) 
                    };
        IntPtr classHints = Marshal.AllocCoTaskMem(Marshal.SizeOf(a));
        Marshal.StructureToPtr(a, classHints, true);
          NativeX11Methods.XSetClassHint(NativeReplacements.MonoGetDisplayHandle(),     NativeReplacements.MonoGetX11Window(handle), classHints);           

        Marshal.FreeCoTaskMem(a.res_name);
        Marshal.FreeCoTaskMem(a.res_class);

        Marshal.FreeCoTaskMem(classHints);
    }   

The above page has a download link to the source code:

Problem

I've got a cross-platform .NET app that is using WinForms. For better compatibility with Unity I'd like to set the `WM_CLASS` property of a WinForms window. Is this possible?

Original source