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. String conversion in c++/CLI

String conversion in c++/CLI

Scheduled Pinned Locked Moved Managed C++/CLI
c++question
3 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.
  • R Offline
    R Offline
    Robin Imrie
    wrote on last edited by
    #1

    Hi, I am porting my vc2003 app to vc2008 and have come across somthing like this...

    // this defined
    LPCTSTR lpsz = _T("myString");

    // then later
    // where Afunc is defined as
    // SomeObject* AFunc( String *str );
    SomeObject *obj = Afunc( lpsz );

    under vc2003 this compiles and works file however in vc2008 it seems that i need to do...

    // this defined
    LPCTSTR lpsz = _T("myString");

    // then later
    // where Afunc is defined as
    // SomeObject^ Afunc( String ^str );
    SomeObject ^obj = Afunc( gcnew String(lpsz) );

    the documentation on string literals in C++/CLI migration primer suggest that the following could be done.

    SomeObject ^obj = AFunc( safe_cast<string^>(lpsz) );

    this is wrong as it not compile. I not sure the gcnew string( lpsz ) method is ideal. is there a better way or what is the propper way of doing this. thanks Robin

    L 1 Reply Last reply
    0
    • R Robin Imrie

      Hi, I am porting my vc2003 app to vc2008 and have come across somthing like this...

      // this defined
      LPCTSTR lpsz = _T("myString");

      // then later
      // where Afunc is defined as
      // SomeObject* AFunc( String *str );
      SomeObject *obj = Afunc( lpsz );

      under vc2003 this compiles and works file however in vc2008 it seems that i need to do...

      // this defined
      LPCTSTR lpsz = _T("myString");

      // then later
      // where Afunc is defined as
      // SomeObject^ Afunc( String ^str );
      SomeObject ^obj = Afunc( gcnew String(lpsz) );

      the documentation on string literals in C++/CLI migration primer suggest that the following could be done.

      SomeObject ^obj = AFunc( safe_cast<string^>(lpsz) );

      this is wrong as it not compile. I not sure the gcnew string( lpsz ) method is ideal. is there a better way or what is the propper way of doing this. thanks Robin

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Robin Imrie wrote:

      I not sure the gcnew string( lpsz ) method is ideal. is there a better way or what is the propper way of doing this.

      in general gcnew is how you create managed objects in C++/CLI so that is always a correct way to do it. Of course strings can be different like in Native C++ based on what you need them for. In your code you are creating a native string and then converting it to a managed string. If you only need a managed string then you could do this.

      System::String^ lpsz = "myString";

      There is a series of introductory C++/CLI articles here on CodeProject. I suggest you take advantage of them.

      led mike

      R 1 Reply Last reply
      0
      • L led mike

        Robin Imrie wrote:

        I not sure the gcnew string( lpsz ) method is ideal. is there a better way or what is the propper way of doing this.

        in general gcnew is how you create managed objects in C++/CLI so that is always a correct way to do it. Of course strings can be different like in Native C++ based on what you need them for. In your code you are creating a native string and then converting it to a managed string. If you only need a managed string then you could do this.

        System::String^ lpsz = "myString";

        There is a series of introductory C++/CLI articles here on CodeProject. I suggest you take advantage of them.

        led mike

        R Offline
        R Offline
        Robin Imrie
        wrote on last edited by
        #3

        Thanks mike for that i had suspected as much. Will give the C++/CLI articles a good look.

        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