gpt4 book ai didi

c - 来自地址的函数原型(prototype)

转载 作者:行者123 更新时间:2023-11-30 20:14:56 25 4
gpt4 key购买 nike

有没有办法通过地址作为原型(prototype)从内存中调用函数?这在 Windows 上运行得很好,但我不知道如何使其在 Linux 上运行,主要是因为 __cdecl 约定。有没有办法在linux上也能做到这一点? gcc 总是返回这个

error: function 'void MyFunction(int)' is initialized like a variable

Windows 代码:

void (WINAPIV *MyFunction)(int param) = (void (WINAPIV *)(int param))0x00000001;

void Print()
{
cout << "1" << endl;
}

#define __cdecl __attribute__((__cdecl__))

void (__cdecl *MyFunction)() = (void (__cdecl *)())&Print;

抱歉,这是我的错误,谢谢。

最佳答案

您到底想如何在 Linux/GCC 上做到这一点?这对我有用(刚刚删除了 WINAPIV 部分):

void (*MyFunction)(int param) = (void (*)(int param))0x00000001;
<小时/>

无论如何,顺便说一句,很多人说使用 typedef 使语法更容易,即:

typedef void (*MyFunctionType)(int param);
MyFunctionType myFunction = (MyFunctionType)0x00000001;

关于c - 来自地址的函数原型(prototype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23281798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com