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. Marek -
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. MarekI am surprised it worked at all! The structure has pointers to memory and unless you are sticking the memory for the strings onto the end of the memory for the structure, I am not seeing how the memory for the strings are getting into the DLL. This seems like more of a C# problem than a problem with the DLL you are calling - might be better to post this to the C#-related message board. Peace!
-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
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. MarekYou should look up the
MarshalAs
attribute, especially with theUnmanagedType.LPTStr
andUnmanagedType.LPArray
parameters. And I have to agree that this question would fit better into the C# forum.Regards, Tim