Ponowne używanie istniejących wskaźników do obiektów
W tym scenariuszu serwer odpowiada na żądanie OBJID_CLIENT przy użyciu tego samego wskaźnika interfejsu IAccessible za każdym razem.
W poniższym przykładowym kodzie obiekt kontrolny jest pobierany z dodatkowych danych okna, a metoda kontrolki jest wywoływana w celu pobrania obiektu serwera ułatwień dostępu (klasy AccServer zdefiniowanej przez aplikację), jeśli istnieje. Jeśli serwer ułatwień dostępu jeszcze nie istnieje, zostanie utworzony.
Po utworzeniu obiektu serwera dostępności ma on licznik odniesień równy 1. LresultFromObject kilkukrotnie zwiększa liczbę referencji, ale te referencje zostaną zwolnione po zakończeniu pracy klienta z obiektem. Serwer zwalnia swoje odwołanie, gdy okno kontrolne zostanie zniszczone.
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;
}