简体   繁体   中英

How do I return a string array in c# and use it in unmanaged c++?

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM