Convert System::String to char*

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Mulholland

    #1

    Convert System::String to char*

    How can i (using Managed C++) convert a System::String object containing
    basic text into a char* object?

    Thanks in advance,

    Tim


  • Nishant S

    #2
    Re: Convert System::String to char*

    Here's one way :-

    String* s1 = S"............" ;
    const __wchar_t __pin* pstr = PtrToStringChar s(s1);

    --
    Regards,
    Nish [VC++ MVP]



    "Tim Mulholland" <tim@eyerespons e.com> wrote in message
    news:#FYbCf6eDH A.2464@TK2MSFTN GP09.phx.gbl...[color=blue]
    > How can i (using Managed C++) convert a System::String object containing
    > basic text into a char* object?
    >
    > Thanks in advance,
    >
    > Tim
    >
    >[/color]


    Comment

    • Sin

      #3
      Re: Convert System::String to char*

      > Here's one way :-[color=blue]
      >
      > String* s1 = S"............" ;
      > const __wchar_t __pin* pstr = PtrToStringChar s(s1);[/color]

      I think he meant a char*, as in single byte char*... Here's one way :

      String foo= "Hello";
      IntPtr ptr = Marshal::String ToCoTaskMemAnsi (foo);
      char *singleByteFoo= (char*)ptr.ToPo inter();
      // do stuff with singleByteFoo
      Marshal::FreeCo TaskMem(ptr);
      // here, singleByteFoo is not valid anymore

      Alex.


      Comment

      • Nishant S

        #4
        Re: Convert System::String to char*

        My bad! :-(

        --
        Regards,
        Nish [VC++ MVP]



        "Sin" <broa29@hotmail .com> wrote in message
        news:uGF6sp6eDH A.2072@TK2MSFTN GP10.phx.gbl...
        [color=blue]
        > I think he meant a char*, as in single byte char*... Here's one way :
        >[/color]


        Comment

        Working...