请问VB如何写HOOK?

CTBOK 2003-10-16 02:54:51
我是要拦截键盘输入,让某些键的输入到达不到特定的进程,因为有一个后台程序的F5热键与IE的刷新相冲突,于是想到了HOOK,截取F5,让F5这个热键只对IE起作用,不对那个台后程序起作用,应该怎样写呢?谢谢
...全文
113 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
knikcn 2003-10-21
  • 打赏
  • 举报
回复
哈哈,我上次就收藏了,关于这本书-Windows 95: A Developer's Guide (Jeffrey Richter着),我也找到了,可惜是个繁体版的。不过讲得非常不错,我想应该作为进阶的必备书。
CTBOK 2003-10-21
  • 打赏
  • 举报
回复
还是不懂,写不出任何程序...
lanman 2003-10-17
  • 打赏
  • 举报
回复
yunfeng007 2003-10-17
  • 打赏
  • 举报
回复
vb能做的全局Hook就是这个键盘hook了。而象api Hook的,vb就做不了了。
captainivy 2003-10-17
  • 打赏
  • 举报
回复
收藏一下
cjlong 2003-10-17
  • 打赏
  • 举报
回复
××××××××××××××××××
为了女友,要做MVP

Hook简介

来源:cww

Hook这个东西有时令人又爱又怕,Hook是用来拦截系统某些讯息之用,例如说,我们想
让系统不管在什麽地方只要按个Ctl-B便执行NotePad,或许您会使用Form的KeyPreview
,设定为True,但在其他Process中按Ctl-B呢?那就没有用,这是就得设一个Keyboard
Hook来拦截所有Key in的键;再如:MouseMove的Event只在该Form或Control上有效,如镇
果希望在Form的外面也能得知Mouse Move的讯息,那只好使用Mouse Hook来栏截Mouse
的讯息。再如:您想记录方才使用者的所有键盘动作或Mosue动作,以便录巨集,那就
使用JournalRecordHook,如果想停止所有Mosue键盘的动作,而放(执行)巨集,那就
使用JournalPlayBack Hook;Hook呢,可以是整个系统为范围(Remote Hook),即其他
Process的动作您也可以拦截,也可以是LocalHook,它的拦截范围只有Process本身。
Remote Hook的Hook Function要在.Dll之中,Local Hook则在.Bas中。
在VB如何设定Hook呢?使用SetWindowsHookEx()

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

idHook代表是何种Hook,有以下几种
Public Const WH_CALLWNDPROC = 4
Public Const WH_CALLWNDPROCRET = 12
Public Const WH_CBT = 5
Public Const WH_DEBUG = 9
Public Const WH_FOREGROUNDIDLE = 11
Public Const WH_GETMESSAGE = 3
Public Const WH_HARDWARE = 8
Public Const WH_JOURNALPLAYBACK = 1
Public Const WH_JOURNALRECORD = 0
Public Const WH_KEYBOARD = 2
Public Const WH_MOUSE = 7
Public Const WH_MSGFILTER = (-1)
Public Const WH_SHELL = 10
Public Const WH_SYSMSGFILTER = 6

lpfn代表Hook Function所在的Address,这是一个CallBack Fucnction,当挂上某个
Hook时,我们便得定义一个Function来当作某个讯息产生时,来处理它的Function
,这个Hook Function有一定的叁数格式

Private Function HookFunc(ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long ) As Long

nCode 代表是什麽请况之下所产生的Hook,随Hook的不同而有不同组的可能值
wParam lParam 传回值则随Hook的种类和nCode的值之不同而不同。

因这个叁数是一个 Function的Address所以我们固定将Hook Function放在.Bas中,
并以AddressOf HookFunc传入。至於Hook Function的名称我们可以任意给定,不一
定叫 HookFunc

hmod 代表.DLL的hInstance,如果是Local Hook,该值可以是Null(VB中可传0进去),
而如果是Remote Hook,则可以使用GetModuleHandle(".dll名称")来传入。

dwThreadId 代表执行这个Hook的ThreadId,如果不设定是那个Thread来做,则传0(所以
一般来说,Remote Hook传0进去),而VB的Local Hook一般可传App.ThreadId进去

值回值 如果SetWindowsHookEx()成功,它会传回一个值,代表目前的Hook的Handle,
这个值要记录下来。

因为A程式可以有一个System Hook(Remote Hook),如KeyBoard Hook,而B程式也来设一
个Remote的KeyBoard Hook,那麽到底KeyBoard的讯息谁所拦截?答案是,最後的那一个
所拦截,也就是说A先做keyboard Hook,而後B才做,那讯息被B拦截,那A呢?就看B的
Hook Function如何做。如果B想让A的Hook Function也得这个讯息,那B就得呼叫
CallNextHookEx()将这讯息Pass给A,於是产生Hook的一个连线。如果B中不想Pass这讯息
给A,那就不要呼叫CallNextHookEx()。

Declare Function CallNextHookEx Lib "user32" Alias "CallNextHookEx" _
(ByVal hHook As Long, _
ByVal ncode As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

hHook值是SetWindowsHookEx()的传回值,nCode, wParam, lParam则是Hook Procedure
中的三个叁数。

最後是将这Hook去除掉,请呼叫UnHookWindowHookEx()

Declare Function UnhookWindowsHookEx Lib "user32" Alias "UnhookWindowsHookEx" _
(ByVal hHook As Long) As Long

hHook便是SetWindowsHookEx()的传回值。此时,以上例来说,B程式结束Hook,则换A可
以直接拦截讯息。


KeyBoard Hook的范例

Hook Function的三个叁数

nCode wParam lParam 传回值
=========== ========================== ============== ================
HC_ACTION 表按键Virtual Key 与WM_KEYDOWN同 若讯息要被处理传0
或 反之传1
HC_NOREMOVE


Public hHook as Long

Public Sub UnHookKBD()
If hnexthookproc <> 0 Then
UnhookWindowsHookEx hHook
hHook = 0
End If
End Sub

Public Function EnableKBDHook()
If hHook <> 0 Then
Exit Function
End If
hhook = SetWindowsHookEx(WH_KEYBOARD, AddressOf _
MyKBHFunc, App.hInstance, App.ThreadId)
End Function

Public Function MyKBHFunc(ByVal iCode As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
MyKBHfunc = 0 '表示要处理这个讯息

If wParam = vbKeySnapshot Then '侦测 有没有按到PrintScreen键
MyKBHFunc = 1 '在这个Hook便吃掉这个讯息
End If
Call CallNextHookEx(hHook, iCode, wParam, lParam) '传给下一个Hook
End Function

至於其他的 Hook的详细资料与nCode,wParam, lParam的意义,请查Win32 Help
或Windows 95: A Developer's Guide (Jeffrey Richter着)(中译本:
基峰 李书良译 侯俊杰总监 Windows95程式设计指南)

CTBOK 2003-10-17
  • 打赏
  • 举报
回复
不太懂,有没有VB的代码呀~~~
sword281 2003-10-16
  • 打赏
  • 举报
回复
要用winapi函数
KeyboardProc
SetWindowsHookEx 建立hook
CallNextHookEx
UnhookWindowsHookEx 撤消hook
findwindows找到后台程序

对后台程序进行wm_keydown事件拦截,当键盘按下f5时发送至ie窗口
下面是一段vc代码可仿造写vb代码
F1CDHelp.exe Uses Keyboard Hook to Access Help
ID: Q83233



--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Windows Software Development Kit (SDK) 3.1

--------------------------------------------------------------------------------


SUMMARY
F1CDHELP is a file in the Microsoft Software Library that demonstrates how an application can implement context sensitive help for the dialog boxes provided by the dynamic-link library (DLL) COMMDLG.DLL (the common dialog box DLL). When the user presses the F1 key, the application calls Windows Help.

The recommended user interface for an application to provide access to Windows Help is through the F1 key. This article describes a technique that can be used with the common dialog box DLL to bring up context sensitive help for each dialog box. The sample uses the SetWindowsHookEx() function to set a task-specific keyboard hook, which monitors keyboard input and responds to the F1 key.

Because this application uses a task-specific hook, the hook function code resides in the application's .EXE file and is not required to be in a fixed code page in a DLL. (The filter function for each system- wide hook must be in a DLL.)



MORE INFORMATION
The following files are available for download from the Microsoft Download Center. Click the file names below to download the files:


F1CDHelp.exe

For more information about how to download files from the Microsoft Download Center, please visit the Download Center at the following Web address

http://www.microsoft.com/downloads/search.asp
and then click How to use the Microsoft Download Center.

The technique used in this article is straightforward and minimizes the number of global variables required. The application installs the keyboard hook and calls the RegisterWindowMessage() function to define a help message. When an application registers the help message, the common dialog box DLL notifies the application each time the user chooses the Help button in one of the common dialog boxes. The DLL sends the help message to the window procedure of the dialog box's parent window.

When the keyboard hook function detects that the F1 key is pressed, it posts a WM_COMMAND message to the appropriate window procedure. In the F1CDHELP example, the message is posted either to the main window (when no dialog box is displayed) or to the dialog box's window procedure. If the message is posted to one of the common dialog boxes, wParam is set to pshHelp; the application simulates choosing the Help button in the common dialog box. Otherwise, wParam is set to IDM_HELPCONTENTS; the application simulates selecting a menu item in the application.

The following code demonstrates installing the hook and registering the help message in response to a WM_CREATE message:

case WM_CREATE:
// Install the keyboard hook

lpfnKbrdHook = MakeProcInstance((FARPROC)KeyboardHook, ghInst);
ghKbrdHook = SetWindowsHookEx(WH_KEYBOARD, lpfnKbrdHook,
ghInst, GetCurrentTask());

// Register the help message. The common dialog box DLL sends this
// message when the user chooses the Help file in a common
// dialog box.
gwHelpMsg = RegisterWindowMessage((LPSTR)HELPMSGSTRING);
break;
The following code demonstrates removing the keyboard hook in response to a WM_DESTROY message:

case WM_DESTROY:
UnhookWindowsHookEx(ghKbrdHook);
break;
The hook function receives notification about the F1 key and posts a message as appropriate:

DWORD FAR PASCAL KeyboardHook(int iCode, WPARAM wParam, LPARAM lParam)
{
if (iCode < 0 || iCode != HC_ACTION)
return CallNextHookEx(ghKbrdHook, iCode, wParam, lParam);

if (wParam == VK_F1)

// If this is a repeat or the key is being released, ignore it.
if (lParam & 0x80000000 || lParam & 0x40000000)
return CallNextHookEx(ghKbrdHook, iCode, wParam, lParam);
else
{
if (IsWindowEnabled(ghWnd)) // F1 pressed in main window?
PostMessage(ghWnd, WM_COMMAND, IDM_HELPCONTENTS, 0L);
else // F1 pressed in a dialog box
PostMessage(GetActiveWindow(), WM_COMMAND, pshHelp, 0L);
}

return CallNextHookEx(ghKbrdHook, iCode, wParam, lParam);
}
The second PostMessage() call above is executed when the user requests help in a specific common dialog box. This simulates choosing the Help button in the dialog box. Because the application registered the help message (during the processing of its WM_CREATE message), the common dialog box DLL will send the gwHelpMessage to the parent window procedure. An application can process this message as follows:

default:
if (message == gwHelpMsg) // Help requested in a common dialog
// box
{
// The lParam points to an OPENFILENAME or a CHOOSECOLOR data
// structure. The application can differentiate between them by
// checking the structure's size, which is in the first four
// bytes (a DWORD) of the structure. This allows the application
// to display different help for each of the common dialog boxes.

dwStructSize = (DWORD)(*(LPDWORD)lParam);

switch (dwStructSize)
{
case sizeof(OPENFILENAME):
MessageBox((HWND)wParam, "Help requested for OpenFile",
gszAppName, MB_OK);
break;

case sizeof(CHOOSECOLOR):
MessageBox((HWND)wParam, "Help requested for ChooseColor"
gszAppName, MB_OK);
break;

default:
break;
}
break;
}
else // Not a help message
return DefWindowProc(hWnd, message, wParam, lParam);








--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧