3

I'm having a issue translating a array of String^ that I'm receiving from a C# application. Why can I not create an array of String^? I'm fairly new to C++ so any help is appreciated.

public ref class Example
    {
        public:
            String^ Convert(String^ pointNames[], String^ outputPath)
            {

                std::string convertedPath = msclr::interop::marshal_as< std::string >(outputPath);
                std::string result = otherFunction(pointNames, convertedPath);

                return  msclr::interop::marshal_as< String^ >(result);
            }
    };

pointsNames[] is underlined as the error, with the message: Array of handles is not allowed.

What would be a better approach to send an array of strings from a C# application to C++?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Moh
  • 149
  • 2
  • 12
  • A quick search suggests changing `String^ pointNames[]` to `WriteOnlyArray^ pointNames` - Can you try that? –  Jul 19 '17 at 14:50
  • @Thebluefish I'm not sure I understand, I replaced String^ with WriteOnlyArray^ but the compiler just says WriteOnlyArray is not a template, do I have to include some library? – Moh Jul 19 '17 at 14:52
  • It looks like this belongs under the `Platform` namespace - Taken from [this post](https://social.msdn.microsoft.com/Forums/en-US/f0e968b2-5798-430a-8a00-d45c4a9ef1e0/pointer-to-a-c-array-of-hat-handles?forum=winappswithnativecode). –  Jul 19 '17 at 14:57
  • @Thebluefish okay that seems to work but how do I convert that to a string[] ? – Moh Jul 19 '17 at 17:03

1 Answers1

4

You tried to declare an unmanaged array type there, but you need a managed one to hold mananged types.

Declare the parameter as array<String^>^ pointNames.

Note: this is not std::array, it's cli::array, but when compiling with /clr then using namespace cli; is implied.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158