Open Aoi-hosizora opened 5 years ago
DLLEXPORT bool __stdcall InitGetMsgHook(int threadId, HWND destination) {
if (hMod == NULL)
return false;
if (hwndMain != NULL) {
UINT msg = RegisterWindowMessage(L"AH_SYSTEM_MENU_HOOK_GETMSG_REPLACE");
if (msg != 0)
SendNotifyMessage(hwndMain, msg, 0, 0);
}
hwndMain = destination;
hookGetMsg = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC) GetMsgHookCb, hMod, threadId);
return hookGetMsg != NULL;
}
static LRESULT CALLBACK GetMsgHookCb(int code, WPARAM wParam, LPARAM lParam) {
if (code >= 0) {
UINT msg = RegisterWindowMessage(L"AH_SYSTEM_MENU_HOOK_GETMSG");
UINT msg2 = RegisterWindowMessage(L"AH_SYSTEM_MENU_HOOK_GETMSG_PARAMS");
auto pMsg = (MSG *) lParam;
if (msg != 0 && pMsg->message != msg && pMsg->message != msg2 && wParam == PM_REMOVE) {
if (pMsg->message == WM_SYSCOMMAND) {
SendNotifyMessage(hwndMain, msg, (WPARAM) pMsg->hwnd, pMsg->message);
SendNotifyMessage(hwndMain, msg2, pMsg->wParam, pMsg->lParam);
}
}
}
return CallNextHookEx(hookGetMsg, code, wParam, lParam);
}
if (m.Msg == HookMessage.MSG_HGETMSG_GETMSG) {
addToList(m.WParam, "GetMsg");
WinUtil.cacheHandle = m.WParam;
WinUtil.cacheMessage = m.LParam;
} else if (m.Msg == HookMessage.MSG_HGETMSG_GETMSG_PARAMS) {
if (WinUtil.cacheHandle != IntPtr.Zero && WinUtil.cacheMessage != IntPtr.Zero) {
addToList(WinUtil.cacheHandle, "GetMsgParam");
onWinProcMsg(WinUtil.cacheHandle, WinUtil.cacheMessage, m.WParam, m.LParam);
WinUtil.cacheHandle = WinUtil.cacheMessage = IntPtr.Zero;
}
}
private void onWinProcMsg(IntPtr hwnd, IntPtr message, IntPtr WParam, IntPtr LParam) {
if (message.ToInt64() == NativeConstant.WM_SYSCOMMAND) {
uint menuid = (uint)(WParam.ToInt64() & 0x0000FFFF);
if (menuid == WinUtil.MENU_ID_TOPMOST)
WinUtil.OnTopMostMenuItemClick(hwnd);
else if (menuid == WinUtil.MENU_ID_PRTSC)
WinUtil.OnPrtScMenuItemClick(hwnd);
else if (menuid == WinUtil.MENU_ID_PATH)
WinUtil.OnPathMenuItemClick(hwnd);
}
}
WH_SHELL
和WH_CBT
的过滤不准确,并且有些窗口没有回调WH_GETMESSAGE
对一些窗口的WM_SYSCOMMAND
信息没有回调WH_CALLWNDPROC
钩子没有反应onWindowCreated
和onWindowActivated
对一些窗口的判断有误,原因可能在GetAllWindows
对窗口样式的判断IsWindow
OnPrtScMenuItemClick
对PrintWindow
的返回窗口样式问题