Reuse Existing Pointers to Objects

In this scenario, the server responds to an OBJID_CLIENT request using the same IAccessible interface pointer each time.

In the following example code, the control object is retrieved from the extra window data, and a method of the control is called to retrieve the accessibility server object (the application-defined AccServer class), if any. If the accessibility server does not yet exist, it is created.

When the accessibility server object is created, it has a reference count of 1. LresultFromObject increments the reference count several times, but these references will be released when the client is finished with the object. The server releases its reference when the control window is destroyed.

case WM_GETOBJECT:
    {
        // Return the IAccessible object.
        if ((DWORD)lParam == (DWORD)OBJID_CLIENT)
        {
            // Get the control. 
            CustomListControl* pCustomList = (CustomListControl*)(LONG_PTR)GetWindowLongPtr(hwnd, 0);
            // Create the accessible object.
            AccServer* pAccServer = pCustomList->GetAccServer();
            if (pAccServer == NULL)
            {
                pAccServer = new AccServer(hwnd, pCustomList);
                pCustomList->SetAccServer(pAccServer);
            }
            if (pAccServer != NULL)  // NULL if out of memory.
            {
                LRESULT Lresult = LresultFromObject(IID_IAccessible, wParam, pAccServer);
                return Lresult;
            }
            else return 0;
        }
        break;
    }

    
case WM_DESTROY:
    {
    CustomListControl* pCustomList = (CustomListControl*)(LONG_PTR)GetWindowLongPtr(hwnd, 0);
    AccServer* pAccServer = pCustomList->GetAccServer();
    if (pAccServer!= NULL)
    {
        // Notify the accessibility object that the control no longer exists.
        pAccServer->SetControlIsAlive(false);
        // Release the reference created in WM_GETOBJECT.
        pAccServer->Release(); 
    }   
    // Destroy the control.
    delete pCustomList;
     break;
    }