passing arrays between C++ and C#

  • Thread starter Thread starter Gregory Khrapunovich
  • Start date Start date
G

Gregory Khrapunovich

Hi,
I need to pass a structure between C++ DLL and C# client
(both ways). Among other elements the strcuture must
contain a fixed size array of integers. In C++ I can
successfully declare a structure like this:

public __gc class MyStructure
{
public:
int* __gc arr;

MyStructure(){arr = new int[10];}
~EtifConfig(){delete[] arr;}
}

But I cannot address this array in C# code, compiler
gives an error message. When I try to use __gc anywhere
in array declaration, compiler gives another error
message.

Can somebody point to an example that works?
Thank you in advance,
Gregory
 
Gregory Khrapunovich said:
Hi,
I need to pass a structure between C++ DLL and C# client
(both ways). Among other elements the strcuture must
contain a fixed size array of integers. In C++ I can
successfully declare a structure like this:

public __gc class MyStructure
{
public:
int* __gc arr;

MyStructure(){arr = new int[10];}
~EtifConfig(){delete[] arr;}
}

But I cannot address this array in C# code, compiler
gives an error message. When I try to use __gc anywhere
in array declaration, compiler gives another error
message.

Can somebody point to an example that works?
Thank you in advance,
Gregory


Please post code that possibly compiles.
You are not declaring an array of int's, but an arry of pointers to an int
(which type is not allowed in a managed array)!
You don't need a destructor, that's the GC job in managed code.

public __gc class MyStructure
{
public:
// explicitly qualify the int array is managed
int arr __gc[] ;
// or use the implicit declaration
// System::Int32 arr[];
MyStructure()
{
arr = new int __gc[10];
}
};


C#
MyStructure ms = new MyStructure();
// Fill array
for (int inx = 0; inx != ms.arr.Length; inx++)
ms.arr[inx] = inx;
// dump to console
foreach(int i in ms.arr)
Console.WriteLine(i.ToString());

Willy.
 
Thank you very much! My problem was that I didn't know
where to put __gc. All my declarations like
"int __gc myarr[10]" etc. caused a compiler error. Now
when I use __gc in a proper place everything works.
Gregory
 
Back
Top