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

我是研究僧i 提交于 2019-12-25 17:10:07

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/43827438/is-there-any-way-to-know-if-a-window-procedure-was-subclassed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!