Passing an array of structs from c# to unmanaged c++ dll
-
Hi, I need to pass a relativly complicated struct into a dll calling a CORBA Service which has to be written in vc6.0 and it has to contain many elements which are necessary to create an input for a service. Unfortunately I can see only the first array element, other are ignored. Here an example of c# code:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct MyStrStruct3 { public string FirstName; public string FamilyName; public int size; } [DllImport(@"C:\Projects\CorbaLib60\Debug\CorbaLib.dll")] private static extern int TestMe(ref MyStrStruct3[] pstr); MyStrStruct3[] Names = new MyStrStruct3[2]; Names[0].FamilyName = "Konieczny"; Names[0].FirstName = "Marek"; Names[0].size = 3; Names[1].FamilyName = "OtherName"; Names[1].FirstName = "OtherFirst"; Names[1].size = 2; int miki1 = TestMe(ref Names);
C++ code look like this:typedef struct _MyStruct2 { char* FirstName; char* FamilyName; UINT size; } MyStruct2; int TestMe(MyStruct2* pStruct[]) { int Size = 2; for (int i=0; isize; char* miki = pStruct[i]->FamilyName; char* miki1 = pStruct[i]->FirstName; } return 42; }
I define export :__declspec(dllexport) int TestMe(MyStruct2* pStruct[2]);
It works, but only for first element. I also have tried to pass it as[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct MyStrStruct4 { public MyStrStruct3[] Names; public int size; }
but it doesn't work and raise an exception. Marek