G
gpg
I am using a legacy DLL and need to marshal some structures for use in
the DLL. For the most part, I have figured out my needs except for one
small item.
I have a structure that contain, among other items, an array of bools
(not BOOL). The array is a fixed size and is contained in the
structure.
Should be simple ie:
[StructLayout(LayoutKind.Sequential)]
public struct MyArrayStruct2
{
[MarshalAs(UnmanagedType.I1)]
public bool flag;
[MarshalAs(UnmanagedType.I1)]
public bool flag2;
public int val1;
public int val2;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType =
UnmanagedType.I1, SizeConst = 9)]
public bool[] bArray;
public BoolArray bArray;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public int[] vals;
}
except when the structure is passed to the DLL, the data after the bool
array is broken and the data. I have found this same issue when the
data alignment in the passed structure is incorrect (ie single bool
type passed without the MarshalAs).
I have found a solution, which involves creating another strucure of
only bool (marshalled) and using the reference to this structure in my
main structure.
[StructLayout(LayoutKind.Sequential)]
public struct MyArrayStruct2
{
[MarshalAs(UnmanagedType.I1)]
public bool flag;
[MarshalAs(UnmanagedType.I1)]
public bool flag2;
public int val1;
public int val2;
public BoolArray bArray;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public int[] vals;
}
[StructLayout(LayoutKind.Sequential)]
public struct BoolArray
{
[MarshalAs(UnmanagedType.I1)]
public bool b1;
[MarshalAs(UnmanagedType.I1)]
public bool b2;
// more of the same
[MarshalAs(UnmanagedType.I1)]
public bool b9;
}
This works fine but is ugly and a bit unwieldy. I have tried different
size and packing attributes for the StructLayout to no avail.
Any ideas? It seems that the ByValArray with the subtype of I1 should
work fine.
Thanks!
GPG
the DLL. For the most part, I have figured out my needs except for one
small item.
I have a structure that contain, among other items, an array of bools
(not BOOL). The array is a fixed size and is contained in the
structure.
Should be simple ie:
[StructLayout(LayoutKind.Sequential)]
public struct MyArrayStruct2
{
[MarshalAs(UnmanagedType.I1)]
public bool flag;
[MarshalAs(UnmanagedType.I1)]
public bool flag2;
public int val1;
public int val2;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType =
UnmanagedType.I1, SizeConst = 9)]
public bool[] bArray;
public BoolArray bArray;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public int[] vals;
}
except when the structure is passed to the DLL, the data after the bool
array is broken and the data. I have found this same issue when the
data alignment in the passed structure is incorrect (ie single bool
type passed without the MarshalAs).
I have found a solution, which involves creating another strucure of
only bool (marshalled) and using the reference to this structure in my
main structure.
[StructLayout(LayoutKind.Sequential)]
public struct MyArrayStruct2
{
[MarshalAs(UnmanagedType.I1)]
public bool flag;
[MarshalAs(UnmanagedType.I1)]
public bool flag2;
public int val1;
public int val2;
public BoolArray bArray;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public int[] vals;
}
[StructLayout(LayoutKind.Sequential)]
public struct BoolArray
{
[MarshalAs(UnmanagedType.I1)]
public bool b1;
[MarshalAs(UnmanagedType.I1)]
public bool b2;
// more of the same
[MarshalAs(UnmanagedType.I1)]
public bool b9;
}
This works fine but is ugly and a bit unwieldy. I have tried different
size and packing attributes for the StructLayout to no avail.
Any ideas? It seems that the ByValArray with the subtype of I1 should
work fine.
Thanks!
GPG