After the clarification from OP, I can see that my original suggestion is applicable.
Instead of un-hooking the event handler temporarily, hook and unhook it only once per application lifetime. In the middle of lifetime, if temporary deactivation of hooked activity is required, preserve some status flag and check its value to see if the functionality is enabled or disabled. You also may need to assign a value to the flag in the interlocked manner due to multi-threading nature of this task. Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684122%28v=vs.85%29.aspx[
^].
static LONG enableCount = 0;
LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode,WPARAM wParam,LPARAM lParam) {
if (enableCount < 1) return;
}
void Enable() {
InterlockedIncrement(&enableCount);
}
void Disable() {
InterlockedDecrement(&enableCount);
}
This way, the calls to
Enable
and
Disable
could be nested and performed in any thread; which is the best to do with try-finally statement. It's also good to use the RAII technique. Please see:
http://en.wikipedia.org/wiki/RAII[
^],
http://stackoverflow.com/questions/7779652/try-catch-finally-construct-is-it-in-c11[
^].
—SA