以下两个演员之间有什么区别(未注释演员表):
#include <iostream>
void Foo(){}
int main()
{
//reinterpret_cast<void*>(Foo)<<"\n"; //0x4014e0
std::cout<<reinterpret_cast<void**>(Foo)<<"\n"; //0x4014e0
std::cout<<&reinterpret_cast<void*&>(Foo)<<"\n";//0x4014e0
}
它们都打印完全相同的值。我无法找出差异。但是,它在以下代码段中有所不同:
template<typename U>
void DetourFunction(void** OriginalFunction, U HookFunction)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(OriginalFunction, reinterpret_cast<void*>(HookFunction));
DetourTransactionCommit();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
}
int main()
{
DetourFunction(reinterpret_cast<void**>(Foo), MyAddress); //doesn't work & crashes
DetourFunction(&reinterpret_cast<void*&>(Foo), MyAddress); //works & does not crash
}
我问的原因是因为我试图模仿它:
template<typename T, typename U>
void DetourFunction(T OriginalFunction, U HookFunction)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&reinterpret_cast<void*&>(OriginalFunction), reinterpret_cast<void*>(HookFunction));
DetourTransactionCommit();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
}
int main()
{
DetourFunction(Foo, MyAddress); //seems to be equivalent to reinterpret_cast<void**>(); Crashes
}
再说一次,这两个演员之间的区别是什么?我如何模拟我的功能以便我可以做以下事情?
DetourFunction(SomeFunc, SomeOtherFunc);
答案 0 :(得分:1)
当你说
时&reinterpret_cast<void*&>(OriginalFunction)
你正在获取一个局部变量的地址,这不是你想要的,因为它会在DetourFunction
退出时消失。