Solved

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

Posted on 2008-11-01
2
486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

734 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