C++ 重新注册用户定义的窗口类-C++;

C++ 重新注册用户定义的窗口类-C++;,c++,winapi,C++,Winapi,我从以下代码中调用RegisterClassEx得到一个类已存在错误。此代码位于类构造函数中: this->m_wcx.cbSize = sizeof(WNDCLASSEX); // size of structure this->m_wcx.style = CS_HREDRAW | CS_VREDRAW; // initially minimized this->m_wcx.lpfnWndProc = &WndProc; // points to win

我从以下代码中调用
RegisterClassEx
得到一个类已存在错误。此代码位于类构造函数中:

this->m_wcx.cbSize = sizeof(WNDCLASSEX);  // size of structure
this->m_wcx.style = CS_HREDRAW | CS_VREDRAW; // initially minimized
this->m_wcx.lpfnWndProc = &WndProc;       // points to window procedure
this->m_wcx.cbClsExtra = 0;               // no extra class memory
this->m_wcx.cbWndExtra = 0;               // no extra window memory
this->m_wcx.hInstance = m_hInstance;      // handle to instance
this->m_wcx.hIcon = ::LoadIcon( NULL, IDI_APPLICATION ); // default app icon
this->m_wcx.hCursor = ::LoadCursor( NULL, IDC_ARROW ); // standard arrow cursor
this->m_wcx.hbrBackground = NULL;         // no background to paint
this->m_wcx.lpszMenuName = NULL;          // no menu resource
this->m_wcx.lpszClassName = s_pwcWindowClass; // name of window class
this->m_wcx.hIconSm = NULL;               // search system resources for sm icon

// Register window class.
if ( (this->m_atom = ::RegisterClassEx( &m_wcx )) == 0 )
{
    dwError = ::GetLastError();
    TRACE(_T("Failed to register window class.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
    THROW(dwError);
}
这段代码第一次执行时,它的工作没有任何问题。调用类析构函数时,它将注销该类:

::UnregisterClass( s_pwcWindowClass, this->m_hInstance );

这一切都很好,第一次通过。对构造函数的后续调用导致
RegisterClassEx
失败,出现
错误\u类\u已存在
。我做错了什么?

如果系统中存在该类的窗口,则取消注册类()将失败(不会删除该类)。因此,对于使用该类创建的所有窗口,您需要执行以下操作:
::destronWindow()

以后需要类时,我不会注销它。我要测试错误,类已经存在,如下所示:

ATOM reg=RegisterClassEx(&m_wcx);
DWORD err=GetLastError();
if(!reg && !(err==ERROR_CLASS_ALREADY_EXISTS)){
    //throw only if not successful and not already registered
}

我也有同样的问题,我确信我销毁了使用指定类创建的唯一窗口,然后调用UnregisterClass,它返回TRUE(1)但是它似乎没有从系统中删除该类,并且在下次调用RegisterClassEx时,我得到了错误“已经存在”\u

您确定在创建第二个实例之前调用了第一个实例的析构函数吗?@unaperson:是的。我正在从测试应用程序运行此程序,除非出现致命错误(将被记录),否则只有在我手动指示调用析构函数时才会调用析构函数。您是否检查了
UnregisterClass
的返回值?请参阅