C++通过功能模板中的通过功能指针
我正在尝试将功能指针传递在模板中,然后在ASM代码中使用它:
template <auto T>
_declspec(naked) void Test()
{
__asm
{
lea eax, T
jmp [eax]
}
}
int main()
{
Test<MessageBoxA>();
Test<Sleep>();
}
我知道执行时裸函数会崩溃,但我简化了代码以显示我要实现的目标。
该代码的问题是,一旦编译,如果您以拆卸器中的汇编代码查看,它将看起来像这样:
lea eax, 0
jmp [eax]
它在EAX中存储0。相反,它应将 MessageBoxa
地址存储在EAX中。
我不知道我在做错事还是编译器失败。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是组装专家,而是创建
static constexpr void*
将设置为非类型模板参数值似乎有能力:生成
示例可以在编译器资源管理器上播放: https://godbolt.org/z/f1d7daa49
I'm not an assembly expert, but creating a
static constexpr void*
set to the non-type template parameter value seems to do the trick:generates
The example can be played with on compiler explorer: https://godbolt.org/z/f1d7daa49