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. Convert string array from C# to C++ ?

Convert string array from C# to C++ ?

Scheduled Pinned Locked Moved Managed C++/CLI
questioncsharpc++graphicsdata-structures
6 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.
  • T Offline
    T Offline
    taibc
    wrote on last edited by
    #1

    Hi everyone, I have a string array in C# includes 4 strings: {"string1", "string2", "string3", "string4"} In C#, I can use List or string[] to process. But now I want to send this array to a function in C++ (may be is vector). How can I do that ? and what is the suitable type of string array in C++ Thanks and regards, Tai

    F 1 Reply Last reply
    0
    • T taibc

      Hi everyone, I have a string array in C# includes 4 strings: {"string1", "string2", "string3", "string4"} In C#, I can use List or string[] to process. But now I want to send this array to a function in C++ (may be is vector). How can I do that ? and what is the suitable type of string array in C++ Thanks and regards, Tai

      F Offline
      F Offline
      Freak30
      wrote on last edited by
      #2

      C# string[] becomes array<String^>^ in C++. List becomes List<String^>^. Remember you need to allocate managed types with gcnew in C++. (The regular new is for unmanaged types.) The conversion is done automatically on function call. In fact it isn't even a real conversion, as the meaning/memory layout is the same, just the expression looks different. So you declare a function taking a array<String^>^ and pass in a string[] or the other way araound. Keep in mind, you can't have C# and C++ in the same module (exe/dll) so you need to separate the code into different ones.

      J T 2 Replies Last reply
      0
      • F Freak30

        C# string[] becomes array<String^>^ in C++. List becomes List<String^>^. Remember you need to allocate managed types with gcnew in C++. (The regular new is for unmanaged types.) The conversion is done automatically on function call. In fact it isn't even a real conversion, as the meaning/memory layout is the same, just the expression looks different. So you declare a function taking a array<String^>^ and pass in a string[] or the other way araound. Keep in mind, you can't have C# and C++ in the same module (exe/dll) so you need to separate the code into different ones.

        J Offline
        J Offline
        John Schroedl
        wrote on last edited by
        #3

        Freak30's answer is good and thinking about it I realize you may want to get the string value to native C++ code instead of just to C++/CLI. If that's the case, you'll want to look into the marshal_as templates. ex.

        #include

        . . .

        const char * message = "test";
        String^ managed = marshal_as( message );

        1 Reply Last reply
        0
        • F Freak30

          C# string[] becomes array<String^>^ in C++. List becomes List<String^>^. Remember you need to allocate managed types with gcnew in C++. (The regular new is for unmanaged types.) The conversion is done automatically on function call. In fact it isn't even a real conversion, as the meaning/memory layout is the same, just the expression looks different. So you declare a function taking a array<String^>^ and pass in a string[] or the other way araound. Keep in mind, you can't have C# and C++ in the same module (exe/dll) so you need to separate the code into different ones.

          T Offline
          T Offline
          taibc
          wrote on last edited by
          #4

          Thanks Freak30, I am using Visual Studio 2010. I tried to send string[] in C# to string[] in C++, but not successful In C#, I used below codes:

          [DllImport("DgnShpConverter.dll")]
          public static extern Boolean ConvertDgnToShp(string source, string destinationPath, string shpFileName, string [] strArray);

          And in C++ (file DgnShpConverter.dll), I used below codes:

          __declspec(dllexport) bool ConvertDgnToShp(const char* sourceFile, const char *destinationPath, const char *shpFileName, string myArray[])
          {
          // do something ....
          }

          Do you know how to fix this ? Thanks and kind regards, Tai

          K 1 Reply Last reply
          0
          • T taibc

            Thanks Freak30, I am using Visual Studio 2010. I tried to send string[] in C# to string[] in C++, but not successful In C#, I used below codes:

            [DllImport("DgnShpConverter.dll")]
            public static extern Boolean ConvertDgnToShp(string source, string destinationPath, string shpFileName, string [] strArray);

            And in C++ (file DgnShpConverter.dll), I used below codes:

            __declspec(dllexport) bool ConvertDgnToShp(const char* sourceFile, const char *destinationPath, const char *shpFileName, string myArray[])
            {
            // do something ....
            }

            Do you know how to fix this ? Thanks and kind regards, Tai

            K Offline
            K Offline
            Karthik Kalyanasundaram
            wrote on last edited by
            #5

            Please check this link Interoperation: C# and native Win32 C++ code; arrays of strings[^]

            Do more work Make more mistakes Learn more things

            T 1 Reply Last reply
            0
            • K Karthik Kalyanasundaram

              Please check this link Interoperation: C# and native Win32 C++ code; arrays of strings[^]

              Do more work Make more mistakes Learn more things

              T Offline
              T Offline
              taibc
              wrote on last edited by
              #6

              Hi Karthik, Thank you very much. Your link is very useful. I will try it. Kind regards, Tai

              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