CComPtr CoCreateInstance returns 0x80070582 (Class already exists.)

1

I have a StartComObjects function called when the user presses the Login button and a StopComObjects function called when the user presses the Cancel button. The StartComObjects function uses CComPtr.CoCreateInstance to create the COM object and sets up some connection points using AfxConnectionAdvise. When the user presses the Cancel button the connection points are disconnected using AfxConnectionUnadvise and the COM object is stopped before calling Release on the CComPtr.

When I press the login button a second time the CComPtr.CoCreateInstance returns 0x80070582 (Class already exists). This prevents the COM object from being created on the second call to StartComObjects. I am not sure why this isn't working. Shouldn't CComPtr::Release free the COM object and allow me to create a new one after the old one was stopped? Is there any way to get around this?

c++
com
atl
smart-pointers
asked on Stack Overflow May 18, 2010 by Trevor Balcom

1 Answer

3

It is a Windows error (facility 7, error code 1410), caused by RegisterClass(Ex). This sample code reproduces it:

#include "stdafx.h"
#include <windows.h>
#include <assert.h>

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int)
{
    WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.hInstance = hInstance;
    wcex.lpszClassName = L"Example";
    ATOM at1 = RegisterClassEx(&wcex);
    assert(at1 != 0);
    // Register again, should fail with error 1410
    ATOM at2 = RegisterClassEx(&wcex);
    assert(at2 == 0);
    int err = GetLastError();
    assert(err == ERROR_CLASS_ALREADY_EXISTS);
    return 0;
}

Look through your code for places where the coclass uses RegisterClass(Ex). It must use UnregisterClass when the instance is destroyed. Or avoid registering the window class again. Or ignore the specific error code.

answered on Stack Overflow May 18, 2010 by Hans Passant

User contributions licensed under CC BY-SA 3.0