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. System::String^ to const WCHAR* ?

System::String^ to const WCHAR* ?

Scheduled Pinned Locked Moved Managed C++/CLI
csharpvisual-studioquestion
7 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.
  • F Offline
    F Offline
    Fritzables
    wrote on last edited by
    #1

    I am trying to write an app which requires me to convert a String^ to a WCHAR but not having much luck with a resolution. Can anyone point out where I am going wrong ? I'm using Visual Studio 2005 and it's a console application. Pete

    L G U 3 Replies Last reply
    0
    • F Fritzables

      I am trying to write an app which requires me to convert a String^ to a WCHAR but not having much luck with a resolution. Can anyone point out where I am going wrong ? I'm using Visual Studio 2005 and it's a console application. Pete

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

      Fritzables wrote:

      Can anyone point out where I am going wrong ?

      Just a guess but you are not using the documentation, examples and samples found on msdn.microsoft.com.

      led mike

      F 1 Reply Last reply
      0
      • L led mike

        Fritzables wrote:

        Can anyone point out where I am going wrong ?

        Just a guess but you are not using the documentation, examples and samples found on msdn.microsoft.com.

        led mike

        F Offline
        F Offline
        Fritzables
        wrote on last edited by
        #3

        G'Day led mike, Well, ya could be right despite the fact I have tried a number of times creating different search strings in the hope I may find something that steers me in the right direction. I am pretty new to C++ as I have recently come across from using Borland's Delphi, so I will use that as my excuse. :) Pete

        L 1 Reply Last reply
        0
        • F Fritzables

          I am trying to write an app which requires me to convert a String^ to a WCHAR but not having much luck with a resolution. Can anyone point out where I am going wrong ? I'm using Visual Studio 2005 and it's a console application. Pete

          G Offline
          G Offline
          George L Jackson
          wrote on last edited by
          #4

          I hope this example helps: #include using namespace System; using namespace System::Runtime::InteropServices; int main(array ^args) { String ^gcString = L"Hello, World!"; IntPtr intptr = IntPtr::Zero; try { intptr = Marshal::StringToHGlobalUni(gcString); const wchar_t *hgString = static_cast(intptr.ToPointer()); std::wcout << hgString << std::endl; } catch (Exception ^e) { Console::WriteLine(e->Message); } finally { if (intptr != IntPtr::Zero) { Marshal::FreeHGlobal(intptr); } } return 0; } -- modified at 19:20 Wednesday 29th November, 2006

          1 Reply Last reply
          0
          • F Fritzables

            G'Day led mike, Well, ya could be right despite the fact I have tried a number of times creating different search strings in the hope I may find something that steers me in the right direction. I am pretty new to C++ as I have recently come across from using Borland's Delphi, so I will use that as my excuse. :) Pete

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

            Fritzables wrote:

            despite the fact I have tried a number of times creating different search strings

            That is easy to believe. Keep at it though. Developing the "skill" to find information is a critical skill in our field. If this helps at all, I use Google to do my searching and when I expect that MSDN will contain the information I seek I put "MSDN" as the first keyword in the Google search phrase. I find that Google provides much better results than the MSDN search engine. But that's just me. :-D Anyway using your question as an example my Google search string would be MSDN System.String convert WCHAR Try it out.

            led mike

            1 Reply Last reply
            0
            • F Fritzables

              I am trying to write an app which requires me to convert a String^ to a WCHAR but not having much luck with a resolution. Can anyone point out where I am going wrong ? I'm using Visual Studio 2005 and it's a console application. Pete

              U Offline
              U Offline
              uusheikh
              wrote on last edited by
              #6

              I use this easy way, but it is not recommended for large strings. String ^s = "Hello world"; CString str = s; WCHAR *w = str.GetBuffer(); WCHAR *myChar = new WCHAR[str.GetLength() +1]; memcpy(myChar,w,str.GetLength()*sizeof(WCHAR)); myChar[str.GetLength()] = '\0'; // Just display the WCHAR AfxMessageBox(myChar);

              F 1 Reply Last reply
              0
              • U uusheikh

                I use this easy way, but it is not recommended for large strings. String ^s = "Hello world"; CString str = s; WCHAR *w = str.GetBuffer(); WCHAR *myChar = new WCHAR[str.GetLength() +1]; memcpy(myChar,w,str.GetLength()*sizeof(WCHAR)); myChar[str.GetLength()] = '\0'; // Just display the WCHAR AfxMessageBox(myChar);

                F Offline
                F Offline
                Fritzables
                wrote on last edited by
                #7

                Thanks all for the help on this one - much appreciated. Fritzables.

                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