问题
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