Win32. Hook. Intercept WM_MOUSEWHEEL by child windows - c++

I am trying to intercept mouse wheel by child windows under cursor. But something is wrong. It seems like message sends many times. What did I do wrong?
LRESULT CALLBACK MouseProc(__in int nCode,
__in WPARAM wParam,
__in LPARAM lParam)
{
LRESULT ret = 0;
static BOOL b = TRUE;
if (wParam == WM_MOUSEWHEEL)
{
if (b)
{
MOUSEHOOKSTRUCTEX *pMhs = (MOUSEHOOKSTRUCTEX *)lParam;
short zDelta = HIWORD(pMhs->mouseData);
POINT pt;
GetCursorPos(&pt);
LPARAM lParam = MAKELPARAM(pt.x, pt.y);
HWND hWnd = WindowFromPoint(pt);
b = FALSE;
SendMessage(hWnd, WM_MOUSEWHEEL, zDelta, lParam);
}
else
{
b = TRUE;
}
ret = 1;
}
else
{
CallNextHookEx(0, nCode, wParam, lParam);
}
return ret;
}

Related

How can I detect Mouse Click simulated by SendMessage or PostMessage?

I'm working on an anti-clicker in C++. My code works with all Programs that simulate Mouse Clicks, except for Programs which use code like this:
NativeMethods.SendMessage((IntPtr)processHandle, WM_LBUTTONDOWN, (IntPtr)1, (IntPtr)LParams(y, x));
My code:
while (TRUE) {
struct tagMSG Msg;
HHOOK Mouse = SetWindowsHookEx(WH_MOUSE_LL, MouseProcces, NULL, NULL);
while (GetMessage(&Msg, GameMainWindow, 0, 0)) {
Sleep(5000);
TranslateMessage(&Msg);
DispatchMessageA(&Msg);
}
UnhookWindowsHookEx(Mouse);
}
LRESULT CALLBACK MouseProcces(INT NCode, WPARAM WParam, LPARAM LParam) {
if (WParam != WM_MOUSEMOVE && WParam != WM_MOUSEWHEEL)
{
std::cout << "click" << std::endl;
}
return CallNextHookEx(NULL, NCode, WParam, LParam);
}

Issue when comparing lpcs->lpszName with other value like "OLEChannelWnd" in a WH_CBT callback

Below is the code of my WH_CBT callback. I am trying to ignore any click of a hyperlink in Outlook. When I click a hyperlink in Outlook, I'm getting a message box:
But when I return 1 in the callback without the if condition, it works fine.
LRESULT __declspec(dllexport)__stdcall CALLBACK GetCBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
HRESULT hResult;
if (nCode < 0)
CallNextHookEx(hkb, nCode, wParam, lParam);
HWND parentWin;
DWORD ChiledThreadID;
DWORD parentProcessID = 0;
std::ostringstream streamcb;
DWORD xx = (DWORD)15028;
HWND hCurrWnd;
TCHAR clsName_v[22];
TCHAR className[22]="OleMainThreadWndClass";
LPCSTR oleName2 = _T("OLEChannelWnd");
int number;
f1 = fopen("C:\\Log\\report.txt", "a+");
if (nCode == HCBT_CREATEWND)
{
hCurrWnd = (HWND)wParam;
parentWin = GetAncestor(hCurrWnd, GA_ROOT);
ChiledThreadID = GetWindowThreadProcessId(parentWin, &parentProcessID);
if (parentProcessID == xx)
{
writetofile("HCBT_CREATEWND", 15, f1);
CBT_CREATEWNDA* cw = (CBT_CREATEWNDA*)lParam;
CREATESTRUCTA* lpcs = (CREATESTRUCTA*)cw->lpcs;
number = GetClassName(hCurrWnd, clsName_v, 22);
writetofile(clsName_v, number, f1);
if (lpcs->lpszName)
{
if (CompareString(LOCALE_SYSTEM_DEFAULT, 0, lpcs->lpszName, -1, _T("OLEChannelWnd"), 13) == CSTR_EQUAL)
{
return 1;
}
}
}
}
fclose(f1);
LRESULT RetVal = CallNextHookEx(hkb, nCode, wParam, lParam);
return RetVal;
}
Your code is missing two return statements, one on the CallNextHookEx(), and one at the end of the callback. So, the return value of the callback is indeterminate unless the input string matches your criteria. Your compiler should have warned you about the second missing return. A function with a non-void return type must exit with a return <value>; statement.
Try this instead:
LRESULT __declspec(dllexport) CALLBACK GetCBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_CREATEWND)
{
HWND hCurrWnd = (HWND) wParam;
HWND parentWin = GetAncestor(hCurrWnd, GA_ROOT);
DWORD parentProcessID = 0;
DWORD ChildThreadID = GetWindowThreadProcessId(parentWin, &parentProcessID);
const DWORD xx = 8108;
if (parentProcessID == xx)
{
FILE *f1 = fopen("C:\\Log\\report.txt", "a+");
writetofile("HCBT_CREATEWND", 14, f1);
CBT_CREATEWND* cw = (CBT_CREATEWND*) lParam;
CREATESTRUCT* lpcs = (CREATESTRUCT*) cw->lpcs;
TCHAR clsName_v[22] = {};
int number = GetClassName(hCurrWnd, clsName_v, 22);
writetofile(clsName_v, number, f1);
fclose(f1);
if (lpcs->lpszName)
{
if (CompareString(LOCALE_SYSTEM_DEFAULT, 0, lpcs->lpszName, -1, _T("OLEChannelWnd"), 13) == CSTR_EQUAL)
{
return 1;
}
}
}
}
return CallNextHookEx(hkb, nCode, wParam, lParam);
}

How to detect drag in a mouse hook procedure

I've successfully set up a mouse hook for a window, and I'd like to detect a drag operation when the left mouse button is clicked. I've tried using DragDetect, as shown below, but it never returns TRUE, and it never suppresses the subsequent mouse-up event (in this code, hwnd is the target window):
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
{
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
switch (wParam)
{
case WM_LBUTTONDOWN:
{
// Get the click point
LPMOUSEHOOKSTRUCT pMouseHookStruct = reinterpret_cast<LPMOUSEHOOKSTRUCT>(lParam);
CPoint ptClick = pMouseHookStruct->pt;
// Drag detect
BOOL fDrag = DragDetect(hwnd, ptClick);
if (fDrag)
{
MessageBox(NULL, L"Drag", NULL, MB_OK);
}
break;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
Does DragDetect not work within hook procedures, or am I just doing something wrong?
As comments pointed, DragDetect does not predict the future. But you can compare the POINT of WM_LBUTTONDOWN and WM_MOUSEMOVE in a click-time.
Sample:
#include <windows.h>
#include <iostream>
using namespace std;
typedef BOOL(*SETHOOK)(HWND hWnd);
typedef BOOL(*UNHOOK)();
LRESULT CALLBACK WndProcFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
int main(int argc, char* argv[])
{
WNDCLASS wc{};
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProcFunc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = L"Class_Name";
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
RegisterClass(&wc);
HWND hWnd = CreateWindow(L"Class_Name", L"Test", WS_OVERLAPPEDWINDOW, 0, 0, 1000, 500, NULL, NULL, GetModuleHandle(NULL), NULL);
SETHOOK SetHook;
UNHOOK UnHook;
HMODULE h = LoadLibraryA("PATH\\DLL4.dll");
SetHook = (SETHOOK)GetProcAddress(h, "SetHook");
UnHook = (UNHOOK)GetProcAddress(h, "UnHook");
BOOL i = SetHook(hWnd);
ShowWindow(hWnd, 1);
UpdateWindow(hWnd);
MSG Msg;
while (GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
UnHook();
return 0;
}
DLL:
// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define PRESS TRUE
#define RELEASE FALSE
HHOOK tHook;
HMODULE hinstDLL;
BOOL button = RELEASE;
BOOL Drag = FALSE;
HWND hWnd;
POINT pt = { 0 };
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode < 0)
{
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
LPMOUSEHOOKSTRUCT pMouseHookStruct = reinterpret_cast<LPMOUSEHOOKSTRUCT>(lParam);
if (pMouseHookStruct->hwnd != hWnd)
{
button = RELEASE;
Drag = FALSE;
}
else {
switch (wParam)
{
case WM_LBUTTONDOWN:
{
button = PRESS;
Drag = FALSE;
printf("WM_LBUTTONDOWN\n");
printf("%d , %d\n", pMouseHookStruct->pt.x, pMouseHookStruct->pt.y);
pt.x = pMouseHookStruct->pt.x;
pt.y = pMouseHookStruct->pt.y;
break;
}
case WM_LBUTTONUP:
{
button = RELEASE;
Drag = FALSE;
printf("WM_LBUTTONUP\n");
break;
}
case WM_MOUSEMOVE:
{
if (button == PRESS)
{
printf("%d , %d\n", pMouseHookStruct->pt.x, pMouseHookStruct->pt.y);
if (Drag == FALSE && (abs(pt.x - pMouseHookStruct->pt.x) >= GetSystemMetrics(SM_CXDRAG) || abs(pt.y - pMouseHookStruct->pt.y) >= GetSystemMetrics(SM_CYDRAG)))
{
printf("Drag\n");
Drag = TRUE;
}
}
break;
}
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
extern "C" __declspec(dllexport) BOOL SetHook(HWND hwnd)
{
hWnd = hwnd;
tHook = SetWindowsHookEx(WH_MOUSE, MouseProc, hinstDLL, 0);
if (tHook == NULL)
return FALSE;
else
return TRUE;
}
extern "C" __declspec(dllexport) BOOL UnHook()
{
return UnhookWindowsHookEx(tHook);
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
hinstDLL = hModule;
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

SetWindowsHookEx 7e error

I'm having some troubles hooking the process to read keypresses. GetLastError gives 0x7e which means module not found, though it seems I implemented the module finding thing correctly. What I currently have (PId is process id, ThreadId is main process thread id):
HWND hWindow = NULL;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
DWORD lpdwProcessId;
GetWindowThreadProcessId(hwnd, &lpdwProcessId);
if (lpdwProcessId == lParam)
{
hWindow = hwnd;
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK kbCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT kbdStruct = *((KBDLLHOOKSTRUCT *)lParam);
if (wParam == WM_KEYDOWN) {
if (kbdStruct.vkCode = VK_F1) {
std::cout << "f1 was pressed" << std::endl;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int main()
{
...
EnumWindows(EnumWindowsProc, initt.PId);
HHOOK hKbdHook = SetWindowsHookEx(WH_CALLWNDPROC, kbCallback, (HINSTANCE)GetWindowLongPtr(hWindow, GWLP_HINSTANCE), initt.ThreadId);
}

How to remap keyboard key in c++ with LowLevelKeyboardProc?

I need to remap some of keys like Left Alt but i just disable it so code for disable Left Alt look like this:
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*) lParam;
if (p->vkCode == VK_LMENU) return 1;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
So I try to remap Left Alt to Left Ctrl and use function like keybd_event and SendMessageA but didn't get nothing.
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*) lParam;
if (p->vkCode == VK_LMENU)
{
keybd_event(VK_CONTROL, 0, 0, 0);
// or use this is sameSendMessageA(0, WM_KEYUP, VK_CONTROL, 0);
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
How to remap Left Alt to Left Ctrl?
This is a way to remap Alt to Ctrl works great. Don't forget the default parameter
switches back to alt if you do not define wParam may just be my computer well give it a try
include iostream
include windows.h
using namespace std;
HHOOK hHook = 0;
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*) lParam;
if (p->vkCode == VK_LMENU) // VK_LMENU = ALT key
{
switch (wParam){
case WM_SYSKEYDOWN :{ // use SYSKEYDOWN
cout << "Key down" << endl;
keybd_event(VK_LCONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | 0, 0 );
break;
}
case WM_KEYUP: // use regular keyup
{
cout << "Key up" << endl;
keybd_event( VK_LCONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
return 1;
break;
}
default:
wParam = WM_SYSKEYDOWN; // if you do not specify it changes back to alt
break;
}
return 1;
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, &LowLevelKeyboardProc, hThisInstance, NULL);
if (hHook == NULL)
{
cout << "Error" << endl;
return 1;
}
MSG messages;
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}