By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,465 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,465 IT Pros & Developers. It's quick & easy.

passing arrays between C++ and C#

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Gregory Khrapunovich" <gr**@a-s-l.com> wrote in message
news:3e****************************@phx.gbl...
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.

Nov 16 '05 #2

P: n/a
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

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.

.

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.