オブジェクトへの既存のポインターを再利用する

このシナリオでは、サーバーは毎回同じ IAccessible インターフェイス ポインターを使用してOBJID_CLIENT要求に応答します。

次のコード例では、コントロール オブジェクトが追加のウィンドウ データから取得され、アクセシビリティ サーバー オブジェクト (アプリケーション定義の AccServer クラス) を取得するためにコントロールのメソッドが呼び出されます (存在する場合)。 アクセシビリティ サーバーがまだ存在しない場合は、作成されます。

アクセシビリティ サーバー オブジェクトを作成すると、参照カウントは 1 になります。 LresultFromObject は 参照カウントを数回インクリメントしますが、クライアントが オブジェクトで終了すると、これらの参照は解放されます。 コントロール ウィンドウが破棄されると、サーバーはその参照を解放します。

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;
    }