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 problem

Conversion problem

Scheduled Pinned Locked Moved Managed C++/CLI
c++data-structureshelptutorialquestion
4 Posts 2 Posters 3 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.
  • A Offline
    A Offline
    Anthony_Yio
    wrote on last edited by
    #1

    Hello all, Does anyone know how to convert System::String to C++ language char * ? I found a few functions but it convert to wchar_t which is not what i want. Any hints? Secondly, i have a unmanaged c++ function in which prototype in such. BOOL MImImage::Combine(MImImage *pImImage); why is that the parameter appear as class NamespaceXX.MImImage in ILDASM? I want a pointer parameter so that i could pass in an array of MImImage. Thanks in advance.

    J 1 Reply Last reply
    0
    • A Anthony_Yio

      Hello all, Does anyone know how to convert System::String to C++ language char * ? I found a few functions but it convert to wchar_t which is not what i want. Any hints? Secondly, i have a unmanaged c++ function in which prototype in such. BOOL MImImage::Combine(MImImage *pImImage); why is that the parameter appear as class NamespaceXX.MImImage in ILDASM? I want a pointer parameter so that i could pass in an array of MImImage. Thanks in advance.

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

      If you haven't already, you can use Marshal::StringToHGlobalAnsi() in the System::Runtime::InteropServices namespace. That will allocate to regular heap which needs to be freed by calling FreeHGlobal(). If you want to copy the bytes to your own memory space/buffer, you can do it with a little extra work. First, pin the String on the GC heap (tell the GC not to move it), and then convert like any wchar_t array. Roughly, given CLR String "Str": inline wchar_t * PtrToStringArray(System::String *s) { System::String __pin*pps = s; //pin to avoid 1-instruction GC hole in reinterpret_cast System::Byte __pin*bp = reinterpret_cast(s); if( bp != 0 ) bp += System::Runtime::CompilerServices::RuntimeHelpers::OffsetToStringData; return reinterpret_cast(bp); }; wchar_t *pStr = PtrToStringArray(Str); //pin and access array int iChrs = WideCharToMultiByte(CP_ACP, 0, pStr, Str->Length, pYourBuf, iBufLen, NULL, NULL); //do whatever with ANSI string in pYourBuf PtrToStringArray() is my version of PtrToStringData() from vcclr.h, which keeps a String pinned, and gets a pointer to its internal wchar_t array. I use it often in MC++ wrappers. The pinning will release when the block that contains PtrToStringArray() exits. As to your second question, all __gc objects are passed around as pointers, and there is no assumption about pointers possibly being to an array of objects. To do so, you need to declare the param as a gc array: BOOL MImImage::Combine(MImImage *ImImageArray __gc[]); The CLR has a strange syntax for declaring arrays, but that's it. Cheers

      A 1 Reply Last reply
      0
      • J Jeff J

        If you haven't already, you can use Marshal::StringToHGlobalAnsi() in the System::Runtime::InteropServices namespace. That will allocate to regular heap which needs to be freed by calling FreeHGlobal(). If you want to copy the bytes to your own memory space/buffer, you can do it with a little extra work. First, pin the String on the GC heap (tell the GC not to move it), and then convert like any wchar_t array. Roughly, given CLR String "Str": inline wchar_t * PtrToStringArray(System::String *s) { System::String __pin*pps = s; //pin to avoid 1-instruction GC hole in reinterpret_cast System::Byte __pin*bp = reinterpret_cast(s); if( bp != 0 ) bp += System::Runtime::CompilerServices::RuntimeHelpers::OffsetToStringData; return reinterpret_cast(bp); }; wchar_t *pStr = PtrToStringArray(Str); //pin and access array int iChrs = WideCharToMultiByte(CP_ACP, 0, pStr, Str->Length, pYourBuf, iBufLen, NULL, NULL); //do whatever with ANSI string in pYourBuf PtrToStringArray() is my version of PtrToStringData() from vcclr.h, which keeps a String pinned, and gets a pointer to its internal wchar_t array. I use it often in MC++ wrappers. The pinning will release when the block that contains PtrToStringArray() exits. As to your second question, all __gc objects are passed around as pointers, and there is no assumption about pointers possibly being to an array of objects. To do so, you need to declare the param as a gc array: BOOL MImImage::Combine(MImImage *ImImageArray __gc[]); The CLR has a strange syntax for declaring arrays, but that's it. Cheers

        A Offline
        A Offline
        Anthony_Yio
        wrote on last edited by
        #3

        Thanx. Your reply really comes in at a good time. :-)

        J 1 Reply Last reply
        0
        • A Anthony_Yio

          Thanx. Your reply really comes in at a good time. :-)

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

          My pleasure. However, I forgot there were embedded < and > characters in my last post, so the code was clipped. Here is the correct code displayed as non-HTML: inline wchar_t * PtrToStringArray(System::String *s) { System::String __pin*pps = s; //pin to avoid 1-instruction GC hole in reinterpret_cast System::Byte __pin*bp = reinterpret_cast(s); if( bp != 0 ) bp += System::Runtime::CompilerServices::RuntimeHelpers::OffsetToStringData; return reinterpret_cast(bp); }; wchar_t *pStr = PtrToStringArray(Str); //pin and access array int iChrs = WideCharToMultiByte(CP_ACP, 0, pStr, Str->Length, pYourBuf, iBufLen, NULL, NULL); //do whatever with ANSI string in pYourBuf 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