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 to Cstring

String to Cstring

Scheduled Pinned Locked Moved Managed C++/CLI
c++question
6 Posts 4 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
    Anonymous
    wrote on last edited by
    #1

    Folks, I was wondering if there is a way to convert String(managed type) to Cstring(unmanaged type) in MC++? Appreciate your comments/replies. CHEERS John

    N 1 Reply Last reply
    0
    • A Anonymous

      Folks, I was wondering if there is a way to convert String(managed type) to Cstring(unmanaged type) in MC++? Appreciate your comments/replies. CHEERS John

      N Offline
      N Offline
      Nick Hodapp
      wrote on last edited by
      #2

      #include <vcclr.h>

      then, call PtrToStringChars(), passing the managed string:

      String\* fx\_string = S"Hello, World";
      
      CString mfc\_string = PtrToStringChars(fx\_string);
      

      This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.

      N 1 Reply Last reply
      0
      • N Nick Hodapp

        #include <vcclr.h>

        then, call PtrToStringChars(), passing the managed string:

        String\* fx\_string = S"Hello, World";
        
        CString mfc\_string = PtrToStringChars(fx\_string);
        

        This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #3

        Hello Nick Wouldn't this be the easier way?

        String* s = S"hello";
        CString str = (CString)s;

        Nish


        Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

        N 1 Reply Last reply
        0
        • N Nish Nishant

          Hello Nick Wouldn't this be the easier way?

          String* s = S"hello";
          CString str = (CString)s;

          Nish


          Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

          N Offline
          N Offline
          Nick Hodapp
          wrote on last edited by
          #4

          I had to check to see why this works because it shouldn't - unless there exists a constructor for CString that takes a String*. And there is. CString has a constructor that looks like this - note it does exactly what I prescribed (and pins the pointer, which I think I neglected to do):

          CStringT( System::String\* pString ) :
          	CThisSimpleString( StringTraits::GetDefaultManager() )
          {
          	const wchar\_t \_\_pin\* psz = PtrToStringChars( pString );
          	\*this = psz;
          }
          

          good catch. N This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.

          N D 2 Replies Last reply
          0
          • N Nick Hodapp

            I had to check to see why this works because it shouldn't - unless there exists a constructor for CString that takes a String*. And there is. CString has a constructor that looks like this - note it does exactly what I prescribed (and pins the pointer, which I think I neglected to do):

            CStringT( System::String\* pString ) :
            	CThisSimpleString( StringTraits::GetDefaultManager() )
            {
            	const wchar\_t \_\_pin\* psz = PtrToStringChars( pString );
            	\*this = psz;
            }
            

            good catch. N This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #5

            Nick Hodapp (MSFT) wrote: good catch. :-) Yeah, I was using this when comparing a String* to a CString in my N-Track program (MFC/MC++ mix). But I just realized that, that is inefficient, because each time a CString object was being allocated on the stack :-) Nish


            Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

            1 Reply Last reply
            0
            • N Nick Hodapp

              I had to check to see why this works because it shouldn't - unless there exists a constructor for CString that takes a String*. And there is. CString has a constructor that looks like this - note it does exactly what I prescribed (and pins the pointer, which I think I neglected to do):

              CStringT( System::String\* pString ) :
              	CThisSimpleString( StringTraits::GetDefaultManager() )
              {
              	const wchar\_t \_\_pin\* psz = PtrToStringChars( pString );
              	\*this = psz;
              }
              

              good catch. N This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.

              D Offline
              D Offline
              Doug
              wrote on last edited by
              #6

              Nick, I had a similar problem, this time involving a CString array. I have found the source of the problem expressed in the thread "ADO.NET : SqlDataReader : I need to assign retrieved value to C++ variable": http://www.codeproject.com/script/comments/forums.asp?forumid=3785#xx309754xx but do not yet know why it occurs or how I can fix it. The problem arises with the /clr compiler setting. Here is the code to illustrate this: ///////////////////////////////////////////////////////// #include #include #include "try_ADONET.h" //#using //#using //#using // This is required for the ADO.NET Provider using namespace std; //using namespace System; int Main() { CString busNumberSQL[2]; CString bus = "Hello"; busNumberSQL[1] = bus; return 0; } //////////////////////////////////////////////////////// Without the /clr setting the array of CString busNumberSQL[2] is correctly contructed as is CString bus and the line busNumberSQL[1] = bus; works as expected. This can be seen by putting a breakpoint in at return 0; and seeing the variables in a Watch. Copy the expanded Watch details (Name, Value & Type) to an EXCEL sheet. .................. Next, add the compiler switch /clr and rerun the code. CString bus is constructed correctly BUT something weird happens in the construction of CString busNumberSQL[2]. Now, busNumberSQL[1] = bus; results in the address of bus as an integer being assigned to busNumberSQL[1], and look at the Watch details!! Copy the expanded Watch details (Name, Value & Type) to an EXCEL sheet Name: busNumberSQL; Value: {Length=2}; Type: ATL::CStringT > >[] ...(Note no dimension at end compared to the "no /clr" case. Next Watch line: [0] 2083454756 __int32 Next Watch line: [1] 3103464 __int32 My conclusion is that busNumberSQL[2] is not an array of CString. Of course, I need the /clr in order to use ADO.NET! It seems as though a CString works, but a CString array for some (unknown) cannot even be defined. Do you have any light for me? (I have not got into your previous post yet on Marshall) Best regards Doug. (In case you are wondering I am very much a beginner with C++ and .NET) Doug

              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