C# C++ 互操作回调 [英] C# C++ Interop callback

查看:38
本文介绍了C# C++ 互操作回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在修改 C# 到 C++ 的互操作,特别是设置一个从 C++ DLL 调用的回调函数.

I have recently been tinkering around with C# to C++ interop, in particularly setting up a callback function which is called from the C++ DLL.

namespace TomCSharpDLLImport
{
    class Program
    {
        public delegate void TomDelegate(int a, int b);

        [DllImport("TomDllNative.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void GetData();

        [DllImport("TomDllNative.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetCallback(TomDelegate aCallback);

        static void Main(string[] args)
        {
            TomDelegate lTD = new TomDelegate(Program.TomCallback);

            SetCallback(lTD); //Sets up the callback

            int thread = Thread.CurrentThread.ManagedThreadId;

            GetData(); //This calls the callback in unmanaged code

            while (true) ;
        }

        //Callback function which is called from the unmanaged code
        public static void TomCallback(int a, int b)
        {
            Console.WriteLine("A: {0} B: {1}", a, b);
            int thread = Thread.CurrentThread.ManagedThreadId;
        }
    }
}

我的问题是,当程序控制进入 TomCallback 函数时,我期望它然后命中 Main 中的 while(true) 循环.然而,程序只是退出.我无法完全理解这种行为,我的一部分认为这符合预期,但我的一部分会期望它继续在主要.

The question I have is that, when the program control comes into the TomCallback function, I was expecting it to then hit the while(true) loop in Main. However instead the program just exits. I can't quite get my head round the behaviour, part of me imagines this is as expected but part of me would have expected it to continue on in main.

我所期待的......

What I was expecting...

  1. 调用 GetData() 函数
  2. GetData 函数调用回调
  3. 回调函数返回GetData
  4. GetData 返回 main()

然而这并不完全正确.

有人愿意解释一下发生了什么吗.

Would someone be kind enough to explain what happens.

为了节省空间,我没有发布非托管代码,但是如果需要,我很乐意发布

In order to save space I haven't posted the unmanaged code, however if it is needed i'm happy to post

我打开了非托管调试(完全忘了这样做),现在我看到了崩溃..

I turned on Unmanaged debugging (totally forgot to do this) and now I see the crash..

运行时检查失败 #0 - ESP 的值未在函数调用中正确保存.这通常是由于使用一种调用约定声明的函数与使用不同调用约定声明的函数指针调用的结果.

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

本机代码,因为这是崩溃的地方

Native code as this is where crash is

#include "stdafx.h"
typedef void (*callback_function)(int, int);

extern "C" __declspec(dllexport) void SetCallback(callback_function aCallback);
extern "C" __declspec(dllexport) void GetData();

callback_function gCBF;

__declspec(dllexport) void SetCallback(callback_function aCallback)
{
    gCBF = aCallback;
}

__declspec(dllexport) void GetData()
{
    gCBF(1, 2);
}

推荐答案

您必须使用

IntPtr Marshal.GetFunctionPointerForDelegate(Delegate d)

方法.

您使用 SetCallback() 和 System.Delegate 作为参数是错误的.

Your usage of SetCallback() with System.Delegate as an argument is wrong.

制作

SetCallback(Marshal.GetFunctionPointerForDelegate(lTD));

并重新声明 SetCallback 为

and redeclare SetCallback as

/// StdCall is crucial here
[DllImport("TomDllNative.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void SetCallback(IntPtr aCallback);

这篇关于C# C++ 互操作回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆