I want to learn something about Windows Hooks. Right now, I'm only interested in "catching" messages.
So I did the following, but it doesn't work.
I want to catch the message in the same thread I'm using. I don't want to catch another thread's messages.
Can somebody explain to me what I'm doing wrong?
I install the hook as follows:
myhookdata.nType = WH_GETMESSAGE;
myhookdata.hkprc = GetMsgProc;
myhookdata.hhook = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, NULL, GetCurrentThreadId());
Then in the hook procedure I do this, just for testing. But the "WM_LBUTTONDOWN" never get catched!!
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
LPCWPSTRUCT message = (LPCWPSTRUCT)lParam;
if (nCode < 0)
return CallNextHookEx(myhookdata.hhook, nCode,
wParam, lParam);
switch (nCode)
{
case HC_ACTION:
if (wParam)
if (message->message == WM_LBUTTONDOWN)
Sleep(0);
break;
default:
break;
}
return CallNextHookEx(myhookdata.hhook, nCode, wParam, lParam);
}
According to the documentation:
lParam [in]
Type: LPARAM
A pointer to an MSG structure that contains details about the message.
So you only need to change the code:
PMSG message = (PMSG)lParam;
Then it works for me.
Related
I have a procedure that when a user press Ctrl button and right click it will show a message box to screen. But it has a loop, I only press Ctrl button and right click once time but it shows a sequence of message box. How to fix this?
https://youtu.be/LzI9M_zEEKQ
This is my MouseProc procedure
#define EXPORT __declspec(dllexport)
unsigned char KeyState[256];
LRESULT EXPORT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
return CallNextHookEx(hHook, nCode, wParam, lParam);
GetKeyboardState(KeyState);
if (nCode == HC_ACTION)
{
if ((wParam == WM_RBUTTONUP) && (KeyState[VK_CONTROL] & 0x80))
{
MessageBox(NULL, L"Ctrl + Right Click", L"Mouse hook", MB_OK);
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
Thanks for reading.
You shall not use MessageBox() in message hooks as it breaks normal message hook flow - MessageBox() runs its own modal loop.
If you need exactly MessageBox there then you should use PostMessage with custom message and handler. In this case MessageBox will be invoked after CallNextHookEx(hHook, nCode, wParam, lParam);
I would like to hide a caret from a RichEdit(50W) with ES_READONLY style specified.
It's pretty confusing for the user, when the caret is blinking and the user can't type.
I tried to hide the caret using HideCaret() function,
however it doesn't work for me with following code:
LRESULT CALLBACK ChatMessaegsSubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) // Subclassed control
{
LRESULT ret = CallWindowProc(WndProc_ChatMessages, hwnd, msg, wParam, lParam);
switch(msg)
{
//Also tried with EN_SETFOCUS
case WM_SETFOCUS:
{
ret = CallWindowProc(WndProc_ChatMessages, hwnd, msg, wParam, lParam);
HideCaret(ChatMessages); //Returns 5 (Access denied.)
break;
}
//According the documentation:
//If your application calls HideCaret five times in a row,
//it must also call ShowCaret five times before the caret is displayed.
case WM_KILLFOCUS: //The message is called when the RichEdit get focus, however nothing happens.
{
ret = CallWindowProc(WndProc_ChatMessages, hwnd, msg, wParam, lParam);
ShowCaret(ChatMessages);
break;
}
}
return ret;
}
Here is the solution:
LRESULT CALLBACK ChatMessaegsSubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
LRESULT ret = CallWindowProc(WndProc_ChatMessages, hwnd, msg, wParam, lParam);
switch(msg)
{
case WM_LBUTTONDOWN:
{
HideCaret(ChatMessages);
break;
}
case WM_KILLFOCUS:
{
ShowCaret(ChatMessages);
break;
}
}
return ret;
}
NOTE this only works when user induces the focus with mouse. Therefore if anyone knows how to deal with it correctly, feel free to answer, I'll be glad.
I am currently facing issues when trying to compile my code that contains this WinProc function which is being used to process messages from our program. For example if a WM_DESTROY message is received via windows I want it to call PostQuitMessage(0) to signal Windows that the application has made a request to quit. Which will cause the WM_QUIT message to cause the WinMain to exit.
I have only been learning C++ a few weeks and don't have the experience or knowledge to fix this and would appreciate any help. I have looked around but so far I cannot find any solutions. I'm pretty new to this so I may have missed something really obvious.
LRESULT WINAPI WinProc (hWnd, msg, UNIT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY;
// Tell windows to kill the program
PostQuitMessage(0);
return 0;
}
return DefWindowProc (hWnd, msg, wParam, lParam );
}
Below is the errors I experience wit the code that I have provided.
error: 'LRESULT WinProc' redeclared as different kind of symbol
error: previous declaration of 'LRESULT WinProc(HWND__*, UINT, WPARAM, LPARAM)'
error: 'hWnd' was not declared in this scope
error: 'msg' was not declared in this scope
error: 'UNIT' was not declared in this scope
Any help would be greatly appreciated.
Thanks
In the function declaration
LRESULT WINAPI WinProc (hWnd, msg, UNIT msg, WPARAM wParam, LPARAM lParam )
you forgot to set type specifiers for the first two parameters hWnd and msg
There must be
LRESULT WINAPI WinProc ( HWND hWnd, UNIT msg, WPARAM wParam, LPARAM lParam )
Also the label has to be followed by a colon while you placed a semicolon
case WM_DESTROY;
try using the callback calling convention instead of winapi
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633570(v=vs.85).aspx
also note the usage of " : " instead of " ; " on switch statement , also specify a type before the handle and message arguments
LRESULT CALLBACK WinProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
case WM_DESTROY:
// Tell windows to kill the program
PostQuitMessage(0);
return 0;
}
return DefWindowProc (hWnd, msg, wParam, lParam );
}
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM )
This should be the function declaration. Your case has a ";" semicolon instead of a ":" colon.
Besides this, I don't see any problems. Try this:
LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
// Tell windows to kill the program
PostQuitMessage(0);
return 0;
}
return DefWindowProc (hWnd, msg, wParam, lParam );
}
I have been having trouble with my program trying to gray out ( and disable ) a sub menu item.
What I'm looking for is that the "run" item be disabled unless the required .ini entry is not empty.
My code
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HMENU hmenu = GetMenu(hWnd);
// Reading in ini
if (0 == strcmp(webLocation, "")){
EnableMenuItem(hmenu,ID_WEBSERVICES_RUN,MF_DISABLED | MF_GRAYED);
WritePrivateProfileString(_T("WEBSERVICES"), _T("Location"), _T("Tool Not Found"), WpathStr);
}
I am unsure as to whether I am getting the HMENU correctly and why this code is not working for the desired effect.
Any help with this would be greatly appreciated.
You can't just put this in the WndProc at the top level. WndProc process events, whether the window has been constructed or not. It'll be called many times for many different reasons.
Your WndProc will almost certainly look like a big switch on message. The one you want here is WM_INITDIALOG:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
// jump to a new function that reads the .ini
// and disables the control etc.
return OnInitDialog(hWnd, wParam, lParam);
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
I'm trying to send a duplicate message to an editbox window in this code:
extern "C" HOOK_DLL_API LRESULT CALLBACK GetMsgHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
{
CallNextHookEx(gMsgHook, nCode, wParam, lParam);
}
KBDLLHOOKSTRUCT *lpk = (KBDLLHOOKSTRUCT*) lParam;
ghServerWnd; // ghServerWnd == Edit1. that defined..
if (wParam == WM_KEYDOWN)
{
// case1: this code working.. but, unicode(IME character) no sent;;
SendMessageW(ghServerWnd, WM_CHAR, (WPARAM)lpk->vkCode, 0);
// case2: this code - not working.. T_T
SendMessageW(ghServerWnd, wParam, lParam, 0);
}
return CallNextHookEx(gMsgHook, nCode, wParam, lParam);
}
I need help with "case2" as marked in the code.
Thanks for reading.
Well i'm not surprised case 2 does not work. You are sending KBDLLHOOKSTRUCT as the wParam.
I would have thought.
SendMessage( ghServerWnd, wParam, (WPARAM)lpk->vkCode, (LPARAM)lpk->scanCode );
Would work better (Though I'm not 100% convinced my LPARAM is complete).