简体   繁体   中英

Is there any way to know if a window procedure was subclassed?

I want to check if winproc of a window form was subclassed. Any winapi or spy++ trick to do this?

you can use next code for determinate is window subclassed by another module (different from module which register window class )

BOOL IsSubclassed(HWND hwnd)
{
    LPARAM pfn = (IsWindowUnicode(hwnd) ? GetWindowLongPtrW : GetWindowLongPtrA)
        (hwnd, GWLP_WNDPROC);

    HMODULE hmod;
    //if (RtlPcToFileHeader(pfn, &hmod))
    if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|
            GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (PCWSTR)pfn, &hmod))
    {
        DbgPrint("pfn=%p hmod=%p\n", pfn, hmod);

        return hmod != (HMODULE)GetClassLongPtrW(hwnd, GCLP_HMODULE);
    }

    // pfn not in any module - guess subclassed
    return TRUE;
}

note: GetWindowLongPtrA(hwnd, GWLP_WNDPROC) and GetWindowLongPtrW(hwnd, GWLP_WNDPROC) - always return different result - one address of the window procedure and another - handle representing the address of the window procedure : special internal value meaningful only to CallWindowProc - for determine which version A or W retrieves the address of the window procedure - need call IsWindowUnicode . this is undocumented, but reasonable. if subclassed procedure have the same ANSI or UNICODE native that original procedure it can direct call original. if native is different - need translation (Unicode <-> ANSI) for window messages. windows assume that caller or GetWindowLongPtrA is native ANSI window and caller of GetWindowLongPtrW is native UNICODE window. and return pointer to the window procedure if the same native ( A and A or W and W ) otherwise returned handle

from another side GetClassLongPtrW(hwnd, GCLP_HMODULE) and GetClassLongPtrA(hwnd, GCLP_HMODULE) - always return the same result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM