Hook usage in WinCE
Hook is a technique used to intercept a message before it reaches the target window. The following functions are used to create a hook: setwindowshookex
Callnexthookex: transmits messages to the next hook in the hook chain.
Unhookwindowshookex: Release hook
For the setwindowshookex function that creates a hook, msdn provides the following prototype:
Hhook setwindowshookex (
Int idhook, // type of hook to install
Hookproc lpfn, // address of hook procedure
Hinstance hmod, // handle to application instance
DWORD dwthreadid // identity of thread to install hook
);
There is no problem in using Windows, but at the end of the description, you can clearly see the following text about the platform restrictions: Windows CE: unsupported.
That is to say, WinCE does not support hooks.
But is it not supported? You can only say that hooks are not directly supported. You can also use the hook function by using other methods. That is, you can directly obtain the address of the hook function and then call the method.
G_hhookapidll = loadlibrary (_ T ("coredll. dll "));
Setwindowshookex = (_ setwindowshookexw) getprocaddress (g_hhookapidll, _ T ("setwindowshookexw "));
For example, you can obtain the addresses of the other two functions. With the addresses of these three functions, you can use them like this:
G_hinstalledllkbdhook = setwindowshookex (wh_keyboard_ll, keyboardproc, hinstance, 0 );
The following is a summary of the Windows Hook. Please correct feihu:
1. Refer to WINBASE. h of wince. There are three types of resources available under wince:
# Define wh_journalrecord 0
# Define wh_journalplayback 1
# Define wh_keyboard_ll 20
The most useful one is the keyboard hook. It is defined as 20 in wince, rather than 14 in windows. Therefore, pay attention to the call.
2. The keyboardproc of the keyboard hook callback function does not contain the following parameters:
Lresult callback keyboardproc (
Int code, // hook code
Wparam, // virtual-key code
Lparam // keystroke-message information
According to the instructions, wparam should store the virtual key information. However, in fact, the second of the three functions is used to indicate whether the key is pressed or popped up, and the third parameter lparam is the key information data actually stored. It stores a KBDLLHOOKSTRUCT struct pointer. This struct is defined as follows:
Typedef struct {
DWORD vkcode;
DWORD scancode;
DWORD flags;
DWORD time;
Ulong_ptr dwextrainfo;
} KBDLLHOOKSTRUCT;
The struct contains the real key information.
3. I can only use one keyboard hook. If I create two hooks for detection (whether using the same DLL or two different DLL), the first one will work normally, but the second one will report an error. The error ID is 31 error_gen_failure, that is
A device attached to the system is not functioning.
I don't know which expert has a solution?
4. Hooks have thread-level and global hooks, but I have only successfully tested the global hooks. They work very well, but the thread-level hooks are not yet successful.
5. Hook has a lot of functions, so we will use it to deal with the driver, and we will not talk about it much more.
The source code of the hook function is based on the source code of prathamesh s Kulkarni on the codeproject website, and adds the part for processing the key message. If the code is long, it will not be pasted. If necessary, you can communicate, for more information, see prathamesh s Kulkarni.