I'm currently stuck wrapping a library that I cannot change due to a struct containing a fixed size unsigned short array, that I seem unable to wrap into something similar in C# using the wrapper generator SWIG.
I have created a minimalistic example swig project file that contains everything needed to reproduce my problem.
%module SwigExperimentWrapper
struct ImportantData
{
static const unsigned short arraySize = 33;
unsigned short someUShortArray[arraySize];
};
The resulting type for the array is not satisfactory. I was expecting something like a
fixed ushort someUShortArray[arraySize];
in C#, but I only get a SWIGTYPE_p_unsigned_short type, which indicates that SWIG probably doesn't have enough information on what to do with this array. Note, that the behaviour is the same if I just define the array without a struct.
I suspect that I need some typemaps for the solution, but first I need to understand, why it won't generate something that matches my expectation. I feel, I am missing something, but I had problems like this with SWIG before, and the problems seemed more relatable.
I'd appreciate any kind of input, as I have not been able to find anything on various searches.
The fixed keyword in this meaning, is only useful when composing a struct
internal unsafe struct Buffer
{
public fixed char fixedBuffer[128];
}
This defines the array is embedded in the structure, not a referenced array-variable. Of course this must be of fixed size, to define the size of the struct at compile time.
This sample is taken from here. https://docs.microsoft.com/de-de/dotnet/csharp/programming-guide/unsafe-code-pointers/fixed-size-buffers
With the StructLayout Attribute you have control, on which byte a certain member of structure hast to start, and they can even overlap. So you can produced anything you like to Map any bytewise defined record, to a C# structure.
I don't know about your swig tool.
This solution requires unsafe code. (It's just a keyword, nothing to worry about)
An older method for chars would be something like this:
struct Foo
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=123)]
public char[] chars;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.