How do  I pass an array of ints from unmanaged C++ to C#?

Posted on 2008-11-01
Last Modified: 2013-12-17
I want to call C# functions from unmanaged C++.
To learn how, I modified to
use C# instead of visual basic.
Then I get the error 3>.\Bridge.cpp(102) : error C2664: 'csharpassembly::csharpclass::test_array' : cannot convert parameter 1 from 'System::Array __gc *' to 'int  __gc[]'
What do I need to change to make it correctly pass an array of ints?

from Bridge.cpp:

  __declspec(dllexport) int __cdecl bridge_function_array(int *values, int len) 



	int i, cnt;

	Array __gc *managed_values;

#ifdef usevb

	mcDotNetClass __gc *DotNetObject;


	csharpclass __gc *DotNetObject;


	managed_values = Array::CreateInstance(__typeof(System::Int32),len);

	cnt = managed_values->GetLength(0);


	for (i=0;i < cnt;i++) {

		managed_values->SetValue(__box(values[i]), i);	


#ifdef usevb

	DotNetObject = new mcDotNetClass;


	DotNetObject = new csharpclass;


	//: error C2664: 'csharpassembly::csharpclass::test_array' : 

//cannot convert parameter 1 from 'System::Array __gc *' to 'int  __gc[]'

	return DotNetObject->test_array(managed_values);

	// test_array takes  int intarray __gc[]



From csharpclass:

        public int test_array(int[] intarray)


            int total = 0;

            int value;

            foreach (int i in intarray)


                value = i;

                total = total + value;


            return total;


Open in new window

Question by:shelleyee
LVL 19

Accepted Solution

drichards earned 250 total points
Comment Utility
As you appear to be using the old managed C++ syntax, try this to declare and load the managed array.  Are you pre-2005 version of Visual Studio?
	int managed_values __gc[] = __gc new int __gc[len];


	for (i=0;i < len;i++) {

		managed_values[i] = values[i];


Open in new window


Author Closing Comment

Comment Utility
Thank you!  You saved me a bunch of guess-work!
I'll also try it separately with 2003.

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now