I have the following structures in C# and C++.
C++:
struct TestA
{
char* iu;
};
struct TestB
{
int cycle1;
int cycle2;
};
struct MainStruct
{
TestA test;
TestB test2;
};
C#:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack = 1)]
internal struct TestA
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 36)]
private string iu;
public TestA(Guid Iu)
: this()
{
iu = Iu.ToString("D");
}
}
[StructLayout(LayoutKind.Sequential), Serializable]
internal struct TestB
{
int cycle1;
int cycle2;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MainStruct
{
private TestA testA;
private TestB testB;
private MainStruct(TestA Test_A) : this()
{
testA = Test_A;
}
public static MainStruct Initial(TestA Test_A, TestB Test_B)
{
return new MainStruct(Test_A)
{
testB = Test_B
};
}
}
Marhsaling data had worked until I had to write struct which combines two others (MainStruct). C++ doesn't receive data. On c++ site I get: "Error reading characters of string"
Could anyone shed light on it? What is wrong? On c# or c++ site?
Change this:
to this:
Note that this code is good only to pass a string in the C#->C++ direction. For the opposite direction (C++->C#) it is more complex, because C# can't easily deallocate C++ allocated memory.
Other important thing: if you pass this structure to a C++ method, like:
the lifetime of the pointer
char* iu
will end at the end of theSomeCPlusPlusMethod
method call... The C# marshaler will allocate some memory before callingSomeCPlusPlusMethod
and will free that memory after callingSomeCPlusPlusMethod
. This is important if the C++ method wants to save thechar* iu
. In that case it has tostrdup(iu)
or something similar.