Passing containers between C# and C++

K

Kenneth Porter

How do I pass a container from C# to C++?

My client is writing an app in C# that needs to pass a collection of 3-
space vectors to my native C++ library. It will also need to read back the
collection.

In pure native C++, I'd just pass a "std::vector<MyType>&" where MyType is
an x/y/z vector (which might contain a std::vector<double>).

What's the portable C++/CLI equivalent that C# can easily deal with?

The passed container might hold from 10,000 to 100,000 objects.
 
A

Arne Vajhøj

Kenneth said:
How do I pass a container from C# to C++?

My client is writing an app in C# that needs to pass a collection of 3-
space vectors to my native C++ library. It will also need to read back the
collection.

In pure native C++, I'd just pass a "std::vector<MyType>&" where MyType is
an x/y/z vector (which might contain a std::vector<double>).

What's the portable C++/CLI equivalent that C# can easily deal with?

The passed container might hold from 10,000 to 100,000 objects.

I would suggest a simple old fashioned array ...

Arne
 
D

David Anton

I might misunderstand your requirement, but you could always use
List<List<int>^> or something (List from System::Collections::Generic).

--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI
 
A

Andre Kaufmann

Kenneth said:
How do I pass a container from C# to C++?

My client is writing an app in C# that needs to pass a collection of 3-
space vectors to my native C++ library. It will also need to read back the
collection.

In pure native C++, I'd just pass a "std::vector<MyType>&" where MyType is
an x/y/z vector (which might contain a std::vector<double>).

You can't pass native C++ objects over Dll boundaries (safely). You
would have to wrap the vector with a COM object or function interface,
which allows you to iterate through the vector.
What's the portable C++/CLI equivalent that C# can easily deal with?
The passed container might hold from 10,000 to 100,000 objects.

Has been already answered by David A.

Each managed object can be directly accessed by C++/CLI and C#.

But for C++ perhaps the STL/CLR is more convenient. You must use VC 2008
however, since only this compiler ships with the STL/CLR.


Sample:

#include <cliext/vector>

public ref class CppObject
{
public:

CppObject()
{
vectorList = gcnew cliext::vector<int>();
// Add some values - for demonstr. purp. only
vectorList->push_back(10);
vectorList->push_back(20);
vectorList->push_back(30);

vectorList2.push_back(10);
vectorList2.push_back(20);
vectorList2.push_back(30);
}

cliext::vector<int>^ vectorList;
cliext::vector<int> vectorList2;
System::Collections::Generic::IList<int>^ GetCSharpCompatible();
System::Collections::Generic::IList<int>^ GetCSharpCompatible2();

}

System::Collections::Generic::IList<int>^ CppObject::GetCSharpCompatible()
{
return vectorList;
}

System::Collections::Generic::IList<int>^ CppObject::GetCSharpCompatible2()
{
// Returns a reference pointer on a member variable directly.
// Normally not a good idea to pass such objects
// over dll boundaries (depends -> on the lifetime of CppObject)
return %vectorList2;
}


Andre
 
P

Pavel Minaev

How do I pass a container from C# to C++?

My client is writing an app in C# that needs to pass a collection of 3-
space vectors to my native C++ library. It will also need to read back the
collection.

In pure native C++, I'd just pass a "std::vector<MyType>&" where MyType is
an x/y/z vector (which might contain a std::vector<double>).

What's the portable C++/CLI equivalent that C# can easily deal with?

The passed container might hold from 10,000 to 100,000 objects.

If the C++ application does not need to change the collection, and if
you need the best performance, you might consider using a managed
array - MyType[] - pinning it as you pass it to the C++ code. For a
more generic but slower solution, the receiving function should
declare its parameter as ICollection<MyType> or IList<MyType> -
whichever has all operations you need.
 
K

Kenneth Porter

I might misunderstand your requirement, but you could always use
List<List<int>^> or something (List from System::Collections::Generic).

Thanks, that sounds like the right thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top