I am developing one simple application which is reading the keystrokes from the OS. I have used API "SetWindowsHookEx" to read the keystroke data.
Currently data is read by the hook as well sent into the OS or application.
I want to stop this data going into the OS or other applications. Is开发者_运维百科 there any way to stop the data going into the windows 7 OS after read it using "SetWindowsHookEx"?
Generally, to prevent any message to move to be sent to its target, don't call CallNextHookEx from the hook callback function. By default, hook callback calls CallNextHookEx.
Bee careful with this feature and stop only some specific messages. For example, sometimes this function is called to disable some standard keys like Alt+F4, Alt+Tab etc.
If you are using a "normal" Keyboard Hook you will find your answer on https://msdn.microsoft.com/en-us/ms644984.aspx:
If code is less than zero, the hook procedure must return the value returned by CallNextHookEx.
If code is greater than or equal to zero, and the hook procedure did not process the message, it is highly recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that have installed WH_KEYBOARD hooks will not receive hook notifications and may behave incorrectly as a result. If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure.
If you are using a low level keyboard hook its written on https://msdn.microsoft.com/en-us/ms644985.aspx:
If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
If nCode is greater than or equal to zero, and the hook procedure did not process the message, it is highly recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that have installed WH_KEYBOARD_LL hooks will not receive hook notifications and may behave incorrectly as a result. If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure.
Summary:
If you want to intercept a message - yust don't call CallNextHookEx and return a non-zero value prevent it from being passed down the chain.
精彩评论