如何在C#中使用回调访问struct C [英] How to access struct C with callback in C#

查看:101
本文介绍了如何在C#中使用回调访问struct C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要访问C dll。这个DLL有一个回调。

但是当调用回调时我有一个例外。



这是我的C dll描述



Hi,

I need to acces to a C dll. this dll has a callback.
But when the callback is called i've an exception.

This is the descriptive of my C dll

typedef struct am3_oem_device
{
   unsigned int number_sensor_width;
   unsigned int number_sensor_height;
   float sensor_surface_cm2;
   char* serial;
   char* am3_id;
   char* bootloader_firmware;
   char* micro_firmware;
   char* cpld_firmware;
   char* wifi_firmware;
   char* hardware_version;
   }AM3_OEM_DEVICE ;


  typedef struct am3_oem_device_manager
  {
      void (*device_has_connected)(AM3_OEM_DEVICE device_connected);
      void (*device_has_disconnected)(AM3_OEM_DEVICE device_disconnected);
      void (*device_status_update)(AM3_OEM_DEVICE device, unsigned int status);
      void (*device_acquisition_available)(AM3_OEM_DEVICE device, unsigned int* millisecond, unsigned int size);

  }*P_AM3_OEM_DEVICE_MANAGER,AM3_OEM_DEVICE_MANAGER;





我的尝试:



这个C#代码关联



What I have tried:

This the C# code associated

[StructLayout(LayoutKind.Sequential)]
public struct AM3_OEM_DEVICE
{
    public uint number_sensor_width;
    public uint number_sensor_height;
    public float sensor_surface_cm2;

    [MarshalAs(UnmanagedType.LPStr)]
    public StringBuilder serial;

    [MarshalAs(UnmanagedType.LPStr)]
    public StringBuilder am3_id;

    [MarshalAs(UnmanagedType.LPStr)]
    public StringBuilder bootloader_firmware;

    [MarshalAs(UnmanagedType.LPStr)]
    public StringBuilder micro_firmware;

    [MarshalAs(UnmanagedType.LPStr)]
    public StringBuilder cpld_firmware;

    [MarshalAs(UnmanagedType.LPStr)]
    public StringBuilder wifi_firmware;

    [MarshalAs(UnmanagedType.LPStr)]
    public StringBuilder hardware_version;
}
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void device_has_connected(AM3_OEM_DEVICE device_connected);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void device_has_disconnected(AM3_OEM_DEVICE device_connected);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void device_status_update(AM3_OEM_DEVICE device_connected, uint status);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void device_acquisition_available(AM3_OEM_DEVICE device_connected, out uint millisecond, uint size);


[StructLayout(LayoutKind.Sequential)]
public struct AM3_OEM_DEVICE_MANAGER
{
    private device_has_connected device_has_connected;
    private device_has_disconnected device_has_disconnected;
    private device_status_update device_status_update;
    private device_acquisition_available device_acquisition_available;

}





我的马歇尔码中肯定有错误。

你能帮我吗?

谢谢。



I've certainly an error in my marshall code.
Can you help me please?
Thanks.

推荐答案

我找到了解决方案



i've found a solution

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
       public delegate void device_has_connected(AM3_OEM_DEVICE device_connected);
       [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
       public delegate void device_has_disconnected(AM3_OEM_DEVICE device_connected);
       [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
       public delegate void device_status_update(AM3_OEM_DEVICE device_connected, uint status);
       [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
       public delegate void device_acquisition_available(AM3_OEM_DEVICE device_connected, ref uint millisecond, uint size);

       [StructLayout(LayoutKind.Sequential)]
       public struct AM3_OEM_DEVICE_MANAGER
       {
           public IntPtr DeviceHasConnected;
           public IntPtr DeviceHasDisconnected;
           public IntPtr DeviceStatusUpdate;
           public IntPtr AcquisitionAvailable;
       }



你打电话给结构




You call the structure with

g_device_manager = new AM3_OEM_DEVICE_MANAGER
  {
      DeviceHasConnected = Marshal.GetFunctionPointerForDelegate(new device_has_connected(DeviceHasConnected)),
      DeviceHasDisconnected = Marshal.GetFunctionPointerForDelegate(new device_has_connected(DeviceHasDisconnected)),
      DeviceStatusUpdate = Marshal.GetFunctionPointerForDelegate(new device_status_update(DeviceStatusUpdate)),
      AcquisitionAvailable = Marshal.GetFunctionPointerForDelegate(new device_acquisition_available(DeviceAcquisitionAvailable))
  };


这篇关于如何在C#中使用回调访问struct C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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