va_arg阻止我在本机回调中调用托管委托

时间:2018-03-15 18:32:07

标签: callback c++-cli variadic-functions

在C ++ / CLI程序集中,我试图从本机回调中调用托管委托。我跟着Doc Brown's answer here,到目前为止我的实现看起来像这样:

原生回调 - 暂时忽略已注释掉的部分:

static ssize_t idaapi idb_callback(void* user_data, int notification_code, va_list va)
{
    switch (notification_code)
    {
        case idb_event::byte_patched:
        {
            //ea_t address = va_arg(va, ea_t);
            //uint32 old_value = va_arg(va, uint32);
            return IdaEvents::BytePatched(0, 0);
        }
        break;
    }
    return 0;
}

如上所示,我在静态类中实例化了此托管代理

public delegate int DatabaseBytePatchedHandler(int address, int originalValue);

private ref class IdaEvents
{
    static IdaEvents()
    {
        BytePatched = gcnew DatabaseBytePatchedHandler(&OnDatabaseBytePatched);
    }

    public: static DatabaseBytePatchedHandler^ BytePatched;

    private: static int OnDatabaseBytePatched(int address, int originalValue)
    {
        return 0;
    }
};

编译好。但代码是不完整的 - 请记住上面的本机回调中注释掉的部分?我实际上必须从传递给回调的va_list中检索值,并将这些值传递给我的托管委托:

            ea_t address = va_arg(va, ea_t);
            uint32 old_value = va_arg(va, uint32);
            return IdaEvents::BytePatched(address, old_value);

但是一旦我使用va_arg取消注释其中一行,我就无法再编译项目并检索以下错误,标记我调用托管代理的行:

C3821   'IdaEvents': managed type or function cannot be used in an unmanaged function
C3821   'IdaEvents::BytePatched': managed type or function cannot be used in an unmanaged function
C3821   'BytePatched': managed type or function cannot be used in an unmanaged function
C3821   'DatabaseBytePatchedHandler::Invoke': managed type or function cannot be used in an unmanaged function
C3642   'int DatabaseBytePatchedHandler::Invoke(int,int)': cannot call a function with __clrcall calling convention from native code
C3175   'DatabaseBytePatchedHandler::Invoke': cannot call a method of a managed type from unmanaged function 'idb_callback'

这让我很困惑。为什么编译器会在我尝试使用va_arg时突然出现问题?即使是没有任何分配的单行也会弹出此错误。

我觉得这里太天真了吗?我显然错过了一块拼图,并且非常感谢任何帮助我找到它的帮助。

0 个答案:

没有答案