Use hooks on Windows Mobile

Source: Internet
Author: User

The Windows system obviously does not support Hook Technology. Some technical documents simply say that wince does not support hooks. Actually, not all. We can still get some useful interfaces from coredll. DLL to intercept some simple keyboard or mouse actions.

Or the code is the most convincing:

# Define wh_journalrecord 0
# Define wh_journalplayback 1
# Define wh_keyboard_ll 20

# Define hc_action 0

Typedef lresult (callback * hookproc) (INT code, wparam, lparam );
Typedef hhook (winapi * _ setwindowshookexw) (INT, hookproc, hinstance, DWORD );
Typedef lresult (winapi * _ callnexthookex) (hhook, Int, wparam, lparam );
Typedef lresult (winapi * _ unhookwindowshookex) (hhook );

Static _ setwindowshookexw sethook;
Static _ unhookwindowshookex unhookhook;
Static _ callnexthookex callnexthook;

Hinstance g_hhookapidll = NULL;
Hhook g_hkbdhook = NULL;
Hhook g_hmousehook = NULL;

Lresult callback kbdhookcallback (INT ncode, wparam, lparam );
Lresult callback mousehookcallback (INT ncode, wparam, lparam );

Bool activatehook (hinstance, hookproc hookcallback );
Bool deactivatehook ();

Bool activatehook (hinstance, hookproc)
{
Sethook = NULL;
Callnexthook = NULL;
Unhookhook = NULL;

Eventmsg MSG = {hc_action };
G_hmousehook = qasetwindowsjournalhook (wh_journalrecord, mousehookcallback, & MSG); // This is for the mouse

If (null = g_hmousehook)
{
Return false;
}

G_hhookapidll = loadlibrary (_ T ("coredll. dll "));
If (null = g_hhookapidll)
{
Return false;
}

Sethook = (_ setwindowshookexw) getprocaddress (g_hhookapidll, _ T ("setwindowshookexw "));
If (null = sethook)
{
Return false;
}

G_hkbdhook = sethook (wh_keyboard_ll, hookproc, hinstance, 0 );
If (null = g_hkbdhook)
{
Return false;
}

Callnexthook = (_ callnexthookex) getprocaddress (g_hhookapidll, _ T ("callnexthookex "));
If (null = callnexthook)
{
Return false;
}

Unhookhook = (_ unhookwindowshookex) getprocaddress (g_hhookapidll, _ T ("unhookwindowshookex "));
If (null = unhookhook)
{
Return false;
}

Return true;
}

Bool deactivatehook ()
{
If (g_hmousehook! = NULL)
{
Qaunhookwindowsjournalhook (wh_journalrecord );
G_hmousehook = NULL;
}

If (g_hkbdhook! = NULL)
{
Unhookhook (g_hkbdhook );
G_hkbdhook = NULL;
}

If (g_hhookapidll! = NULL)
{
Freelibrary (g_hhookapidll );
G_hhookapidll = NULL;
}

Return true;
}

Lresult callback kbdhookcallback (INT ncode, wparam, lparam)
{
If (ncode> = hc_action)
{
If (wparam = wm_keyup)
{
DWORD dwkey = (KBDLLHOOKSTRUCT *) lparam)-> vkcode;

If (dwkey = vk_up | dwkey = vk_down | dwkey = vk_left | dwkey = vk_right)
{
// Do something
}
}
}

Return callnexthook (g_hkbdhook, ncode, wparam, lparam );
}

Lresult callback mousehookcallback (INT ncode, wparam, lparam)
{
If (ncode> = hc_action)
{
Peventmsgmsg PMSG = (peventmsgmsg) lparam;
If (PMSG)
{
If (PMSG-> message = wm_lbuttondown | PMSG-> message = wm_lbuttonup)
{
// Do something
}
}
}

Return callnexthook (g_hmousehook, ncode, wparam, lparam );
}

If a hook is enabled in a program, other programs cannot use the hook.

We recommend that you use this function with caution because it is not publicly available by Microsoft and may cause potential problems. In addition, it is said that wm6.5 or above does not support mouse hooks.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.