I'm trying to use SetWindowsHookExA() with WH_MOUSE_LL to ready low level mouse clicks that are injected.
Here is my code for hooking.
DWORD dwThreadMain; // The process main thread. (Already have this)
HWND mainWindow; // Already have this pointer
MSG hMsg;
MouseHOOK = SetWindowsHookExA(WH_MOUSE_LL, MouseHookProc, NULL, dwThreadMain);
if (!MouseHOOK) {
// Cant't Hook
}
KeyBoardHOOK = SetWindowsHookExA(WH_KEYBOARD_LL, KeyBoardHookProc, NULL, dwThreadMain);
if (!KeyBoardHOOK) {
// Cant't HOOK
}
while (GetMessage(&hMsg, mainWindow, 0, 0)) {
TranslateMessage(&hMsg);
DispatchMessage(&hMsg);
}
This is my MouseHookProc and KeyBoardHookProc:
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION) {
if (wParam == WM_RBUTTONUP || wParam == WM_RBUTTONDOWN
|| wParam == WM_LBUTTONUP || wParam == WM_LBUTTONDOWN) {
MSLLHOOKSTRUCT* hookStruct = (MSLLHOOKSTRUCT*)lParam;
if ((hookStruct->flags & LLMHF_INJECTED) == LLMHF_INJECTED) {
MessageBoxA(NULL, "MOUSE INJECTED!", "POOR MOUSE", MB_OK);
return TRUE;
}
if ((hookStruct->flags & LLMHF_LOWER_IL_INJECTED) == LLMHF_LOWER_IL_INJECTED) {
MessageBoxA(NULL, "MOUSE INJECTED!", "POOR MOUSE", MB_OK);
return TRUE;
}
}
}
return CallNextHookEx(MouseHOOK, nCode, wParam, lParam);
}
LRESULT CALLBACK KeyBoardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION) {
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
KBDLLHOOKSTRUCT* hookStruct = (KBDLLHOOKSTRUCT*)lParam;
if ((hookStruct->flags & LLKHF_INJECTED) == LLKHF_INJECTED) {
MessageBoxA(NULL, "KEYBOARD INJECTED!", "POOR KEYBOARD", MB_OK);
return TRUE;
}
if ((hookStruct->flags & LLKHF_LOWER_IL_INJECTED) == LLKHF_LOWER_IL_INJECTED) {
MessageBoxA(NULL, "KEYBOARD INJECTED!", "POOR KEYBOARD", MB_OK);
return TRUE;
}
}
}
return CallNextHookEx(KeyBoardHOOK, nCode, wParam, lParam);
}
The problem here is not on hooking, the message boxes are showing when the injected flag is there. The KeyBoard blocks the input. But only in mouse, the 'return TRUE' don't prevent the click to be processed.
I Think I've missed something... There is another way to do this?
Related
I want to use the Windows API to get keyboard input from a window. From what I have heard, I can create a callback function that will be called when some events happen. I just don't know how.
ModuleManager::ModuleManager() {
HWND getGameWindow = FindWindow(NULL, TEXT("GameName"));
wndProc = (WNDPROC)SetWindowLongPtrA(getGameWindow, GWLP_WNDPROC, 0);
}
LRESULT CALLBACK ModuleManager::WndProc(const HWND hwnd, unsigned int message, uintptr_t wParam, long lparam) {
if (message == WM_CHAR) {
for (Module* m : modules) {
if (m->getKeybinding() == wParam) {
m->isActive = !m->isActive; // toggle
}
}
}
return CallWindowProcA(wndProc, hwnd, message, wParam, lparam);
}
Here is my current code. I want to set a callback on WndProc function.
It seems i figured it out.
The callback function must be a static function for some reason.
So the correct code is following:
ModuleManager::ModuleManager() {
HWND getGameWindow = FindWindow(NULL, TEXT("GameName"));
wndProc = (WNDPROC)SetWindowLongPtrA(getGameWindow, GWLP_WNDPROC, (LONG_PTR) &ModuleManager::WndProc);
}
LRESULT CALLBACK ModuleManager::WndProc(const HWND hwnd, unsigned int message, uintptr_t wParam, long lparam) {
if (message == WM_CHAR) {
for (Module* m : modules) {
if (m->getKeybinding() == wParam) {
m->isActive = !m->isActive; // toggle
}
}
}
return CallWindowProcA(wndProc, hwnd, message, wParam, lparam);
}
I'm trying to hook WH_GETMESSAGE from my class to determine the moment when specific window is resizing. However, looks like the hook isn't set.
Class from where I try to hook:
class WindowDisplayHelper : // public ...
{
public:
// some other public methods here
void SetMsgHook();
protected:
LRESULT CALLBACK GetMsgProcHook(int code, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK MsgPoc(int code, WPARAM wParam, LPARAM lParam);
private:
// some other private members there
HWND m_windowHandle;
bool m_isWindowResizing = false;
static HHOOK m_msgHook;
static WindowsDisplayHelperMasterWindow* m_pThis;
};
.cpp file:
WindowDisplayHelper* WindowDisplayHelper ::m_pThis = nullptr;
HHOOK WindowDisplayHelper ::m_msgHook = NULL;
void WindowDisplayHelper ::SetMsgHook()
{
m_pThis = this;
m_msgHook = SetWindowsHookEx(WH_GETMESSAGE, MsgPoc, NULL, 0);
}
LRESULT CALLBACK WindowDisplayHelper::MsgPoc(int code, WPARAM wParam, LPARAM lParam)
{
if (m_pThis != nullptr)
{
return m_pThis->GetMsgProcHook(code, wParam, lParam);
}
return CallNextHookEx(0, code, wParam, lParam);
}
LRESULT CALLBACK WindowDisplayHelper::GetMsgProcHook(int code, WPARAM wParam, LPARAM lParam)
{
DUMPER_INFO("Hooked");
if (code < 0)
{
return CallNextHookEx(0, code, wParam, lParam);
}
MSG* lpmsg = (MSG*)lParam;
DUMPER_INFO("Hooked for HWND: %p. Current window %p", lpmsg->hwnd, m_windowHandle);
if (lpmsg->hwnd != m_windowHandle)
{
return CallNextHookEx(0, code, wParam, lParam);
}
if (lpmsg->message == WM_ENTERSIZEMOVE && !m_isWindowResizing)
{
DUMPER_INFO("Start window resizing");
m_isWindowResizing = true;
}
else if (lpmsg->message == WM_EXITSIZEMOVE && m_isWindowResizing)
{
DUMPER_INFO("Stop window resizing");
m_isWindowResizing = false;
}
return CallNextHookEx(0, code, wParam, lParam);
}
Here is how I create WindowDisplayHelper object:
bool DisplayManager::CreateWindowDisplay(TDisplayId displayId, void * windowHandle)
{
auto helper = boost::make_shared<WindowDisplayHelper>(windowHandle);
helper->SetMsgHook();
AddDisplayHelper(displayId, helper);
return true;
}
Though I call SetMsgHook() after the object is created, looks like hook isn't set, because I don't see any debug outputs in my log file and m_isWindowResizing variable always == false. So the question is why my hook doesn't work?
Thanks.
m_msgHook = SetWindowsHookEx(WH_GETMESSAGE, MsgPoc, NULL, 0);
This line produce a ERROR_HOOK_NEEDS_HMOD (1428) system error. It means that Cannot set nonlocal hook without a module handle. If you set dwThreadId parameter to zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread. It is a nonload hook you need specified a valid hmod parameter. You need put the hook code in a DLL as #Remy Lebeau pointed out.
Or set a valid dwThreadId parameter using GetCurrentThreadId() as #rudolfninja pointed out.
I test based on a Windows Desktop Application Template with the following code. It works. You can have a try.
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
// ...
HHOOK m_msgHook = SetWindowsHookEx(WH_GETMESSAGE, MsgPoc, NULL, GetCurrentThreadId());
DWORD errCode = GetLastError();
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
LRESULT CALLBACK MsgPoc(int code, WPARAM wParam, LPARAM lParam)
{
OutputDebugString(L"Hooked");
if (code < 0)
{
return CallNextHookEx(0, code, wParam, lParam);
}
MSG* lpmsg = (MSG*)lParam;
//OutputDebugString("Hooked for HWND: %p. Current window %p", lpmsg->hwnd, m_windowHandle);
if (lpmsg->hwnd != m_windowHandle)
{
return CallNextHookEx(0, code, wParam, lParam);
}
if (lpmsg->message == WM_ENTERSIZEMOVE && !m_isWindowResizing)
{
OutputDebugString(L"Start window resizing");
m_isWindowResizing = true;
}
else if (lpmsg->message == WM_EXITSIZEMOVE && m_isWindowResizing)
{
OutputDebugString(L"Stop window resizing");
m_isWindowResizing = false;
}
return CallNextHookEx(0, code, wParam, lParam);
}
I can't seem to find what I am looking for which is a way to alter the OS's mouse clicks. To specify this would be on Windows.
The goal is to limit the number of mouse clicks a user can register within a period of time.
I think the function that you are looking for is SetWindowsHookEx. Here is a quick example.
#include <windows.h>
const DWORD desireddelay = 10;
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
static DWORD previoustimestamp = 0;
if ((nCode == 0) && (wParam == WM_RBUTTONDOWN))
{
if ((((MSLLHOOKSTRUCT*)lParam)->time - previoustimestamp) < desireddelay)
{
return 1; //Non-Zero Swallows the keystroke. 0 Allows it. Always CallNextHookEx if you are not swallowing it.
}
else
{
previoustimestamp = ((MSLLHOOKSTRUCT*)lParam)->time;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int main()
{
HHOOK hook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, GetModuleHandle(NULL), NULL);
MSG msg;
bool done = false;
while (GetMessage(&msg, NULL, NULL, NULL)) {}
UnhookWindowsHookEx(hook);
return 0;
}
I have wrote a window hook to retrieve the mouse events
_handle = SetWindowsHookEx(WH_MOUSE,
(HOOKPROC)KeyEvent,
nullptr,
GetCurrentThreadId());
static LRESULT WINAPI KeyEvent(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode >= 0)
{
MOUSEHOOKSTRUCT* mStruct = ( MOUSEHOOKSTRUCT*)lParam;
MSLLHOOKSTRUCT* mWheelDStruct = (MSLLHOOKSTRUCT*)lParam;
CMouseHookCom::_this->ReportEventW(mStruct->hwnd, wParam, mWheelDStruct);
}
return(CallNextHookEx(NULL, nCode, wParam, lParam));
}
case WM_MOUSEWHEEL:
OutputDebugString(L"CMouseHookCom-WM_MOUSEWHEEL");
strm = (MSLLHOOKSTRUCT*)extraData;
zDelta = (short)(HIWORD(((MSLLHOOKSTRUCT*)extraData)->mouseData));
_stprintf(buffer, L"CMouseHookCom - WM_MOUSEWHEEL delta %d %i", zDelta, short((strm->mouseData >> 16) & 0xffff));
OutputDebugString(buffer);
if (zDelta > 0)
{
OutputDebugString(L"CMouseHookCom-WM_MOUSEWHEEL UP");
Invoke_onScrollOut(componentId);
}
else
{
OutputDebugString(L"CMouseHookCom-WM_MOUSEWHEEL DOWN");
Invoke_onScrollIn(componentId);
}
break;
default:
_stprintf(buffer, L"CMouseHookCom - UnHandleled event %d", event);
OutputDebugString(buffer);
break;
}
no matter how I try I always get positive results of the wheel. Also I always get different value. What is the correct way to retrieve the data?
You should use Raw Input instead of a mouse hook. That is less overhead on the OS to monitor the mouse, and it gives you information that a hook would not.
my program knows 2 states: A annd B, where A is the default state.
When you press ALT-# it should change it's state to B and when you release the combination it should go back to A.
This should work with a LowLevel Keyboard hook but it seems I am stuck somewhere.
#define VK_POUND 0xBF // 191 - the # key
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
KBDLLHOOKSTRUCT* kbdStruct = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
switch (wParam) {
case WM_KEYDOWN: {
std::cout << kbdStruct->vkCode << "\n";
if (kbdStruct->vkCode == VK_POUND && kbdStruct->flags & LLKHF_ALTDOWN) {
MessageBox(NULL, "WE GOT IT", "", MB_OK);
}
} break;
case WM_KEYUP: {
} break;
}
}
return CallNextHookEx(g_HotKeyHook, nCode, wParam, lParam);
}
What confuses me: When I press the combination ALT and # there is no output in the console.