Solved

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

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

628 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