Solved

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

Posted on 2008-11-01
2
483 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 250 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

828 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