how to transfer contents of char[] to a String^ in C++/CLI

588 Views Asked by At

I've commandLine arguments in a C++/CLI program denoted by char* argv[]. I want to transfer all the contents getting concatenated to a String^ class.

Code:

String ^masterString = "Commands=>";

for(int i=0; argv[i] != nullptr; ++i)
masterString += String(argv[i]);

However, I find the above not working in the last statement where I use += operator.

  1. What's the wrong usage here? Error here is No operators match the operands.

  2. Any other better ways to store contents into String^ from char*?

1

There are 1 best solutions below

0
On

Look into MSDN, mostly into this:

#include <stdlib.h>
#include <string.h>
#include <msclr\marshal.h>

using namespace System;
using namespace msclr::interop;

int main() {
   const char* message = "Test String to Marshal";
   String^ result;
   result = marshal_as<String^>( message );
   return 0;
}

btw: I didn't check this. Just googled it. But, I think, this would work, cause it posted in MSDN.