Free Source Code and Program Tips
Install global hook without dll
Chinese version author: peach@newsmth.net
Userally, when you install a global hook, you need a dll. But now, you can install WH_KEYBOARD_LL or WH_MOUSE_LL global hook without dll’s help.
The following code was tested under win2k3. And I it will also run on 2k/xp.
#define _WIN32_WINNT 0400
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include
#include
#include
DWORD g_main_tid = 0;
HHOOK g_kb_hook = 0;
BOOL CALLBACK con_handler (DWORD)
{
PostThreadMessage (g_main_tid, WM_QUIT, 0, 0);
return TRUE;
};
LRESULT CALLBACK kb_proc (int code, WPARAM w, LPARAM l)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)l;
const char *info = NULL;
if (w == WM_KEYDOWN)
info = “key dn”;
else if (w == WM_KEYUP)
info = “key up”;
else if (w == WM_SYSKEYDOWN)
info = “sys key dn”;
else if (w == WM_SYSKEYUP)
info = “sys key up”;
printf (“%s – vkCode [%04x], scanCode [%04x]\n”,
info, p->vkCode, p->scanCode);
// always call next hook
return CallNextHookEx (g_kb_hook, code, w, l);
};
int main (void)
{
g_main_tid = GetCurrentThreadId ();
SetConsoleCtrlHandler (&con_handler, TRUE);
g_kb_hook = SetWindowsHookEx (
WH_KEYBOARD_LL,
&kb_proc,
GetModuleHandle (NULL), // It shouldn’t be NULL, or will fail
0);
if (g_kb_hook == NULL)
{
fprintf (stderr,
“SetWindowsHookEx failed with error %d\n”,
::GetLastError ());
return 0;
};
// The message loop is necesarry
MSG msg;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
};
UnhookWindowsHookEx (g_kb_hook);
return 0;
};
| Print article | This entry was posted by support on January 3, 2007 at 1:14 am, and is filed under Win32/MFC. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
No trackbacks yet.
How to catch the events when click on the app icon on taskbar
about 1 year ago - No comments
How to catch the events when you click on the application’s icon on the taskbar? It’s the WM_NC class messages. For example, you can handle the WM_NCLBUTTONDBLCLK message. And you can alse reponsible the WM_SYSCOMMAND messages. In particular, the special cases of SC_MINIMIZE and SC_MAXIMIZE are what are called from the system menu, which is
How to send/post message to CDocument?
about 2 years ago - 2 comments
Sometime you need to send/post message to CDoumnet, but CDocument is not a window, and it can’t receive message. How do that? There are 3 solutions: Solution 1: Send the message to the main window. Send/Post the message to the main frame or the view, and then call some functions of the document. But if
Intermittent work in UI thread
about 2 years ago - No comments
Pedro Ferreira I’m trying to create a UI thread to do constant background work, but I’m having some design problems. The thread needs to fetch records from a database. do some processing on each record and, when there are no more records, sleep for 1 minute before check the database again. The process will be
Intermittent work in UI thread
about 2 years ago - No comments
Pedro Ferreira said: I’m trying to create a UI thread to do constant background work, but I’m having some design problems. The thread needs to fetch records from a database. do some processing on each record and, when there are no more records, sleep for 1 minute before check the database again. The process will
How to hook the file related event
about 3 years ago - No comments
How to hook the file related event? It depends on what you need to do when such an event takes places. If what you just want to be notified, FindFirstChangeNotification and ReadDirectoryChanges are enough. If you need to intercept adn affect the operation somehow, I have heard it can be done with a kenerl-level driver(Filesystem
CComModule in porting ATL DLL project to VC8
about 3 years ago - No comments
When migrating an ATL project from vc6 to vc8, you will get a problem about CComModule. Problem: In vc6, there is such a declaration in stdafx.h as: extern CComModule _Module; and in the main module: CComModule _Module; But when you complie it in vc8, the following errors apears: c:\temp\myatl\stdafx.h(24) : error C2146: syntax error :
Solution: Crash in Release build with self-defined message
about 3 years ago - No comments
We often meet such case: I have an application with a modeless dialog. The dialog comes up perfectly in DEBUG build, But in RELEASE build, it crashed. Such problem often occures because of incorrect signature of message handle functions, that included into MFC message map for your CWnd based class.We should very carefully check signatures
What is the usage of WM_NULL?
about 3 years ago - No comments
Alex Rest found the WM_NULL message and was curious where somebody would need to use that. I have found a strange Windows message. It is described in MSDN so: WM_NULL: The WM_NULL message performs no operation. An application sends the WM_NULL message if it wants to post a message that the recipient window will ignore.
ucoSpy,a system enhancement tool like spy++
about 3 years ago - No comments
Introduce ucoSpy is a system enhancement application, used to view various attributes of windows or controls, and copy multiple contents that can’t be normally copied as well, such as passwords hidden as asteroids, and a TreeView. And ucoSpy can give a report the tree relationship of the windows and their child control. Main features ucoSpy
about 8 months ago
Thank you!
This helps a lot!
about 2 months ago
Thanks. This saved a big project from some bad bugs.