Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-11-01
2
Medium Priority
?
495 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 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

Implementing Azure Infrastructure Exam 70-533

This course is designed to familiarize and instruct students in the content that is covered by Microsoft Exam 70-533, Implementing Microsoft Azure Solutions. It focuses on all the November 2016 objective domain topics.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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