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. C / C++ / MFC
  4. Convert CString to const unsigned short *

Convert CString to const unsigned short *

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 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.
  • _ Offline
    _ Offline
    __yash__
    wrote on last edited by
    #1

    convert cstring to const unsigned short * How can i achieve this??

    K N S 3 Replies Last reply
    0
    • _ __yash__

      convert cstring to const unsigned short * How can i achieve this??

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #2

      This is a really bad idea. A CString is an object representing a memory area in the PC. You can interpret to a pointer BUT it should be temporary. => (LPCSTR). (const unsigned short *)(LPCSTR) But there is no unsigned short :doh:!!!! RTFM

      Greetings from Germany

      _ 1 Reply Last reply
      0
      • _ __yash__

        convert cstring to const unsigned short * How can i achieve this??

        N Offline
        N Offline
        Naveen
        wrote on last edited by
        #3

        Use the MultiByteToWideChar() function. Other option is to define the _UNICODE in the project setting preprocessor definitions, so that the CString will be keeping wide characters internally and hence no conversion is requirent

        nave [OpenedFileFinder]

        1 Reply Last reply
        0
        • K KarstenK

          This is a really bad idea. A CString is an object representing a memory area in the PC. You can interpret to a pointer BUT it should be temporary. => (LPCSTR). (const unsigned short *)(LPCSTR) But there is no unsigned short :doh:!!!! RTFM

          Greetings from Germany

          _ Offline
          _ Offline
          __yash__
          wrote on last edited by
          #4

          VARIANT v; First case: v.bstrVal = SysAllocString(sz); //where sz is a OLECHAR FAR* Second case: v.bstrVal =sz.AllocSysString(); //where sz is a CString Second case solved my problem

          K N 2 Replies Last reply
          0
          • _ __yash__

            VARIANT v; First case: v.bstrVal = SysAllocString(sz); //where sz is a OLECHAR FAR* Second case: v.bstrVal =sz.AllocSysString(); //where sz is a CString Second case solved my problem

            K Offline
            K Offline
            KarstenK
            wrote on last edited by
            #5

            The right decription is often half of the solution of the problem!!! :cool: For these String conversion are some API function and macros available. Its better to use them. I dont need them often so I dont remember gut I guess like OLE2CHAR( ):confused:

            Greetings from Germany

            1 Reply Last reply
            0
            • _ __yash__

              VARIANT v; First case: v.bstrVal = SysAllocString(sz); //where sz is a OLECHAR FAR* Second case: v.bstrVal =sz.AllocSysString(); //where sz is a CString Second case solved my problem

              N Offline
              N Offline
              Naveen
              wrote on last edited by
              #6

              yashveer wrote:

              v.bstrVal =sz.AllocSysString();

              dont forget to free it using the SysFreeString().

              nave [OpenedFileFinder]

              _ 1 Reply Last reply
              0
              • N Naveen

                yashveer wrote:

                v.bstrVal =sz.AllocSysString();

                dont forget to free it using the SysFreeString().

                nave [OpenedFileFinder]

                _ Offline
                _ Offline
                __yash__
                wrote on last edited by
                #7

                ya i m actually using SysFreeString() I wouldnt have known about all this. I was searching for a way to convert text to csv.... i found something on msdn online;-) http://support.microsoft.com/kb/179706/ Instead of fixed string given in the code: FillSafeArray(L"John", 0, 0, &saRet); I wanted to use the following: FillSafeArray(str, 0, 0, &saRet); where str is CString and hence....all this

                1 Reply Last reply
                0
                • _ __yash__

                  convert cstring to const unsigned short * How can i achieve this??

                  S Offline
                  S Offline
                  Suneet 03
                  wrote on last edited by
                  #8

                  You can use MultiByteToWideChar may be somethng like this CString str; wchar_t* wch MultiByteToWideChar(CP_ACP,0,str,strlen(str),wch,strlen(str),NULL,NULL) Don't forget to allocate memory to this wchar variable then typecast this wchar to unsigned short* FillSafeArray((unsigned short*)wch ,....) I think this will help u out !!!

                  S 1 Reply Last reply
                  0
                  • S Suneet 03

                    You can use MultiByteToWideChar may be somethng like this CString str; wchar_t* wch MultiByteToWideChar(CP_ACP,0,str,strlen(str),wch,strlen(str),NULL,NULL) Don't forget to allocate memory to this wchar variable then typecast this wchar to unsigned short* FillSafeArray((unsigned short*)wch ,....) I think this will help u out !!!

                    S Offline
                    S Offline
                    Suneet 03
                    wrote on last edited by
                    #9

                    typecast it to const unsigned short* ..... FillSafeArray((const unsigned short*).....

                    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