我最近在和一些人谈论我正在编写的程序时听到了“hook”这个词。我不确定这个术语到底意味着什么,尽管我从对话中推断钩子是一种函数类型。我寻找一个定义,但无法找到一个好的答案。有没有人能告诉我这个术语的一般含义,或者举个小例子来说明这个定义?


当前回答

A chain of hooks is a set of functions in which each function calls the next. What is significant about a chain of hooks is that a programmer can add another function to the chain at run time. One way to do this is to look for a known location where the address of the first function in a chain is kept. You then save the value of that function pointer and overwrite the value at the initial address with the address of the function you wish to insert into the hook chain. The function then gets called, does its business and calls the next function in the chain (unless you decide otherwise). Naturally, there are a number of other ways to create a chain of hooks, from writing directly to memory to using the metaprogramming facilities of languages like Ruby or Python.

钩子链的一个例子是MS Windows应用程序处理消息的方式。处理链中的每个函数要么处理消息,要么将其发送给链中的下一个函数。

其他回答

通常挂钩指的是Win32消息挂钩或Linux/OSX的等等物,但更一般的挂钩只是通知另一个对象/窗口/程序等,当指定的操作发生时,你想被通知。例如:让系统上的所有窗口在即将关闭时通知您。

作为一般规则,钩子有点危险,因为在不了解它如何影响系统的情况下这样做可能会导致不稳定或至少是意想不到的行为。在某些情况下,它也非常有用。例如:FRAPS使用它来确定应该在哪个窗口显示FPS计数器。

在一般意义上,“钩子”是让程序员查看和/或与/或改变系统/程序中已经发生的事情的东西。

例如,Drupal CMS为开发人员提供了钩子,允许他们在创建“内容节点”之后采取额外的操作。如果开发人员没有实现钩子,则按常规创建节点。如果开发人员实现了钩子,他们可以在创建节点时运行一些额外的代码。这段代码可以做任何事情,包括回滚和/或更改原始操作。它还可以做一些与节点创建完全无关的事情。

回调可以被认为是一种特定类型的钩子。通过在系统中实现回调功能,该系统允许您在操作完成后调用一些额外的代码。然而,钩子(作为一个通用术语)并不局限于回调。

另一个例子。有时Web开发人员会将元素上的类名和/或id称为钩子。这是因为通过在元素上放置ID/类名,他们可以使用Javascript修改该元素,或“钩入”到页面文档。(这是一个延伸的意思,但它是常用的,值得一提)

A chain of hooks is a set of functions in which each function calls the next. What is significant about a chain of hooks is that a programmer can add another function to the chain at run time. One way to do this is to look for a known location where the address of the first function in a chain is kept. You then save the value of that function pointer and overwrite the value at the initial address with the address of the function you wish to insert into the hook chain. The function then gets called, does its business and calls the next function in the chain (unless you decide otherwise). Naturally, there are a number of other ways to create a chain of hooks, from writing directly to memory to using the metaprogramming facilities of languages like Ruby or Python.

钩子链的一个例子是MS Windows应用程序处理消息的方式。处理链中的每个函数要么处理消息,要么将其发送给链中的下一个函数。

编程中的钩子是一种技术,它使用所谓的钩子来创建一个过程链作为事件处理程序。

简单的说:

钩子是在现有代码之前、之后或代替现有代码执行自定义代码(函数)的一种方法。例如,为了在继续正常登录过程之前执行验证码函数,可以编写一个函数“hook”到登录过程中。