How use struct with union in c# - c#

Hi I writing a wrapper in c# and i have some problem. I have this struct in c++.
typedef struct pjmedia_format
{
pj_uint32_t id;
pjmedia_type type;
pjmedia_format_detail_type detail_type;
union
{
pjmedia_audio_format_detail aud;
pjmedia_video_format_detail vid;
char user[PJMEDIA_FORMAT_DETAIL_USER_SIZE];
} det;
} pjmedia_format;
This is link to this struct pjmedia_format
in c# I have this:
[StructLayout(LayoutKind.Sequential)]
public struct pjmedia_format
{
public uint id;
public pjmedia_type type;
public pjmedia_format_detail_type detail_type;
public det_t det;
}
[StructLayout(LayoutKind.Explicit)]
public struct det_t
{
[FieldOffset(0)]
[MarshalAs(UnmanagedType.Struct)]
public pjmedia_audio_format_detail aud;
[FieldOffset(36)]
[MarshalAs(UnmanagedType.Struct)]
public pjmedia_video_format_detail vid;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
[FieldOffset(60)]
public char[] user;
}
[StructLayout(LayoutKind.Sequential)]
public struct pjmedia_audio_format_detail
{
public uint clock_rate;
public uint channel_count;
public uint frame_time_usec;
public uint bits_per_sample;
public int avg_bps;
public int max_bps;
}
[StructLayout(LayoutKind.Sequential)]
public struct pjmedia_video_format_detail
{
public pjmedia_rect_size size;
public pjmedia_ratio fps;
public int avg_bps;
public int max_bps;
}
and when i want to use this struct i get this error
System.Runtime.InteropServices.MarshalDirectiveException was unhandled.
Message="A method signature is not PInvoke compatible with the element."
I try to use some attributes like size or pack but it doesn't help (probably i use it wrong). I tested singly other struct e.g. pjmedia_video_format_detail and they works well. Any advice?
Best regards
Andrzej

As this is a union, shouldn't that be:
[StructLayout(LayoutKind.Explicit)]
public struct det_t
{
[FieldOffset(0)]
[MarshalAs(UnmanagedType.Struct)]
public pjmedia_audio_format_detail aud;
[FieldOffset(0)]
[MarshalAs(UnmanagedType.Struct)]
public pjmedia_video_format_detail vid;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
[FieldOffset(0)]
public char[] user;
}
i.e. overlapping? Also, you might need user to be a fixed buffer rather than an array.

In the C++ code, det is a union. That means that all the fields have zero offset, they are overlayed. Simply change your C# declaration to match by using [FieldOffset(0)] for all fields in det_t.

Related

converting c++ API struct to c#

I am trying to convert c++ api example to c#. but I can not find the way of struct pointer in struct by the way struct has string types. Here is c++ structs and functions
struct AuthParam { char server_ip[32]; char username[50]; char password[50]; };
struct CameraInfo { int index; char devicename[100]; char smallrtsp[1000]; char bigrtsp[1000]; };
struct SingleDevice { char deviceid[50]; char devicename[100]; int flag_onuse; int cameralist_size; CameraInfo* cameralist; };
struct DeviceList { int listsize; SingleDevice* singledevicelist; };
typedef int (WINAPI capi_init)(void);
typedef int (WINAPI capi_disabled)(void);
typedef int (WINAPI capi_GetServerTimeCode)(char* server_ip, unsigned int* timecode);
typedef int (WINAPI GetDevicelist)(AuthParam auth_para, DeviceList* devicelist);
and this is my c# code but I can not find the way of defining struct pointer in struct I got "
Error CS0208 Cannot take the address of, get the size of, or declare a pointer to a managed type ('Form1.CameraInfo')" error.
public struct AuthParam
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string server_ip;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string username;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string password;
};
[StructLayout(LayoutKind.Sequential)]
public struct CameraInfo
{
public int index;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string devicename;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1000)]
public string smallrtsp;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1000)]
public string bigrtsp;
};
[StructLayout(LayoutKind.Sequential)]
public struct SingleDevice
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string deviceid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string devicename;
public int flag_onuse;
public int cameralist_size;
public CameraInfo *cameralist;
};
[StructLayout(LayoutKind.Sequential)]
public unsafe struct DeviceList
{
public int listsize;
public SingleDevice *singledevicelist;
};
[DllImport("c:\\lib\\api_client.dll")] public static extern int capi_init();
[DllImport("c:\\lib\\api_client.dll")] public static extern int capi_disabled();
[DllImport("c:\\lib\\api_client.dll")] public static extern int capi_GetServerTimeCode(string ip, ref uint timecode);
[DllImport("c:\\lib\\api_client.dll")] public static extern int GetDevicelist (AuthParam auth_para,ref DeviceList devicelist);
is there any way to achieve this convertion?
i think this error because of the different nature of c++ (un-managed code) and c# (managed code), so maybe using System.IntPtr as a pointer to your camera struct.
please consider this question here, and you can find about P/Invoke Interop Assistant which is an open source tool to convert your code from the un-managed code to a managed C# code. with a small blog article about C++/C# interoperability

How to correctly parse IntPtr to Object in C# when marshaling from C++

I'm invoking a C++ function from within C#.
The function I am trying to invoke returns following C++ struct type result:
LPWFSCIMNOTETYPELIST lpNoteTypeList;
typedef struct _wfs_cim_note_type_list
{
USHORT usNumOfNoteTypes;
LPWFSCIMNOTETYPE *lppNoteTypes;
} WFSCIMNOTETYPELIST, *LPWFSCIMNOTETYPELIST;
typedef struct _wfs_cim_note_type
{
USHORT usNoteID;
CHAR cCurrencyID[3];
ULONG ulValues;
USHORT usRelease;
BOOL bConfigured;
} WFSCIMNOTETYPE, *LPWFSCIMNOTETYPE;
I found the suitable struct in C#:
[StructLayout(LayoutKind.Sequential, Pack = XFSConstants.STRUCTPACKSIZE, CharSet = CharSet.Ansi)]
public unsafe struct WfsCimNoteType
{
public ushort usNoteID;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public char[] cCurrencyID;
public uint ulValues;
public ushort usRelease;
public byte bConfigured;
}
[StructLayout(LayoutKind.Sequential, Pack = XFSConstants.STRUCTPACKSIZE, CharSet = CharSet.Ansi)]
public unsafe struct WfsCimNoteTypeList
{
public ushort usNumOfNoteTypes;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public WfsCimNoteType[] lppNoteTypes;
}
I used the following code to parse the result from invoked function:
WfsCimNoteTypeList data = new WfsCimNoteTypeList();
XFSUtil.PtrToStructure<WfsCimNoteTypeList>(lpBuffer, ref data);
(here lbBuffer is IntPtr)
XFSUtil.PtrToStructure is defined like this:
public static void PtrToStructure<T>(IntPtr ptr, ref T p) where T : struct
{
p = (T)Marshal.PtrToStructure(ptr, typeof(T));
}
But after parsing from IntPtr to WfsCimNoteType object array, I am getting different values???

Marshaling c# call to DLL library

I am having trouble calling a DLL library with a specific signature. This is on the xamarin platform and running on iOS. I am trying to get a list of DNS servers. I can call other functions without an issue, but the one I really need is not working. I am sure its becuase my struct is not correct. I believe the issue is the "union" type. I just dont know what my struct should look like to appease that parameter. Here are the relevant c++ types:
1: The DLL singature
`int res_getservers __P((res_state, union res_sockaddr_union * , int));`
2: The c++ structs
union res_sockaddr_union {
struct sockaddr_in sin;
#ifdef IN6ADDR_ANY_INIT
struct sockaddr_in6 sin6;
#endif
#ifdef ISC_ALIGN64
int64_t __align64; /* 64bit alignment */
#else
int32_t __align32; /* 32bit alignment */
#endif
char __space[128]; /* max size */
};
struct sockaddr_in6 {
__uint8_t sin6_len; /* length of this struct(sa_family_t) */
sa_family_t sin6_family; /* AF_INET6 (sa_family_t) */
in_port_t sin6_port; /* Transport layer port # (in_port_t) */
__uint32_t sin6_flowinfo; /* IP6 flow information */
struct in6_addr sin6_addr; /* IP6 address */
__uint32_t sin6_scope_id; /* scope zone index */
};
struct sockaddr_in {
__uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
Any help on generating the proper c# structs would be greatly appreciated! Thank you.
Disclaimer: I don't have C++ installed on my machine any more but I believe this will either work, or at least give you a good shove in the right direction.
Based on: https://msdn.microsoft.com/en-us/library/windows/desktop/ms738571(v=vs.85).aspx
And: https://learn.microsoft.com/en-us/cpp/mfc/reference/sockaddr-in-structure
And: https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute(v=vs.110).aspx
And numerous other Google searches for the types in question.
using System;
using System.Runtime.InteropServices;
namespace SockStructTest
{
class Program
{
[StructLayout(LayoutKind.Explicit, Size = 8)]
public struct struct_sin_zero
{
[FieldOffset(0)]
public byte s_b1;
[FieldOffset(1)]
public byte s_b2;
[FieldOffset(2)]
public byte s_b3;
[FieldOffset(3)]
public byte s_b4;
[FieldOffset(4)]
public byte s_b5;
[FieldOffset(5)]
public byte s_b6;
[FieldOffset(6)]
public byte s_b7;
[FieldOffset(7)]
public byte s_b8;
[FieldOffset(0)]
public UInt64 s_ui64;
};
[StructLayout(LayoutKind.Explicit, Size = 4)]
public struct in_addr
{
[FieldOffset(0)]
public byte s_b1;
[FieldOffset(1)]
public byte s_b2;
[FieldOffset(2)]
public byte s_b3;
[FieldOffset(3)]
public byte s_b4;
[FieldOffset(0)]
public UInt16 s_w1;
[FieldOffset(2)]
public UInt16 s_w2;
[FieldOffset(0)]
public UInt32 S_addr;
};
[StructLayout(LayoutKind.Explicit, Size = 16)]
public struct sockaddr_in
{
[FieldOffset(0)]
public Int16 sin_family;
[FieldOffset(2)]
public UInt16 sin_port;
[FieldOffset(4)]
public in_addr sin_addr;
[FieldOffset(8)]
public struct_sin_zero sin_zero;
};
[StructLayout(LayoutKind.Explicit, Size = 16)]
public struct in6_addr_u
{
[FieldOffset(0)]
public byte Byte0;
[FieldOffset(1)]
public byte Byte1;
[FieldOffset(2)]
public byte Byte2;
[FieldOffset(3)]
public byte Byte3;
[FieldOffset(4)]
public byte Byte4;
[FieldOffset(5)]
public byte Byte5;
[FieldOffset(6)]
public byte Byte6;
[FieldOffset(7)]
public byte Byte7;
[FieldOffset(8)]
public byte Byte8;
[FieldOffset(9)]
public byte Byte9;
[FieldOffset(10)]
public byte ByteA;
[FieldOffset(11)]
public byte ByteB;
[FieldOffset(12)]
public byte ByteC;
[FieldOffset(13)]
public byte ByteD;
[FieldOffset(14)]
public byte ByteE;
[FieldOffset(15)]
public byte ByteF;
[FieldOffset(0)]
public UInt16 Word0;
[FieldOffset(2)]
public UInt16 Word1;
[FieldOffset(4)]
public UInt16 Word2;
[FieldOffset(6)]
public UInt16 Word3;
[FieldOffset(8)]
public UInt16 Word4;
[FieldOffset(10)]
public UInt16 Word5;
[FieldOffset(12)]
public UInt16 Word6;
[FieldOffset(14)]
public UInt16 Word7;
}
[StructLayout(LayoutKind.Explicit, Size = 16)]
public struct in6_addr
{
[FieldOffset(0)]
public in6_addr_u u;
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct sockaddr_in6
{
public UInt64 sin6_len; // length of this struct(sa_family_t)
public UInt32 sin6_family; // AF_INET6 (sa_family_t)
public UInt16 sin6_port; // Transport layer port # (in_port_t)
public UInt32 sin6_flowinfo; // IP6 flow information
public in6_addr sin6_addr; // IP6 address
public UInt32 sin6_scope_id; // scope zone index
};
[StructLayout(LayoutKind.Explicit, Size = 16)]
public struct res_sockaddr_union
{
[FieldOffset(0)]
public sockaddr_in sin;
[FieldOffset(0)]
public sockaddr_in6 sin6;
[FieldOffset(0)]
public Int64 __align64;
[FieldOffset(0)]
public Int32 __align32;
// This is the char __space[128] bit.
// I've cheated and used 16 8 byte values.
[FieldOffset(0)]
public UInt64 __space0;
[FieldOffset(8)]
public UInt64 __space1;
[FieldOffset(16)]
public UInt64 __space2;
[FieldOffset(24)]
public UInt64 __space3;
[FieldOffset(32)]
public UInt64 __space4;
[FieldOffset(40)]
public UInt64 __space5;
[FieldOffset(48)]
public UInt64 __space6;
[FieldOffset(56)]
public UInt64 __space7;
[FieldOffset(64)]
public UInt64 __space8;
[FieldOffset(72)]
public UInt64 __space9;
[FieldOffset(80)]
public UInt64 __spaceA;
[FieldOffset(88)]
public UInt64 __spaceB;
[FieldOffset(96)]
public UInt64 __spaceC;
[FieldOffset(104)]
public UInt64 __spaceD;
[FieldOffset(112)]
public UInt64 __spaceE;
[FieldOffset(120)]
public UInt64 __spaceF;
}
static void Main(string[] args)
{
sockaddr_in ad;
ad.sin_zero.s_ui64 = 0;
ad.sin_family = 0; // or whatever
ad.sin_port = 1234;
// Address as b bit values
ad.sin_addr.s_b1 = 0;
ad.sin_addr.s_b2 = 1;
ad.sin_addr.s_b3 = 2;
ad.sin_addr.s_b4 = 3;
// Address as 16 bits
ad.sin_addr.s_w1 = 0;
ad.sin_addr.s_w2 = 0;
}
}
}
Best I can do - don't feel under pressure to accept the answer if it doesn't solve your issue but please do take some time to try to understand what's going on in the example I've given you.
I haven't had chance to test it - but it does build.
Good luck!
Regards,
Adam.

Translating C++ DLL into C# - How to translate a struct included in a struct

I have to translate a C++ DLL into C# to use it in a project. Here is the part that causes me trouble. This is the C++ code of the header of the DLL :
struct initiate_rb
{
unsigned char Rem_Add;
unsigned char Features_Supported_1_m;
struct add_addr_param Add_Addr_Param;
};
struct add_addr_param
{
unsigned char D_Type;
unsigned char D_Len;
struct
{
unsigned char Network_Address[6];
unsigned short MAC_Address_Len;
unsigned char * MAC_Address;
} S_Addr;
};
I am not sure how to handle this in C#. Here is what I have achieved so far :
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct initiate_rb
{
public byte Rem_Add;
public byte Features_Supported_1_m;
public add_addr_param Add_Addr_Param;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct add_addr_param
{
public byte D_Type;
public byte D_Len;
[StructLayout(LayoutKind.Sequential)]
public struct S_Addr
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public byte[] Network_Address;
public ushort MAC_Address_Len;
public byte* MAC_Address;
};
}
I used Pack = 1 because in my DLL header file there is the line #pragma pack (1)
The problem is, this doesn't work when I have to use this struct. It returns me an error code.
So first, concerning this struct translation, am I doing it right ?
Thanks for helping.
I'm not sure about the attributes but I believe the second struct shoudn't be nested in C# because the one in C++ defines a variable of the struct's type just after the closing parentheses.
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct dpc2_add_addr_param
{
public byte D_Type;
public byte D_Len;
public S_Addr S_Addr;
}
[StructLayout(LayoutKind.Sequential)]
public struct S_Addr
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public byte[] Network_Address;
public ushort MAC_Address_Len;
public byte* MAC_Address;
};
In fact I resolved this problem. The problem was that I was using a constructor for my initiate_rb struct
unsafe struct initiate_rb
{
public byte Rem_Add;
public byte Features_Supported_1_m;
public add_addr_param Add_Addr_Param;
public initiate_rb(int networkAddressLength) : this()
{
Rem_Add = (byte)0;
Features_Supported_1_m = (byte)0;
add_addr_param = new Add_Add_Param(networkAddressLength);
}
}
and this constructor for add_addr_param :
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct add_addr_param
{
public byte D_Type;
public byte D_Len;
[StructLayout(LayoutKind.Sequential)]
public struct S_Addr
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public byte[] Network_Address;
public ushort MAC_Address_Len;
public byte* MAC_Address;
};
public add_addr_param(int networkAddressLength) : this()
{
D_Type = (byte)0;
D_Len = (byte)0;
S_Addr.Network_Address = new byte[6]; //problem with this line
}
}
This was the problem. Since I remove this line, the code was operating correctly. In fact I didn't need constructors since all parameters have to be set to 0.
Thanks for helping!

Casting explicitly-laid out structures

Let's say I have this structure,
[StructLayout(LayoutKind.Explicit)]
public struct Chapter4Time
{
[FieldOffset(0)]
public UInt16 Unused;
[FieldOffset(2)]
public UInt16 TimeHigh;
[FieldOffset(4)]
public UInt16 TimeLow;
[FieldOffset(6)]
public UInt16 MicroSeconds;
}
and this structure.
[StructLayout(LayoutKind.Explicit)]
public struct IEEE_1588Time
{
[FieldOffset(0)]
public UInt32 NanoSeconds;
[FieldOffset(4)]
public UInt32 Seconds;
}
How would I convert from one structure to the other?
Two options here:
No unsafe code, but explicit structure layout
(Note that although this isn't unsafe as far as the C# compiler is concerned, some frameworks may still disallow it - see Marc Gravell's comment.)
You could use a union type, which is just another struct with two fields, both set explicitly to the same location. Here's a complete example using your structures:
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Explicit)]
public struct Chapter4Time
{
[FieldOffset(0)]
public UInt16 Unused;
[FieldOffset(2)]
public UInt16 TimeHigh;
[FieldOffset(4)]
public UInt16 TimeLow;
[FieldOffset(6)]
public UInt16 MicroSeconds;
}
[StructLayout(LayoutKind.Explicit)]
public struct IEEE_1588Time
{
[FieldOffset(0)]
public UInt32 NanoSeconds;
[FieldOffset(4)]
public UInt32 Seconds;
}
[StructLayout(LayoutKind.Explicit)]
public struct TimeUnion
{
[FieldOffset(0)]
public Chapter4Time Chapter4Time;
[FieldOffset(0)]
public IEEE_1588Time IEEE_1588Time;
}
class Test
{
static void Main()
{
var ch4 = new Chapter4Time { TimeLow = 100, MicroSeconds = 50 };
var union = new TimeUnion { Chapter4Time = ch4 };
Console.WriteLine(union.IEEE_1588Time.Seconds);
}
}
Unsafe code, casting pointers
An alternative to the union type if you can use unsafe code is to cast a pointer of type Chapter4Time* to IEEE_1588Time*:
class Test
{
unsafe static void Main()
{
var ch4 = new Chapter4Time { TimeLow = 100, MicroSeconds = 50 };
var ieee1588 = *((IEEE_1588Time*) &ch4);
Console.WriteLine(ieee1588.Seconds);
}
}
Personally I'd avoid doing any of this if at all possible, but if you *really, really want to do it, these are probably the simplest approaches.

Categories

Resources