0

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.

3
  • How is this significantly different from this one stackoverflow.com/questions/1032060/…? Commented Jun 24, 2009 at 11:30
  • He is asking it for the third time now... stackoverflow.com/questions/1036720/… Commented Jun 24, 2009 at 11:49
  • But i am looking for the way to return strings and use them on main () but in previous it is list here a string array Commented Jun 24, 2009 at 12:36

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

That's about C# and managed C++ and the OP asks about C# and unmanaged C++.
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.
@sharptooth the OP doesn't really indicate whether he is referring to managed or unmanaged C++
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.