Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-11-01
2
Medium Priority
?
499 Views
Last Modified: 2013-12-17
I want to call C# functions from unmanaged C++.
To learn how, I modified http://www.codeproject.com/KB/dotnet/bridge.aspx 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?

Thanks!
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;
#else
	csharpclass __gc *DotNetObject;
#endif
 
	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;
#else
	DotNetObject = new csharpclass;
#endif
 
 
	//: 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[]
	//DotNetObject->test_array(
 
 
 }
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

0
Comment
Question by:shelleyee
2 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 1000 total points
ID: 22863351
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

0
 

Author Closing Comment

by:shelleyee
ID: 31512442
Thank you!  You saved me a bunch of guess-work!
I'll also try it separately with 2003.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

782 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