Going deeply out of Hooks (之)

zhaozj2021-02-16  48

Establish mouse keyboard operation playback with VB

Many teaching software or system monitoring software automatically records operational operations of playback of the user's input text or click button, this function is used

Windows hook function. This article describes how to implement record and playback of mouse keyboard operations by using VB.

Windows provides an API function setWindowshookex to create a hook, you can add a program to your hook chain to monitor Windows

Message, function syntax is:

Public Declare Function SetWindowsHookex Lib "User32" _

Alias ​​"setwindowshookexa" _

(Byval IDHOOK AS Long, _

BYVAL LPFN As Long, _

Byval HMOD As Long, _

BYVAL DWTHREADID AS Long) As long

Where parameter IDHOOK specifies the type of monitoring function. It can be seen from the Windows MSDN Help, and the SETWINDOWSHOKEX function provides 15 different

The message monitoring type, where we will use WH_JournalRecord and WH_JournalPlayback to monitor keyboards and mouse operations. Parameter LPFN Specify

The interest function, after the corresponding message is generated, the system calls the function and passes the message value to the function. The general form of functions is:

HookProc (Code: INTEGER; WPARAM: WPARAM; LPARAM: LPARAM): LRESULT STDCALL;

Where Code is systemic indication mark, WPARAM, and LPARAM are additional parameters, depending on different messaging types. As long as it is established in the program

A function can handle the message again through the SetWindowsHookex function to add it to the message monitoring chain.

You need to call to provide UnHookWindowsHooKex when you do not need to monitor the system message.

The wh_journalRecord and the wh_journalplayback type are two opposite hook types, the former gets the mouse, the keyboard action message, the latter returns the mouse

Tag keyboard message. So in the program we need to build two message functions, one for record mouse keyboard operations and saves them into an array, another

The saved operation will return to the system playback.

Below is a specific program implementation: first establish a new project, add three CommandButton controls in Form1 to control the message hook,

You can add a number of Command or TextBox controls to verify the effects of operation playback. Then add a module file in the project, add the following in the module

Definitions and code:

Option expedition

Public Type EventMSG

Message as long

Paraml as long

Paramh as long

Time as long

HWND As Long

End Type

Public Declare Function CallNexthookex Lib "User32" _

(Byval HHOOK As Long, _

Byval ncode as long, _

Byval wparam as long, _

BYVAL LPARAM AS Long AS Long

Public Declare Function SetWindowsHookex Lib "User32" _

Alias ​​"setwindowshookexa" _

(Byval IDHOOK AS Long, _

BYVAL LPFN As Long, _

Byval HMOD As Long, _

BYVAL DWTHREADID AS Long) As long

Public Declare Sub CopyMemoryt2h LIB "Kernel32" _Alias ​​"RTLMoveMemory" _

(ByVal Dest As Long, _

Source as Eventmsg, _

BYVAL Length As long

Public Declare Sub CopyMemoryH2T LIB "Kernel32" _

Alias ​​"RTLmoveMemory" _

(Dest as Eventmsg, _

Byval Source as long, _

BYVAL Length As long

Public Declare Function UnHookWindowsHookex Lib "User32" _

(BYVAL HHOOK AS Long) As Long

Public const wh_journalplayback = 1

Public const wh_journalrRecord = 0

Public const hc_sysmodaloff = 5

Public const hc_sysmodalon = 4

Public const hc_skip = 2

Public const hc_getnext = 1

Public const hc_action = 0

Public Eventarr (1000) as EventMSG

Public EventLog As Long

Public Playlog As Long

Public HHOOK As Long

Public HPLAY As Long

Public Recok As Long

Public Canplay As Long

Public BDELAY As Boolean

Public Function HookProc (Byval Icode As Long, Byval WParam As Long, _

BYVAL LPARAM AS Long AS Long

Dim Result As Long

Recok = 1

Result = 0

If ICODE <0 THEN 'ICODE is less than 0 must be called directly to the next message hook function

Result = CallNexthookex (HHOOK, ICODE, WPARAM, LPARAM)

Elseif iCode = HC_SYSMODALON TEN 'is not allowed

Recok = 0

Elseif icide = hc_sysmodaloff the 'allowed record

Recok = 1

Elseif ((RecoK> 0) and (iCode = HC_Action).

'Put the message record in the record queue

CopyMemoryH2T Eventarr (Eventlog), LPARAM, LEN (Eventarr (EventLog))

EventLog = EventLog 1

If EventLog> = 1000 THEN

'When the record is more than 1000, release the message hook

UnHookWindowshookex HHOOK

END IF

END IF

HookProc = Result

END FUNCTION

Public Function PlaybackProc (Byval Icode As Long, Byval WPARAM As Long, _

BYVAL LPARAM AS Long AS Long

Dim Result As Long

Canplay = 1

Result = 0

If iCode <0 THEN 'ICODE is less than 0 must directly call the next message hook function Result = CallNexThookex (HPLAY, ICODE, WPARAM, LPARAM)

Elseif icode = hc_sysmodalon kiln 'is not allowed to play back

Canplay = 0

Elseif icode = hc_sysmodaloff the 'allowed playback

Canplay = 1

Elseif (CANPLAY = 1) and (iCode = HC_GetNext) THEN

IF bdlay the

BDELAY = FALSE

Result = 50

END IF

'Remove the message from the record queue and give the LPARAM pointer to the EventMSG area

CopyMemoryt2h LParam, Eventarr (Playlog), Len (Eventarr (EventLog)

Elseif (CANPLAY = 1) and (iCode = HC_SKIP) "

BDELAY = TRUE

Playlog = playlog 1

END IF

IF playlog> = EventLog then

UnHookWindowshookex Hplay

END IF

PlayBackProc = Result

END FUNCTION

Add the following code in the FORM1 code window:

Option expedition

Private submmand1_click ()

EventLog = 0

HHOOK = SETWINDOWSHOKEX (Wh_JournalrRecord, Addressof Hookproc, _

App.hinstance, 0)

Command2.enabled = TRUE

Command1.enabled = false

End Sub

Private sub fascist2_click ()

UnHookWindowshookex HHOOK

hHOOK = 0

Command1.enabled = TRUE

Command2.enabled = false

Command3.enabled = TRUE

End Sub

Private submmand3_click ()

Playlog = 0

HPLAY = SETWINDOWSHOKEX (WH_Journalplayback, Addressof PlaybackProc, _

App.hinstance, 0)

Command3.enabled = false

End Sub

Private sub flow_load ()

Command1.caption = "record"

Command2.caption = "Stop"

Command3.caption = "Playback"

Command2.enabled = false

Command3.enabled = false

End Sub

Run the program, click the "Record" button, then enter some text in TextBox or move the cursor on the window and press the "Stop" button to stop the message.

Record, then press the "Playback" button, you can see that the operation of the mouse keyboard is not disconnected by the slightest.

From the above program, you can see: A mouse keyboard message hook can be created via WH_JournalRecord, and when each mouse keyboard message is generated

The hook function is called. Save messages in the message event queue in the hook function. Then use the WH_JournalPlayback to create a message playback hook, when each system can play back the message, the hook function is called, and the original record will be removed from the message queue in the hook function.

This enables record and playback of mouse keyboard operation.

转载请注明原文地址:https://www.9cbs.com/read-23189.html

New Post(0)