C ++ Managing Unmanaged Transforms

I made some managed wrappers that deal with packing unlisted code for use in managed ones, but not many that go the other way.

The experiment that I performed included using managed code to search directories and return them to an std vector. In short, I was busy with the following example and noticed a problem.

#include "Helper.h"

#include <msclr/marshal.h>
#include <msclr/marshal_cppstd.h>

using namespace msclr::interop;

using namespace System;

namespace CLIWrapper {

   std::vector<std::string> Helper::GetDirs(const char* root)
   {
      std::vector<std::string> rval;

      String^ path = gcnew System::String(root);
      array<String^,1>^ dirs = System::IO::Directory::GetDirectories(path);

      for (int i=0;  i < dirs->Length; i++)
      {
         //this fails
         std::string nativeString1(marshal_as<std::string>(dirs[i]));

         //this fails as well
         std::string nativeString2(marshal_as<std::string>((String ^ const)dirs[i]));

         // this works
         String ^mStr = dirs[i];
         std::string nativeString(marshal_as<std::string>(mStr));


         rval.push_back(nativeString);
      }

      return rval;
   }

}

Failure for 'nativeString1 and' nativeString2: error C2665: 'msclr :: interop :: marshal_as': none of the three overloads can convert all types of arguments

'nativeString2 uses a constant because it is indicated in one of the marshal_as signatures if you look at the details of the error.

, 'nativeString1, ' nativeString? ?

: , , , .. Im .

+4
2

, dirs[i] ( ). , %, dirs[i]:

// this works as well
auto nativeString1(marshal_as<std::string>(%*dirs[i]));
+3

, ,

template <> inline std::string marshal_as(System::String^ const & _from_obj)

. Ssytem::String. const, , , , , gc .

, , ++, . Temporaries gc, .

, , , .

Justin - . , . .NET .

, const . . marshal_as, .

template <> inline std::string marshal_as(System::String^ const _from_obj)

.

- ,

template <> inline std::string marshal_as(System::String^ const % _from_obj)

, , .

+3

All Articles