private void RegisterClass()
 {
     string className;
     System.Windows.Forms.NativeMethods.WNDCLASS_D wc = new System.Windows.Forms.NativeMethods.WNDCLASS_D();
     if (NativeWindow.userDefWindowProc == IntPtr.Zero)
     {
         string lpProcName = (Marshal.SystemDefaultCharSize == 1) ? "DefWindowProcA" : "DefWindowProcW";
         NativeWindow.userDefWindowProc = System.Windows.Forms.UnsafeNativeMethods.GetProcAddress(new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle("user32.dll")), lpProcName);
         if (NativeWindow.userDefWindowProc == IntPtr.Zero)
         {
             throw new Win32Exception();
         }
     }
     if (this.className == null)
     {
         wc.hbrBackground = System.Windows.Forms.UnsafeNativeMethods.GetStockObject(5);
         wc.style = this.classStyle;
         this.defWindowProc = NativeWindow.userDefWindowProc;
         className = "Window." + Convert.ToString(this.classStyle, 0x10);
         this.hashCode = 0;
     }
     else
     {
         System.Windows.Forms.NativeMethods.WNDCLASS_I wndclass_i = new System.Windows.Forms.NativeMethods.WNDCLASS_I();
         bool flag = System.Windows.Forms.UnsafeNativeMethods.GetClassInfo(System.Windows.Forms.NativeMethods.NullHandleRef, this.className, wndclass_i);
         int error = Marshal.GetLastWin32Error();
         if (!flag)
         {
             throw new Win32Exception(error, System.Windows.Forms.SR.GetString("InvalidWndClsName"));
         }
         wc.style = wndclass_i.style;
         wc.cbClsExtra = wndclass_i.cbClsExtra;
         wc.cbWndExtra = wndclass_i.cbWndExtra;
         wc.hIcon = wndclass_i.hIcon;
         wc.hCursor = wndclass_i.hCursor;
         wc.hbrBackground = wndclass_i.hbrBackground;
         wc.lpszMenuName = Marshal.PtrToStringAuto(wndclass_i.lpszMenuName);
         className = this.className;
         this.defWindowProc = wndclass_i.lpfnWndProc;
         this.hashCode = this.className.GetHashCode();
     }
     this.windowClassName = this.GetFullClassName(className);
     this.windowProc = new System.Windows.Forms.NativeMethods.WndProc(this.Callback);
     wc.lpfnWndProc = this.windowProc;
     wc.hInstance = System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null);
     wc.lpszClassName = this.windowClassName;
     short num2 = System.Windows.Forms.UnsafeNativeMethods.RegisterClass(wc);
     if (num2 == 0)
     {
         int num3 = Marshal.GetLastWin32Error();
         if (num3 == 0x582)
         {
             System.Windows.Forms.NativeMethods.WNDCLASS_I wndclass_i2 = new System.Windows.Forms.NativeMethods.WNDCLASS_I();
             if (System.Windows.Forms.UnsafeNativeMethods.GetClassInfo(new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null)), this.windowClassName, wndclass_i2) && (wndclass_i2.lpfnWndProc == NativeWindow.UserDefindowProc))
             {
                 if (System.Windows.Forms.UnsafeNativeMethods.UnregisterClass(this.windowClassName, new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null))))
                 {
                     num2 = System.Windows.Forms.UnsafeNativeMethods.RegisterClass(wc);
                 }
                 else
                 {
                     do
                     {
                         domainQualifier++;
                         this.windowClassName = this.GetFullClassName(className);
                         wc.lpszClassName = this.windowClassName;
                         num2 = System.Windows.Forms.UnsafeNativeMethods.RegisterClass(wc);
                     }
                     while ((num2 == 0) && (Marshal.GetLastWin32Error() == 0x582));
                 }
             }
         }
         if (num2 == 0)
         {
             this.windowProc = null;
             throw new Win32Exception(num3);
         }
     }
     this.registered = true;
 }
Example #2
0
            private void RegisterClass()
            {
                string className;

                System.Windows.Forms.NativeMethods.WNDCLASS_D wc = new System.Windows.Forms.NativeMethods.WNDCLASS_D();
                if (NativeWindow.userDefWindowProc == IntPtr.Zero)
                {
                    string lpProcName = (Marshal.SystemDefaultCharSize == 1) ? "DefWindowProcA" : "DefWindowProcW";
                    NativeWindow.userDefWindowProc = System.Windows.Forms.UnsafeNativeMethods.GetProcAddress(new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle("user32.dll")), lpProcName);
                    if (NativeWindow.userDefWindowProc == IntPtr.Zero)
                    {
                        throw new Win32Exception();
                    }
                }
                if (this.className == null)
                {
                    wc.hbrBackground   = System.Windows.Forms.UnsafeNativeMethods.GetStockObject(5);
                    wc.style           = this.classStyle;
                    this.defWindowProc = NativeWindow.userDefWindowProc;
                    className          = "Window." + Convert.ToString(this.classStyle, 0x10);
                    this.hashCode      = 0;
                }
                else
                {
                    System.Windows.Forms.NativeMethods.WNDCLASS_I wndclass_i = new System.Windows.Forms.NativeMethods.WNDCLASS_I();
                    bool flag  = System.Windows.Forms.UnsafeNativeMethods.GetClassInfo(System.Windows.Forms.NativeMethods.NullHandleRef, this.className, wndclass_i);
                    int  error = Marshal.GetLastWin32Error();
                    if (!flag)
                    {
                        throw new Win32Exception(error, System.Windows.Forms.SR.GetString("InvalidWndClsName"));
                    }
                    wc.style           = wndclass_i.style;
                    wc.cbClsExtra      = wndclass_i.cbClsExtra;
                    wc.cbWndExtra      = wndclass_i.cbWndExtra;
                    wc.hIcon           = wndclass_i.hIcon;
                    wc.hCursor         = wndclass_i.hCursor;
                    wc.hbrBackground   = wndclass_i.hbrBackground;
                    wc.lpszMenuName    = Marshal.PtrToStringAuto(wndclass_i.lpszMenuName);
                    className          = this.className;
                    this.defWindowProc = wndclass_i.lpfnWndProc;
                    this.hashCode      = this.className.GetHashCode();
                }
                this.windowClassName = this.GetFullClassName(className);
                this.windowProc      = new System.Windows.Forms.NativeMethods.WndProc(this.Callback);
                wc.lpfnWndProc       = this.windowProc;
                wc.hInstance         = System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null);
                wc.lpszClassName     = this.windowClassName;
                short num2 = System.Windows.Forms.UnsafeNativeMethods.RegisterClass(wc);

                if (num2 == 0)
                {
                    int num3 = Marshal.GetLastWin32Error();
                    if (num3 == 0x582)
                    {
                        System.Windows.Forms.NativeMethods.WNDCLASS_I wndclass_i2 = new System.Windows.Forms.NativeMethods.WNDCLASS_I();
                        if (System.Windows.Forms.UnsafeNativeMethods.GetClassInfo(new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null)), this.windowClassName, wndclass_i2) && (wndclass_i2.lpfnWndProc == NativeWindow.UserDefindowProc))
                        {
                            if (System.Windows.Forms.UnsafeNativeMethods.UnregisterClass(this.windowClassName, new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null))))
                            {
                                num2 = System.Windows.Forms.UnsafeNativeMethods.RegisterClass(wc);
                            }
                            else
                            {
                                do
                                {
                                    domainQualifier++;
                                    this.windowClassName = this.GetFullClassName(className);
                                    wc.lpszClassName     = this.windowClassName;
                                    num2 = System.Windows.Forms.UnsafeNativeMethods.RegisterClass(wc);
                                }while ((num2 == 0) && (Marshal.GetLastWin32Error() == 0x582));
                            }
                        }
                    }
                    if (num2 == 0)
                    {
                        this.windowProc = null;
                        throw new Win32Exception(num3);
                    }
                }
                this.registered = true;
            }