Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;


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.
Posted
Updated 23-Mar-17 5:27am

1 solution

i've found a solution

C#
[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

C#
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))
  };
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900