解析 MFC 中的 FromHandle

MFC 对 Windows API 进行了封装,在很多方面都会提供便利。用 FromHandle 返回零时对象的指针,就可以调用各种类的方法。临时对象会在 OnIdle 中销毁。这里对 FromHandle 的实现原理从源码上进行解析。

// 
// 1 
// 
CWnd* PASCAL CWnd::FromHandle(HWND hWnd) 
{ 
    CHandleMap* pMap = afxMapHWND(TRUE); //create map if not exist 
    ASSERT(pMap != NULL); 
    CWnd* pWnd = (CWnd*)pMap->FromHandle(hWnd); 
#ifndef _AFX_NO_OCC_SUPPORT 
    pWnd->AttachControlSite(pMap); 
#endif 
    ASSERT(pWnd == NULL || pWnd->m_hWnd == hWnd); 
    return pWnd; 
}
这是 CWnd 的 FromHandle 方法,大致的意思为从 CHandleMap 中获取临时 CWnd 对象的指针。
// 
// 2 
// 
CHandleMap* PASCAL afxMapHWND(BOOL bCreate) 
{ 
    AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState(); 
    if (pState->m_pmapHWND == NULL && bCreate) 
    { 
        BOOL bEnable = AfxEnableMemoryTracking(FALSE); 
#ifndef _AFX_PORTABLE 
        _PNH pnhOldHandler = AfxSetNewHandler(&AfxCriticalNewHandler); 
#endif

        pState->m_pmapHWND = new CHandleMap(RUNTIME_CLASS(CTempWnd), 
            offsetof(CWnd, m_hWnd)); 
#ifndef _AFX_PORTABLE 
        AfxSetNewHandler(pnhOldHandler); 
#endif 
        AfxEnableMemoryTracking(bEnable); 
    } 
    return pState->m_pmapHWND; 
}
再看
#define offsetof(s,m)   (size_t)&(((s *)0)->m)
继续
// 
// 3 
// 
CObject* CHandleMap::FromHandle(HANDLE h) 
{ 
    ASSERT(m_pClass != NULL); 
    ASSERT(m_nHandles == 1 || m_nHandles == 2);
    if (h == NULL) 
        return NULL;
    CObject* pObject = LookupPermanent(h); 
    if (pObject != NULL) 
        return pObject;   // return permanent one 
    else if ((pObject = LookupTemporary(h)) != NULL) 
    { 
        HANDLE* ph = (HANDLE*)((BYTE*)pObject + m_nOffset); 
        ASSERT(ph[0] == h || ph[0] == NULL); 
        ph[0] = h; 
        if (m_nHandles == 2) 
        { 
            ASSERT(ph[1] == h || ph[1] == NULL); 
            ph[1] = h; 
        } 
        return pObject;   // return current temporary one 
    }
    // This handle wasn't created by us, so we must create a temporary 
    // C++ object to wrap it.  We don't want the user to see this memory 
    // allocation, so we turn tracing off.
    BOOL bEnable = AfxEnableMemoryTracking(FALSE); 
#ifndef _AFX_PORTABLE 
    _PNH pnhOldHandler = AfxSetNewHandler(&AfxCriticalNewHandler); 
#endif
    CObject* pTemp = NULL; 
    TRY 
    { 
        pTemp = m_pClass->CreateObject(); 
        if (pTemp == NULL) 
            AfxThrowMemoryException();
        m_temporaryMap.SetAt((LPVOID)h, pTemp); 
    } 
    CATCH_ALL(e) 
    { 
#ifndef _AFX_PORTABLE 
        AfxSetNewHandler(pnhOldHandler); 
#endif 
        AfxEnableMemoryTracking(bEnable); 
        THROW_LAST(); 
    } 
    END_CATCH_ALL
#ifndef _AFX_PORTABLE 
    AfxSetNewHandler(pnhOldHandler); 
#endif 
    AfxEnableMemoryTracking(bEnable);
    // now set the handle in the object 
    HANDLE* ph = (HANDLE*)((BYTE*)pTemp + m_nOffset);  // after CObject 
    ph[0] = h; 
    if (m_nHandles == 2) 
        ph[1] = h;
    return pTemp; 
}

 

转载于:https://www.cnblogs.com/xingrun/p/3409541.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值