468,758 Members | 2,081 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,758 developers. It's quick & easy.

passing arrays between C++ and C#

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
2 2095

"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
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.

Similar topics

2 posts views Thread by dave.harper | last post: by
2 posts views Thread by Morgan | last post: by
2 posts views Thread by Steve Turner | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.