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. Conversion between array<String^>^ and const char *const* and back?

Conversion between array<String^>^ and const char *const* and back?

Scheduled Pinned Locked Moved Managed C++/CLI
c++data-structuresquestiondiscussion
8 Posts 4 Posters 0 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.
  • E Offline
    E Offline
    Ed K
    wrote on last edited by
    #1

    Attempting to dig into C++/CLI by wrapping VLC with it. You'd know the very fist thing I run across is conversions between arrays in C++/CLI and C++. Are there any 'conversion' libraries/utilities out there?? Thanks,

    ed ~"Watch your thoughts; they become your words. Watch your words they become your actions. Watch your actions; they become your habits. Watch your habits; they become your character. Watch your character; it becomes your destiny." -Frank Outlaw.

    N 1 Reply Last reply
    0
    • E Ed K

      Attempting to dig into C++/CLI by wrapping VLC with it. You'd know the very fist thing I run across is conversions between arrays in C++/CLI and C++. Are there any 'conversion' libraries/utilities out there?? Thanks,

      ed ~"Watch your thoughts; they become your words. Watch your words they become your actions. Watch your actions; they become your habits. Watch your habits; they become your character. Watch your character; it becomes your destiny." -Frank Outlaw.

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Ed K wrote:

      Conversion between array<String^>^ and const char *const* and back

      You don't need any library functions to do this. It's simple enough to write. Converting String^ to char*

      String^ str = "Hello world!";
      IntPtr p = Marshal::StringToHGlobalAnsi(str);
      char *nativeString = static_cast<char*>(p.ToPointer());
      Marshal::FreeHGlobal(p);

      Converting char* to String^

      const char* native = "hello world";
      String^ managed = gcnew String(f);

      Now you know the basic conversions and it will be easy to create const char *const* from a managed string array. :)

      Best wishes, Navaneeth

      I L 2 Replies Last reply
      0
      • N N a v a n e e t h

        Ed K wrote:

        Conversion between array<String^>^ and const char *const* and back

        You don't need any library functions to do this. It's simple enough to write. Converting String^ to char*

        String^ str = "Hello world!";
        IntPtr p = Marshal::StringToHGlobalAnsi(str);
        char *nativeString = static_cast<char*>(p.ToPointer());
        Marshal::FreeHGlobal(p);

        Converting char* to String^

        const char* native = "hello world";
        String^ managed = gcnew String(f);

        Now you know the basic conversions and it will be easy to create const char *const* from a managed string array. :)

        Best wishes, Navaneeth

        I Offline
        I Offline
        ian__lindsay 0
        wrote on last edited by
        #3

        If you are using .Net 3.5 or newer, you can use the marshalling library: http://msdn.microsoft.com/en-us/library/bb384865.aspx[^] For your example, the marshal_context methods are probably most appropriate, then you can leave RAII to clear up the HGlobal stuff internally. You will still need to loop over the array elements though.

        N 1 Reply Last reply
        0
        • I ian__lindsay 0

          If you are using .Net 3.5 or newer, you can use the marshalling library: http://msdn.microsoft.com/en-us/library/bb384865.aspx[^] For your example, the marshal_context methods are probably most appropriate, then you can leave RAII to clear up the HGlobal stuff internally. You will still need to loop over the array elements though.

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          ian__lindsay wrote:

          For your example, the marshal_context methods are probably most appropriate, then you can leave RAII to clear up the HGlobal stuff internally.

          Correct. Beauty of stack semantics! :)

          Best wishes, Navaneeth

          1 Reply Last reply
          0
          • N N a v a n e e t h

            Ed K wrote:

            Conversion between array<String^>^ and const char *const* and back

            You don't need any library functions to do this. It's simple enough to write. Converting String^ to char*

            String^ str = "Hello world!";
            IntPtr p = Marshal::StringToHGlobalAnsi(str);
            char *nativeString = static_cast<char*>(p.ToPointer());
            Marshal::FreeHGlobal(p);

            Converting char* to String^

            const char* native = "hello world";
            String^ managed = gcnew String(f);

            Now you know the basic conversions and it will be easy to create const char *const* from a managed string array. :)

            Best wishes, Navaneeth

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            N a v a n e e t h wrote:

            It's simple enough to write.

            Really? why does C++ need another type of string for each new string? after all, a string is a string. :laugh:

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            N 1 Reply Last reply
            0
            • L Luc Pattyn

              N a v a n e e t h wrote:

              It's simple enough to write.

              Really? why does C++ need another type of string for each new string? after all, a string is a string. :laugh:

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              Luc Pattyn wrote:

              why does C++ need another type of string for each new string? after all, a string is a string.

              Not sure I got you :confused:

              Best wishes, Navaneeth

              L 1 Reply Last reply
              0
              • N N a v a n e e t h

                Luc Pattyn wrote:

                why does C++ need another type of string for each new string? after all, a string is a string.

                Not sure I got you :confused:

                Best wishes, Navaneeth

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                It seems C++ offers more string types than I have strings in a typical app. Just wondering why that is. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                N 1 Reply Last reply
                0
                • L Luc Pattyn

                  It seems C++ offers more string types than I have strings in a typical app. Just wondering why that is. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #8

                  Luc Pattyn wrote:

                  It seems C++ offers more string types than I have strings in a typical app. Just wondering why that is.

                  Ok. I guess you are talking about the various string types shown in the marshal_context MSDN page. std::string is the only string type that C++ standard provides. Rest everything is non-standard. When it comes to C++/CLI, System::String^ is the standard string type. CString is Microsoft's implementation for MFC. :)

                  Best wishes, Navaneeth

                  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