C# 编组回调

发布于 2024-10-21 05:52:30 字数 1445 浏览 3 评论 0原文

我正在尝试对结构中的 c 回调进行编组。我很确定我的一切都是正确的,但是当使用我的 C# 示例时,我没有收到事件,而当使用 C++ 时,我确实收到了事件。

这是 C#

class Program
{
    [DllImport("Some.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
    public static extern int SetCallbacks(Callbacks callBack);


    static Callbacks Callback = new Callbacks { DataArrived = DataArrived, SendFailure = SendFailure };
    static void Main(string[] args)
    {
        SetCallbacks(Callback);

        Console.ReadLine();
    }

    static void DataArrived(uint id, IntPtr data)
    {

    }

    static void SendFailure(uint id, uint id2, IntPtr data)
    {

    }
}



[StructLayout(LayoutKind.Sequential)]
public struct Callbacks
{
    public DataArrived DataArrived;
    public SendFailure SendFailure;
}

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void DataArrived(uint id,   IntPtr data);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void SendFailure(uint id, uint id2, IntPtr ulpData);

这是来自 C 头文件。

struct callBacks
{
    void (*dataArriveNotif) (unsigned int,    void*);
    void (*sendFailureNotif) (unsigned int, unsigned int, void*);
}

int SetCallbacks(callBacks callBacks);

这是工作的c++。

struct callBacks;
callbacks.dataArriveNotif = &dataArriveNotif;
callbacks.sendFailureNotif = &sendFailureNotif;
SetCallbacks(callBacks);

I am trying to Marshall c call backs that are in a struct. I am pretty sure I have everything correct, but when using my C# example I don't get events, when using c++ I do get events.

Here is the C#

class Program
{
    [DllImport("Some.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
    public static extern int SetCallbacks(Callbacks callBack);


    static Callbacks Callback = new Callbacks { DataArrived = DataArrived, SendFailure = SendFailure };
    static void Main(string[] args)
    {
        SetCallbacks(Callback);

        Console.ReadLine();
    }

    static void DataArrived(uint id, IntPtr data)
    {

    }

    static void SendFailure(uint id, uint id2, IntPtr data)
    {

    }
}



[StructLayout(LayoutKind.Sequential)]
public struct Callbacks
{
    public DataArrived DataArrived;
    public SendFailure SendFailure;
}

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void DataArrived(uint id,   IntPtr data);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void SendFailure(uint id, uint id2, IntPtr ulpData);

This is from the C header file.

struct callBacks
{
    void (*dataArriveNotif) (unsigned int,    void*);
    void (*sendFailureNotif) (unsigned int, unsigned int, void*);
}

int SetCallbacks(callBacks callBacks);

Here is the working c++.

struct callBacks;
callbacks.dataArriveNotif = &dataArriveNotif;
callbacks.sendFailureNotif = &sendFailureNotif;
SetCallbacks(callBacks);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮凉 2024-10-28 05:52:30

与代表有关的一切实际上都是正确的。我在示例中稍微简化了情况。

public static extern int SetCallbacks(Callbacks callBack); 

实际上

public static extern int SetCallbacks(String[] array, Callbacks callBack);

字符串数组末尾有很多尾随 0。这使得回调结构全部为空。我放弃了尝试以正确的方式封送 string[] ,只是将其设为 Intptr ,一切都开始工作了。

Everything dealing with the delegate was actually correct. I simplified the senario a little bit in the example.

public static extern int SetCallbacks(Callbacks callBack); 

was actually

public static extern int SetCallbacks(String[] array, Callbacks callBack);

The string array had lots of trailing 0's at the end. Which made the callback struct all nulls. I gave up trying to marshal the string[] the correct way and just made it a Intptr and everything started working.

饮湿 2024-10-28 05:52:30

与昨天提出的这个问题非常相似...

PInvoke C#:函数将函数指针作为参数

so very similar to this question asked yesterday...

PInvoke C#: Function takes pointer to function as argument

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文