Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. std::string marshalling

std::string marshalling

Scheduled Pinned Locked Moved Managed C++/CLI
questiongraphicshelp
2 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 104196
    wrote on last edited by
    #1

    I am having difficulty handling a vector that is returned from unmanaged code and then converting it to an ArrayList of String. I get the following error: cannot convert parameter 1 from 'std::string' to 'System::Object __gc * How do I Marshal this? #ifndef __STAT_H__ #define __STAT_H__ #include #include // Unmanaged file class CStatus { public: std::vector statuses; CStatus() {} }; #endif // EOF // in Managed file ArrayList statuses = new ArrayList(); CStatus stat; int iReturn = SubmitJob( stat); for (unsigned int ivector = 0; ivector < stat.statuses.size(); ivector++) { statuses->Add((stat.statuses[ivector])); // ERROR occurs } Thanks for all advice.

    J 1 Reply Last reply
    0
    • U User 104196

      I am having difficulty handling a vector that is returned from unmanaged code and then converting it to an ArrayList of String. I get the following error: cannot convert parameter 1 from 'std::string' to 'System::Object __gc * How do I Marshal this? #ifndef __STAT_H__ #define __STAT_H__ #include #include // Unmanaged file class CStatus { public: std::vector statuses; CStatus() {} }; #endif // EOF // in Managed file ArrayList statuses = new ArrayList(); CStatus stat; int iReturn = SubmitJob( stat); for (unsigned int ivector = 0; ivector < stat.statuses.size(); ivector++) { statuses->Add((stat.statuses[ivector])); // ERROR occurs } Thanks for all advice.

      J Offline
      J Offline
      Jeff J
      wrote on last edited by
      #2

      You only have to marshal your strings to CLR Strings, since all CLR reference types (including String) are based on Object. Then you can Add() your Strings to an ArrayList. If your std::strings are strictly lower ASCII (and I do mean 7-bit ASCII, not MBCS/ANSI), the following method will work: using namespace System; using namespace System::Collections; using namespace System::Text; using namespace std; vector vec; string s = "a std::string"; vec.push_back(s); ASCIIEncoding *enc = new ASCIIEncoding(); String *sCopy = new String(vec[0].c_str(), 0, vec[0].size(), enc); ArrayList *al = new ArrayList(); al->Add(sCopy); For anything beyond English, you will need to use either Marshal::PtrToStringAnsi() or MultiByteToWideChar(). I could explain that if it's what you need instead. Cheers

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups