tags:

views:

246

answers:

1

Can any one tell me how to return a String array from a method and use it in c#?

Suppose i have to return an array of {one,two, .....ten} and in c++ i have to display this array on console and perform some actions.

+1  A: 

See here for an example: http://haroonsaeed.wordpress.com/2006/08/11/interop-managed-c-and-c/

not done it my self but at a guess:

in C# create assembly called csharpassembly.dll with the following class

using System;
namespace Csharpassembly  {
 public class CSharpClass {
   public static string[] GetStrings() { 
    return new string[] {"1", "2", "3"}));
   }
 }
}

But in your case. Have an assembly that creates the array in c# and havea mC++ program reference this assembly and call it:

#include "stdafx.h"

#using <mscorlib.dll>
#using "csharpassembly.dll"

using namespace System;
using namespace Csharpassembly 

__gc class Test {
public:

    static void ProcessCShaperStrings()     {
        array^ stringArray = CSharpClass::GetStrings();
        Console::WriteLine(stringArray [0]); ...
        // etc

    } 
};
int wmain(void) { 
    Test:: ProcessCShaperStrings();    
    return 0;
}
Preet Sangha
That's about C# and managed C++ and the OP asks about C# and unmanaged C++.
sharptooth
That blog post seems to be a very good answer in my view. Is it possible for you to detail in your answer a little of what is explained in the blog post? That way your answer will still be helpful if haroon saeed's blog ever becomes unavailable.
mezoid
@sharptooth the OP doesn't really indicate whether he is referring to managed or unmanaged C++
mezoid
It is managed but i am looking for unmanaged code
Cute
There is always going to be interop. Why not just take the above example and populate a unmanaged datum in ProcessCShaperStrings(). Then the main() above could renamed to return this datum which you consume in an unmanaged consumer.
Preet Sangha