Support case where win32socketserver's window class is not unregistered properly.

Either from failure to shutdown or when instantiated in a dll that is loaded or
unloaded multiple times within a single process lifetime.

Change-Id: I52b05a6d84c9312fbd45aaa34ed3f49566daadfd
Bug: b/140961297
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/155987
Commit-Queue: Tommi <tommi@webrtc.org>
Reviewed-by: Tommi <tommi@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29574}
diff --git a/rtc_base/win32_window.cc b/rtc_base/win32_window.cc
index b4d919d..775535a 100644
--- a/rtc_base/win32_window.cc
+++ b/rtc_base/win32_window.cc
@@ -19,24 +19,16 @@
 // Win32Window
 ///////////////////////////////////////////////////////////////////////////////
 
-static const wchar_t kWindowBaseClassName[] = L"WindowBaseClass";
+static const wchar_t kWindowBaseClassName[] = L"RtcWindowBaseClass";
 HINSTANCE Win32Window::instance_ = nullptr;
 ATOM Win32Window::window_class_ = 0;
 
 Win32Window::Win32Window() : wnd_(nullptr) {}
 
-Win32Window::~Win32Window() {
-  RTC_DCHECK(nullptr == wnd_);
-}
+Win32Window::~Win32Window() { RTC_DCHECK(nullptr == wnd_); }
 
-bool Win32Window::Create(HWND parent,
-                         const wchar_t* title,
-                         DWORD style,
-                         DWORD exstyle,
-                         int x,
-                         int y,
-                         int cx,
-                         int cy) {
+bool Win32Window::Create(HWND parent, const wchar_t* title, DWORD style,
+                         DWORD exstyle, int x, int y, int cx, int cy) {
   if (wnd_) {
     // Window already exists.
     return false;
@@ -51,10 +43,18 @@
       return false;
     }
 
-    // Class not registered, register it.
+    // Register or reregister the class as necessary.  window_class_ == nullptr
+    // is not an infallible indicator that the class is unregistered.
     WNDCLASSEXW wcex;
     memset(&wcex, 0, sizeof(wcex));
     wcex.cbSize = sizeof(wcex);
+    if (::GetClassInfoExW(instance_, kWindowBaseClassName, &wcex) &&
+        !::UnregisterClassW(kWindowBaseClassName, instance_)) {
+      RTC_LOG_GLE(LS_ERROR) << "UnregisterClass failed.";
+    }
+
+    memset(&wcex, 0, sizeof(wcex));
+    wcex.cbSize = sizeof(wcex);
     wcex.hInstance = instance_;
     wcex.lpfnWndProc = &Win32Window::WndProc;
     wcex.lpszClassName = kWindowBaseClassName;
@@ -76,14 +76,14 @@
 
 void Win32Window::Shutdown() {
   if (window_class_) {
-    ::UnregisterClass(MAKEINTATOM(window_class_), instance_);
+    if (!::UnregisterClass(MAKEINTATOM(window_class_), instance_)) {
+      RTC_LOG_GLE(LS_ERROR) << "UnregisterClass failed.";
+    }
     window_class_ = 0;
   }
 }
 
-bool Win32Window::OnMessage(UINT uMsg,
-                            WPARAM wParam,
-                            LPARAM lParam,
+bool Win32Window::OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam,
                             LRESULT& result) {
   switch (uMsg) {
     case WM_CLOSE:
@@ -96,17 +96,13 @@
   return false;
 }
 
-bool Win32Window::OnClose() {
-  return true;
-}
+bool Win32Window::OnClose() { return true; }
 
 void Win32Window::OnNcDestroy() {
   // Do nothing. }
 }
 
-LRESULT Win32Window::WndProc(HWND hwnd,
-                             UINT uMsg,
-                             WPARAM wParam,
+LRESULT Win32Window::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam,
                              LPARAM lParam) {
   Win32Window* that =
       reinterpret_cast<Win32Window*>(::GetWindowLongPtr(hwnd, GWLP_USERDATA));