Marshalling pointer to an array in struct from c++ to c# - c#

How do I convert this expression from C++ to C#?
struct MyStruct
{
uint8_t *rcSource;
uint8_t *rcMask;
uint32_t *clientAuthSchemes;
}
The structure is initialised in C++ this way:
MyStruct st;
st.rcSource = (uint8_t*) malloc(width*height);
st.rcMask = (uint8_t*) malloc(width*height);
st.clientAuthSchemes = (uint32_t*) malloc(sizeof(uint32_t)*(size+1));

If those are 1-dimensional arrays, you probably want something like this:
struct MyStruct
{
public byte[] rcSource;
public byte[] rcMask;
public uint[] clientAuthSchemes;
}
Initialization:
MyStruct st;
st.rcSource = new byte[width*height];
st.rcMask = new byte[width*height];
st.clientAuthSchemes = new uint[size+1];

I believe this is the way:
struct MyStruct
{
sbyte rcSource, rcMask;
int clientAuthSchemes;
}
:)

Related

Marshal array of struct into Ptr

I am calling a C library from a C# code. The function I am calling take as parameter a struct containing arrays of struct :
struct Example1Struct
{
char* a;
uint16_t b;
AnotherStruct* c;
}
c here is an array of pointer to AnotherStruct.
the struct in my C# code look like this
public struct Example1Struct
{
public IntPtr StationName;//is char*
public UInt16 IdCode;
public IntPtr AnotherStruct; //array of struct AnotherStruct
}
public static IntPtr MarshalToPointer(object data)
{
Type valueType = data.GetType();
IntPtr buf = IntPtr.Zero;
if (valueType.IsArray)
{
if (data is char[])
{
var d = data as char[];
buf = Marshal.AllocHGlobal(Marshal.SizeOf(d.GetType().GetElementType()) * d.Length);
}
else if (data is char[,])
{
var d = data as char[,];
buf = Marshal.AllocHGlobal(Marshal.SizeOf(d.GetType().GetElementType()) * d.Length);
}
else
{
buf = Marshal.AllocHGlobal(Marshal.SizeOf(data.GetType().GetElementType()) * count);
long LongPtr = buf.ToInt64(); // Must work both on x86 and x64
for (int I = 0; I < data.Lenght; I++)
{
IntPtr RectPtr = new IntPtr(LongPtr);
Marshal.StructureToPtr(data[I], RectPtr, false); // You do not need to erase struct in this case
LongPtr += Marshal.SizeOf(typeof(Rect));
}
}
return buf;
}
else
buf = Marshal.AllocHGlobal(Marshal.SizeOf(data));
Marshal.StructureToPtr(data, buf, false);
return buf;
}
my problem here is that I cannot cast data (who is an array of AnotherStruct) to object[] , neither in IEnumerable. So I cannot access to data[I] and don't have data.Lenght
Any idea ?
Usually I'd recommend using the MarshalAs attribute rather than writing manual marshalling code. It looks like:
public struct Example1Struct
{
public IntPtr StationName;//is char*
public UInt16 IdCode;
public IntPtr AnotherStruct; //array of struct AnotherStruct
}
Could be:
public struct Example1Struct
{
[MarshalAs(UnmanagedType.LPStr)]
public string StationName;
public UInt16 IdCode;
[MarshalAs(UnmanagedType.LPArray)]
public AnotherStruct[] OtherStructs;
}
And the marshaller should do the right thing for you when you pass it to unmanaged code.
You can get the length of the array like this:
if (data is Array a)
Console.WriteLine(a.Length);
Arrays in c# always derive from Array, so you can cast it to that.
But if possible in your real code, I'd recommend Damien's answer

How to use UnmanagedMemoryAccessor.ReadArray<T> (Int64, T[], Int32, Int32)

I want to read/write data to Memory Mapped File .How do I use UnmanagedMemoryAccessor.ReadArray (Int64, T[], Int32, Int32)
struct Data{public int a; public int b; public byte[] ;}
static Data _sdata = new Data();
static Data _mydata = new Data();
_mdata.byte = _sdata.byte = new byte[2] ;
_mmf = MemoryMappedFile.CreateNew( "test", 10);
var ired = _mmf.CreateViewAccessor();
ired.Read( 0, out mdata );here has Error
So, what is the T struct should correct ??
The array cannot be a reference it has to be contained within the struct. You have to enable unsafe code for your assembly and use the following struct definition :
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct Data {
public int a;
public int b;
public fixed byte bytes[100];
}

C# How to P/Invoke with struct array in struct?

In C#, how to P/Invoke with struct array in struct?
C Lang defined struct is below...
struct 'OuterStruct'
int outerId
InnerStruct[10] innerStruct
struct 'InnerStruct'
int innerId
char[32] name
And C Lang defined Function is:
int ClangFunc(OuterStruct* arg)
'ClangFunc' is set values to 'arg'.
I call 'ClangFunc' from C#...
[DllImport("makefromclang.dll", EntryPoint="ClangFunc")]
public static extern int ClangFunc(IntPtr arg);
[StructLayout(LayoutKind.Sequential)]
public struct OuterStruct
{
public int outerId;
[MarshalAs(UnmanagementType.ByValArray, SizeConst=10)]
public InnerStuct[] innerStruct;
}
[StructLayout(LayoutKind.Sequential)]
public struct InnerStruct
{
public int innerId;
[MarshalAs(UnmanagementType.ByValTStr, SizeConst=32)]
public string name;
}
/* caller */
OuterStruct outerStruct = new OuterStruct();
IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(outerStruct));
Marshal.StructureToPtr(outerStruct, ptr, false);
int result = ClangFunc(ptr);
OuterStruct resultStruct = (OuterStruct)Marshal.PtrToStructure(ptr, typeof(OuterStruct));
Call ClangFunc is succeeded.
Results of OuterStruct.outerId and OuterStruct.innerStruct[0].innerId are set collect values.(in above resultStruct value)
But OuterStruct.innerStruct[0].name is null, why?.
I expected ""(empty string) or any Shift_JIS string. There's no way to set null value.
Thanks all.
The problem is that the value set in char[] is Shift_JIS charset.
When .NET converted char[] to string, it did not consider the character set.
As a result, the string value is corrupted, and the debugger seems to have a null string value.
To solve this problem, I modified the string mapped to the structure to byte[] and convert byte[] to string.
// [MarshalAs(UnmanagementType.ByValTStr, SizeConst=32)]
// public string name;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType=UnmanagedType.ByValTStr, SizeConst=32)]
public byte[] name;
string str = System.Text.Encoding.GetEncoding("Shift_JIS").GetString(name);

Moving structure data in C#

Lets say I have the following structure in C
typedef struct
{
int field1;
char field2[16];
} MYSTRUCT;
Now I have a C routine that is called with a pointer to MYSTRUCT and I need to populate the structure, e.g.,
int MyCall(MYSTRUCT *ms)
{
char *hello = "hello world";
int hlen = strlen(hello);
ms->field1 = hlen;
strcpy_s(ms->field2,16,hello);
return(hlen);
}
How would I write MyCall in C#? I have tried this in Visual Studio 2010:
...
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Explicit)]
public struct MYSTRUCT
{
[FieldOffset(0)]
UInt32 field1;
[FieldOffset(4)]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
string field2;
}
public int MyProc(ref MYSTRUCT ms)
{
string hello = "hello world";
int hlen = hello.Length;
Marshal.Copy(hello, ms.field2, 0, hlen); // doesn't work
Array.Copy(hello, ms.field2, hlen); // doesn't work
// tried a number of other ways with no luck
// ms.field2 is not a resolved reference
return(hlen);
}
Thanks for any tips on the right way to do this.
Try changing the StructLayout.
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct MYSTRUCT
{
public UInt32 field1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string field2;
}
Since, you're passing as a reference, have you tried setting it as:
public int MyProc(ref MYSTRUCT ms)
{
string hello = "hello world";
ms.field2 = hello;
return hello.Length;
}
When using the ref keyword, you'll call MyProc like so:
static void Main(string[] args)
{
var s = new MYSTRUCT();
Console.WriteLine(MyProc(ref s)); // you must use "ref" when passing an argument
Console.WriteLine(s.field2);
Console.ReadKey();
}

Passing parameters from c# to c++

As the topic says, trying to pass a struct from c# environnement to c++.
c++ code that defines both the struct and the interface:
#pragma pack(push, 4)
struct CEA708CONFIG
{
BYTE b608Service;
BYTE bCompactStream;
BYTE pActiveServices[63];
LONG lActiveServiceCount; //
POINT ptAlignmentPosition;
};
#pragma pack(pop)
interface
__declspec(uuid("{some clsid}"))
ICEA708Decoder : IUnknown {
virtual HRESULT SetConfig(IN const CEA708CONFIG* pConfig) = 0;
virtual HRESULT GetConfig(OUT CEA708CONFIG* pConfig) = 0;
};
now to the c# code, i defined the same struct in c#
[StructLayout(LayoutKind.Sequential, Pack = 4), Serializable]
public struct CEA708CONFIG
{
public byte is608Service;
public byte isCompactStream;
//[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_UI1)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 63)]
public IntPtr activeServices;
public long activeServiceCount;
public Point alignmentPosition;
};
and the corresponding interface that accepts the config structure
[ComVisible(true), ComImport, Guid("same clsid as above"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ICEA708Decoder
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int SetConfig([In, MarshalAs(UnmanagedType.Struct)] ref CEA708CONFIG config);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int GetConfig([Out, MarshalAs(UnmanagedType.Struct)] out CEA708CONFIG config);
}
my problem occurs whenever i try to pass the structure, i can clearly see that while executing the c# code the entire struct is intialized with "reasonable" values, but once passed to the c++, i see that something has happened during the transaction.
the c# code that makes the magic happen:
CEA708CONFIG cc708Config;
ICEA708Decoder CC708DecoderConfig = CC708Filter as ICEA708Decoder;
if (CC708DecoderConfig == null)
{
throw new ApplicationException("Couldn't get ICEA708Decoder structure");
}
byte[] dataByte = new byte[63];
int size = Marshal.SizeOf(dataByte[0]) * dataByte.Length;
IntPtr pnt = Marshal.AllocHGlobal(size);
dataByte[0] = 1;
Marshal.Copy(dataByte, 0, pnt, dataByte.Length);
cc708Config.activeServices = pnt;
if (0 != (hr = CC708DecoderConfig.SetConfig(ref cc708Config)))
{
throw new ApplicationException("Couldn't SetConfig() because: " + DirectShowLib.DsError.GetErrorText(hr));
}
and the exception triggered by the SetConfig is:
{"Cannot marshal field 'activeServices' of type
'CCReIndexer.Graphs.CEA708CONFIG': Invalid managed/unmanaged type
combination (Int/UInt must be paired with SysInt or SysUInt).":""}
thanks for your help!!
Have you tried transfer array as array?
[StructLayout(LayoutKind.Sequential, Pack = 4), Serializable]
public struct CEA708CONFIG
{
public byte is608Service;
public byte isCompactStream;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 63)]
public byte[] activeServices;
public long activeServiceCount;
public Point alignmentPosition;
};
byte[] dataByte = new byte[63];
cc708Config.activeServices = dataByte;

Categories