代码之家  ›  专栏  ›  技术社区  ›  Facundo

在c上创建\uu cdecl to \uu thiscall包装函数++

  •  0
  • Facundo  · 技术社区  · 7 年前

    我需要动态创建一个函数,外部库可以使用 __cdecl 调用约定,然后将调用重定向到类上的方法,有效地充当 __thiscall 调用约定。

    主要的想法是( program1 )应该从外部应用程序接收函数指针( program2 ),将其打包为可以查询我们的对象( 程序1 )要知道 程序2 是否应该制作,然后将其传递给图书馆。

    我对此类课程的标题应该是什么样子有一个模糊的概念

    template <typename F, class C>
    class this_call_wrapper
    {
    public:
        // Creates a wrapper function that calls `operator()` on `object`
        // `operator()` should take the same arguments as `F`
        this_call_wrapper(const C* object);
        // Deallocates memory used by this and the wrapper
        ~this_call_wrapper();
        // Returns the pointer to the function wrapper
        F* get_wrapper();
    private:
        C* object;
        F* wrapper;
    };
    

    有没有提供类似功能的库?如果没有,我如何在C++中实现这一点?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Community CDub    5 年前

    我发现 libffcall 是解决这类问题的最合适的方法。在程序集/机器代码上构造闭包也是一种有效的选择,但使用 libffcall ,我认为您不会想弄乱前者,除非您对二进制文件有某种(非常严格的)大小限制。

    This is the final solution .