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. const char* to LPCTSTR conversion

const char* to LPCTSTR conversion

Scheduled Pinned Locked Moved Managed C++/CLI
questiondebuggingjson
6 Posts 3 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
    fjlv2005
    wrote on last edited by
    #1

    Hello, How can I convert const char* to LPCTSTR? I tried to use WideCharToMultiByte so that I could have a LPCWSTR then convert to LPCTSTR but the value of LPCWSTR(pdbfilename) on watch during breakpoint at line 9 is "???". (see code below) 1 HANDLE 2 fCreate(const char *fname, UInt32 access, UInt32 shared, UInt32 create, UInt32 attrib, Err *r_) 3 { 4 Err r = errNone; 5 HANDLE handle = NULL; 6 LPSTR holder=(LPSTR)fname; 7 LPCWSTR pdbfilename; 8 WideCharToMultiByte(CP_OEMCP, WC_DEFAULTCHAR,pdbfilename, -1,holder,strlen(fname),NULL, NULL); 9 handle = CreateFile(pdbfilename, access, shared, NULL, create, attrib, NULL); 10 if(handle == INVALID_HANDLE_VALUE) r = ERR_FILE_CREATEOPENFAILED; 11 if(r_ != NULL) 12 *r_ = r; 13 return handle; 14 } Is there something wrong with my WideCharToMultiByte API call? Is there other way to convert const char* to LPCTSTR? Thanks and more power.

    C H 2 Replies Last reply
    0
    • F fjlv2005

      Hello, How can I convert const char* to LPCTSTR? I tried to use WideCharToMultiByte so that I could have a LPCWSTR then convert to LPCTSTR but the value of LPCWSTR(pdbfilename) on watch during breakpoint at line 9 is "???". (see code below) 1 HANDLE 2 fCreate(const char *fname, UInt32 access, UInt32 shared, UInt32 create, UInt32 attrib, Err *r_) 3 { 4 Err r = errNone; 5 HANDLE handle = NULL; 6 LPSTR holder=(LPSTR)fname; 7 LPCWSTR pdbfilename; 8 WideCharToMultiByte(CP_OEMCP, WC_DEFAULTCHAR,pdbfilename, -1,holder,strlen(fname),NULL, NULL); 9 handle = CreateFile(pdbfilename, access, shared, NULL, create, attrib, NULL); 10 if(handle == INVALID_HANDLE_VALUE) r = ERR_FILE_CREATEOPENFAILED; 11 if(r_ != NULL) 12 *r_ = r; 13 return handle; 14 } Is there something wrong with my WideCharToMultiByte API call? Is there other way to convert const char* to LPCTSTR? Thanks and more power.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I thought LPCTSTR WAS a char * ? Anyhow, the easiest way to convert is to use the _bstr_t class, which is a string class that returns wide and narrow strings automagically. Christian Graus - Microsoft MVP - C++

      F 1 Reply Last reply
      0
      • C Christian Graus

        I thought LPCTSTR WAS a char * ? Anyhow, the easiest way to convert is to use the _bstr_t class, which is a string class that returns wide and narrow strings automagically. Christian Graus - Microsoft MVP - C++

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

        Hi Christian, Actually, the function belongs to a library and so the library is restricted to plain C so that it will be portable because I'am developing under Pocket PC environment. Let me rephrase the question, on EVC4 LPCTSTR is unsigned short*, my first parameter (fname) is const char*, so how can i convert const char* to unsigned short*? Thanks a lot Christian, Fjlv2005

        C 1 Reply Last reply
        0
        • F fjlv2005

          Hi Christian, Actually, the function belongs to a library and so the library is restricted to plain C so that it will be portable because I'am developing under Pocket PC environment. Let me rephrase the question, on EVC4 LPCTSTR is unsigned short*, my first parameter (fname) is const char*, so how can i convert const char* to unsigned short*? Thanks a lot Christian, Fjlv2005

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          OK, if you want to do it in C, _bstr_t won't help, it's a class. However, it just calls the API function that you're trying to call now, anyhow. You could check it's source to see if it differs from your syntax. Christian Graus - Microsoft MVP - C++

          F 1 Reply Last reply
          0
          • C Christian Graus

            OK, if you want to do it in C, _bstr_t won't help, it's a class. However, it just calls the API function that you're trying to call now, anyhow. You could check it's source to see if it differs from your syntax. Christian Graus - Microsoft MVP - C++

            F Offline
            F Offline
            fjlv2005
            wrote on last edited by
            #5

            Ok I'll just check the API. Thanks and Have a nice day.

            1 Reply Last reply
            0
            • F fjlv2005

              Hello, How can I convert const char* to LPCTSTR? I tried to use WideCharToMultiByte so that I could have a LPCWSTR then convert to LPCTSTR but the value of LPCWSTR(pdbfilename) on watch during breakpoint at line 9 is "???". (see code below) 1 HANDLE 2 fCreate(const char *fname, UInt32 access, UInt32 shared, UInt32 create, UInt32 attrib, Err *r_) 3 { 4 Err r = errNone; 5 HANDLE handle = NULL; 6 LPSTR holder=(LPSTR)fname; 7 LPCWSTR pdbfilename; 8 WideCharToMultiByte(CP_OEMCP, WC_DEFAULTCHAR,pdbfilename, -1,holder,strlen(fname),NULL, NULL); 9 handle = CreateFile(pdbfilename, access, shared, NULL, create, attrib, NULL); 10 if(handle == INVALID_HANDLE_VALUE) r = ERR_FILE_CREATEOPENFAILED; 11 if(r_ != NULL) 12 *r_ = r; 13 return handle; 14 } Is there something wrong with my WideCharToMultiByte API call? Is there other way to convert const char* to LPCTSTR? Thanks and more power.

              H Offline
              H Offline
              hamster1
              wrote on last edited by
              #6

              Have a look at the macros like A2CT, use MSDN "String Conversion Macros". That was VC6, with VC7 their name has changed a bit (to A2CTEX or something). ---------------------- ~hamster1

              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