MobileSubstrate:MSHookFunction示例 [英] MobileSubstrate: MSHookFunction example
问题描述
我正在尝试编写一个挂钩到C方法的MobileSubstrate插件。我试着编辑着名的ExampleHook,只需编写一个演示MSHook并将其挂钩在Initialize方法中。
这可能过于乐观,但不起作用。但我无法找到MSHookFunction()的简单示例。互联网上几乎没有关于此的信息。我可能会误解MSHookFunction的整个概念。
I am trying to write a MobileSubstrate plugin which hooks into a C-method. I tried to edit the famous "ExampleHook", by just writing a demo MSHook and hook it in the Initialize method. This is probably too optimistic and it doesn't work. But I cannot find anywhere a simple example of a MSHookFunction(). There is barely information about this on the Internet. It might be possible I misunderstood the whole concept of MSHookFunction.
请问,有人可以用一些示例代码帮助我吗?我非常感谢任何帮助。
Please, can anybody help me out with a little example code? I would deeply appreciate any help.
祝你好运,
Marc Backes
Best regards, Marc Backes
推荐答案
我发现你已经找到了这个,但我发布了这个答案,以帮助其他人可能需要这个。
I realize you have found this, but I am posting this answer to help whoever else may be needing this.
一个简单的例子可以在iPhone Dev Wiki上的 MobileSubstrate文章,以及项目中的一个实际示例是 User Agent Faker 的此位。
A simple example can be found at the MobileSubstrate article on the iPhone Dev Wiki, and an actual example of this in a project is at this bit of User Agent Faker.
但没有实际解释,答案是什么?因此,我们走了!
But what is an answer without an actual explanation? Therefore, here we go!
void MSHookFunction(void * function,void * replacement,void ** p_original);
是 MSHookFunction
的函数定义,这个函数会导致函数 X()
被<$插入例如,c $ c> Y()。
void MSHookFunction(void* function, void* replacement, void** p_original);
is the function definition for MSHookFunction
, the magic function which causes your function X()
to be interposed by Y()
, for instance.
也就是说,当一个程序通常会调用 X( )
,该呼叫将被重定向到 Y()
。这几乎是函数插入的基本解释。
That is, when a program commonly would call X()
, the call will be redirected to Y()
instead. This is pretty much a basic explanation of function interposing.
现在,参数是什么,它们的用处是什么?
Now, what are the parameters, and their usefulness?
-
function
是指向要插入的函数的函数指针。在我们的快速解释中,这将是指向X()
的函数指针。 -
replacement
是一个函数指针,指向要插入的函数function
。在我们的快速解释中,这将是指向Y()
的函数指针。 -
p_original
是一个指向函数指针的指针,从现在开始将指向函数
过去。
function
is a function pointer to the function you want to interpose. That would be a function pointer toX()
, in our quick explanation.replacement
is a function pointer to the function you wantfunction
to be interposed with. In our quick explanation, that would be a function pointer toY()
.p_original
is a pointer to a function pointer, which from now on will point to whatfunction
used to be.
原因很简单:如果你打算修改行为但不压制它,你仍然需要调用什么 X()
以前是。但是对 X()
的常见调用将无法正常工作,因为它将结束调用 Y()
而不是默认函数
。
The reason this is there is simple: If you intend to modify behavior, but not suppress it, you'll still need to call what X()
used to be. But a common call to X()
wouldn't work as intended, as it would end calling Y()
instead of the default function
.
因此,你有一个函数指针来调用 X( )
好像它没有插入。
Therefore, you have a function pointer to call X()
as if it wasn't interposed.
现在,解释devwiki示例:
Now, explaining the devwiki example:
static void (*original_CFShow)(CFTypeRef obj); // a function pointer to store the original CFShow().
void replaced_CFShow(CFTypeRef obj) { // our replacement of CFShow().
printf("Calling original CFShow(%p)...", obj);
original_CFShow(obj); // calls the original CFShow.
printf(" done.\n");
}
...
// hook CFShow to our own implementation.
MSHookFunction(CFShow, replaced_CFShow, &original_CFShow);
// From now on any call to CFShow will pass through replaced_CFShow first.
...
CFShow(CFSTR("test"));
在这里,我们:
- 将指针传递给
CFShow
,我们想要更改默认行为的函数为函数
参数。 - 将指针传递给我们刚刚创建的函数,
replacement_CFShow
作为替换
参数。也就是说,默认情况下,只要调用CFShow
,就会调用replacement_CFShow
。 - 我们将指向
original_CFShow
函数指针的指针作为p_original
参数传递。由于我们仍然希望CFShow
在我们的替换函数中完成,我们称之为。
- Pass a pointer to
CFShow
, the function we want to change default behavior from as thefunction
parameter. - Pass a pointer to the function we just created,
replaced_CFShow
as thereplacement
parameter. That is, wheneverCFShow
would be called by default,replaced_CFShow
will be called instead. - We pass a pointer to the
original_CFShow
function pointer as thep_original
parameter. Since we still want the thingsCFShow
still does by itself to be done inside our replacement function, we call it.
这篇关于MobileSubstrate:MSHookFunction示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!