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 System::String* str[] to const char* chars[] array!

Convert System::String* str[] to const char* chars[] array!

Scheduled Pinned Locked Moved Managed C++/CLI
csharpdata-structuresquestion
7 Posts 2 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.
  • C Offline
    C Offline
    Cracked Down
    wrote on last edited by
    #1

    Hi all, I want convert System::String* str[] to const char* chars[] array for one of mu application! System::String* str[] is a parameter to function. And this function is called from c# any have idea how this conversion happens?

    N 1 Reply Last reply
    0
    • C Cracked Down

      Hi all, I want convert System::String* str[] to const char* chars[] array for one of mu application! System::String* str[] is a parameter to function. And this function is called from c# any have idea how this conversion happens?

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

      SaveTigers wrote:

      System::String* str[]

      Does that even compile? I don't think you can write System::String* str[]. Do you want to convert managed string to const char*?

      Navaneeth How to use google | Ask smart questions

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

        SaveTigers wrote:

        System::String* str[]

        Does that even compile? I don't think you can write System::String* str[]. Do you want to convert managed string to const char*?

        Navaneeth How to use google | Ask smart questions

        C Offline
        C Offline
        Cracked Down
        wrote on last edited by
        #3

        Hey thanks for reply Navaneeth, Got it! I am doing something like this

        char *abc[100];
        for(int i = 0; i < ss->Count; i++)
        {
        abc[i] = (char*)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(ss[i])).ToPointer();
        }

        the above code is working file where ss is System::String* ss[] as function argument I spend 2 days to get it working!!! Now the problem is with System::Double dd[][] I want to convert it to double[][] used in Unmanaged C++ how should I use System::Double dd[][] in managed C++ I am getting this sort of error error C2440: 'initializing' : cannot convert from 'double __gc *' to 'double' I tried many permutation but could n0t get through!! Navaneeth any idea how to do this conversion! Thanks in advance!

        N 1 Reply Last reply
        0
        • C Cracked Down

          Hey thanks for reply Navaneeth, Got it! I am doing something like this

          char *abc[100];
          for(int i = 0; i < ss->Count; i++)
          {
          abc[i] = (char*)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(ss[i])).ToPointer();
          }

          the above code is working file where ss is System::String* ss[] as function argument I spend 2 days to get it working!!! Now the problem is with System::Double dd[][] I want to convert it to double[][] used in Unmanaged C++ how should I use System::Double dd[][] in managed C++ I am getting this sort of error error C2440: 'initializing' : cannot convert from 'double __gc *' to 'double' I tried many permutation but could n0t get through!! Navaneeth any idea how to do this conversion! Thanks in advance!

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

          Ahh you are using old syntax. In the new C++/CLI syntax, System::String* is invalid. Old syntax is obsolete and don't use it if you have a compiler that supports new syntax. Here is how you use 2 * 2 double array in C++/CLI.

          array<double, 2>^ doubleArray = gcnew array<double, 2>(2, 2);
          doubleArray[0 , 0] = 10;
          doubleArray[0 , 1] = 11;
          doubleArray[1 , 0] = 12;
          doubleArray[1 , 1] = 13;

          :)

          Navaneeth How to use google | Ask smart questions

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

            Ahh you are using old syntax. In the new C++/CLI syntax, System::String* is invalid. Old syntax is obsolete and don't use it if you have a compiler that supports new syntax. Here is how you use 2 * 2 double array in C++/CLI.

            array<double, 2>^ doubleArray = gcnew array<double, 2>(2, 2);
            doubleArray[0 , 0] = 10;
            doubleArray[0 , 1] = 11;
            doubleArray[1 , 0] = 12;
            doubleArray[1 , 1] = 13;

            :)

            Navaneeth How to use google | Ask smart questions

            C Offline
            C Offline
            Cracked Down
            wrote on last edited by
            #5

            Hey thanks Navneeth! Actually what I am trying to do is... I have on Static DLL which I am exposing to c# using Managed C++ library, so that I can use it directly, will not have overhead of registering and de-registering (id I develop COM DLL to expose static DLL functionality to C# client) Now, the problem is I am Passing "System::Double dd[][]" as argument to Managed C++ as a function argument and I want to convert the passed double array to array used in the function(so that I can forward it to Static DLL), However while doing

            doubleArray[0 , 0] = dd[0][0]; //this statement is throwing error C2440: 'initializing' : cannot convert from 'double __gc *' to 'double'

            I am clueless about how to get rid of this error Can I use this code provided by you

            array<double, 2>^ doubleArray = gcnew array<double, 2>(2, 2);
            doubleArray[0 , 0] = 10;
            doubleArray[0 , 1] = 11;
            doubleArray[1 , 0] = 12;
            doubleArray[1 , 1] = 13;

            or there is any other way to type cast? Thanks in advance Navneeth!!

            N 1 Reply Last reply
            0
            • C Cracked Down

              Hey thanks Navneeth! Actually what I am trying to do is... I have on Static DLL which I am exposing to c# using Managed C++ library, so that I can use it directly, will not have overhead of registering and de-registering (id I develop COM DLL to expose static DLL functionality to C# client) Now, the problem is I am Passing "System::Double dd[][]" as argument to Managed C++ as a function argument and I want to convert the passed double array to array used in the function(so that I can forward it to Static DLL), However while doing

              doubleArray[0 , 0] = dd[0][0]; //this statement is throwing error C2440: 'initializing' : cannot convert from 'double __gc *' to 'double'

              I am clueless about how to get rid of this error Can I use this code provided by you

              array<double, 2>^ doubleArray = gcnew array<double, 2>(2, 2);
              doubleArray[0 , 0] = 10;
              doubleArray[0 , 1] = 11;
              doubleArray[1 , 0] = 12;
              doubleArray[1 , 1] = 13;

              or there is any other way to type cast? Thanks in advance Navneeth!!

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

              You want to accept managed array as parameter and pass that to a native function that expects a native double array, right? Here is how you do it:

              void Function(array<double, 2>^ doubleArray)
              {
              double nativeArray[2][2];
              for(int i = 0; i < 2; i++)
              {
              for(int j = 0; j < 2; j++)
              {
              nativeArray[i][j] = doubleArray[i , j];
              }
              }
              }

              I reiterate, please avoid using the obsolete syntax and use C++/CLI style syntax. Compile with /clr switch rather than /clr:oldsyntax.

              Navaneeth How to use google | Ask smart questions

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

                You want to accept managed array as parameter and pass that to a native function that expects a native double array, right? Here is how you do it:

                void Function(array<double, 2>^ doubleArray)
                {
                double nativeArray[2][2];
                for(int i = 0; i < 2; i++)
                {
                for(int j = 0; j < 2; j++)
                {
                nativeArray[i][j] = doubleArray[i , j];
                }
                }
                }

                I reiterate, please avoid using the obsolete syntax and use C++/CLI style syntax. Compile with /clr switch rather than /clr:oldsyntax.

                Navaneeth How to use google | Ask smart questions

                C Offline
                C Offline
                Cracked Down
                wrote on last edited by
                #7

                thanks 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