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. typecasting

typecasting

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 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
    FotisSs
    wrote on last edited by
    #1

    Hi guys I have a simple question for your. Let say that I have CreateFile function that takes as a first argument a LPCTSTR pointer. Also there is a declared buffer as char* szBuffer . I' ve seen a lot of guys using a typecasting for calling CreateFile like this CreateFile((LPCTSTR)szBuffer,....). I simply use CreateFile(szBuffer,....) with no problems Is that typecasting needed, given that both char* and LPCTSTR are pointers. Are there any true gains by using typecasting? Thanx

    C R D 3 Replies Last reply
    0
    • F FotisSs

      Hi guys I have a simple question for your. Let say that I have CreateFile function that takes as a first argument a LPCTSTR pointer. Also there is a declared buffer as char* szBuffer . I' ve seen a lot of guys using a typecasting for calling CreateFile like this CreateFile((LPCTSTR)szBuffer,....). I simply use CreateFile(szBuffer,....) with no problems Is that typecasting needed, given that both char* and LPCTSTR are pointers. Are there any true gains by using typecasting? Thanx

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      FotisSs wrote:

      I' ve seen a lot of guys using a typecasting

      Well, they were wrong... LPCTSTR is an array of TCHAR and the definition of a TCHAR depends if UNICODE is enabled or not. So, the code will work fine if UNICODE is not defined but it will not work if UNICODE is defined. In which case, if you have the typecast, it will simply be plain wrong (the string will be corrupted), but if you don't have the cast, then it will not compile (which is way better, because at least you don't have a nasty bug in your code). Typecasts should be avoided at maximum: using them too much means probably that you don't understand what you are doing. I suggest that you read this article[^], it explains a lot about character encoding (and about TCHAR).

      Cédric Moonen Software developer
      Charting control [v2.0] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      • F FotisSs

        Hi guys I have a simple question for your. Let say that I have CreateFile function that takes as a first argument a LPCTSTR pointer. Also there is a declared buffer as char* szBuffer . I' ve seen a lot of guys using a typecasting for calling CreateFile like this CreateFile((LPCTSTR)szBuffer,....). I simply use CreateFile(szBuffer,....) with no problems Is that typecasting needed, given that both char* and LPCTSTR are pointers. Are there any true gains by using typecasting? Thanx

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        Usage of things like char is what scaring me more than the typecasts. One should ideally use TCHAR instead, which is neutral to builds (and is defined as char in an ANSI build and as a wchar_t in an Unicode build). If an API expects an LPCTSTR, that means that it has both an ANSI and an Unicode implementation and the appropriate version will be called based on the build (for Unicode build, CreateFileW and for an ANSI build, CreateFileA will be called). There is no excuse to use char unless you're *sure* that you are always going to do an ANSI only build. Even then, I won't recommend it. Typecasting a char* to an LPCTSTR in an Unicode build is an invitation to disaster, because the wide version of the API will be called, which is going to expect a wchar_t*. There is no excuse to write such crap code.

        “Follow your bliss.” – Joseph Campbell

        F 1 Reply Last reply
        0
        • R Rajesh R Subramanian

          Usage of things like char is what scaring me more than the typecasts. One should ideally use TCHAR instead, which is neutral to builds (and is defined as char in an ANSI build and as a wchar_t in an Unicode build). If an API expects an LPCTSTR, that means that it has both an ANSI and an Unicode implementation and the appropriate version will be called based on the build (for Unicode build, CreateFileW and for an ANSI build, CreateFileA will be called). There is no excuse to use char unless you're *sure* that you are always going to do an ANSI only build. Even then, I won't recommend it. Typecasting a char* to an LPCTSTR in an Unicode build is an invitation to disaster, because the wide version of the API will be called, which is going to expect a wchar_t*. There is no excuse to write such crap code.

          “Follow your bliss.” – Joseph Campbell

          F Offline
          F Offline
          FotisSs
          wrote on last edited by
          #4

          ok my fault I always tend to use specifically CreateFileA so it is LPSTR! And the question remains is typecasting needed?

          R 1 Reply Last reply
          0
          • F FotisSs

            ok my fault I always tend to use specifically CreateFileA so it is LPSTR! And the question remains is typecasting needed?

            R Offline
            R Offline
            Rajesh R Subramanian
            wrote on last edited by
            #5

            FotisSs wrote:

            And the question remains is typecasting needed?

            I haven't answered it? Take CreateFile for example, which expects an LPCTSTR If you have a TCHAR*, no typecasting is needed, or typecasting has no effect. If you have a char*, passing that to this function by casting it to an LPCTSTR will cause trouble in Unicode Build. If you have a wchar_t*, passing that to this function by casting it to an LPCTSTR will cause trouble in ANSI build. If you use TCHAR*, no casting will be needed at all, and the correct data type will be automatically used. I will also highly recommend that you read the article that the other poster gave you a link for! It's well worth the time you'll spend on it.

            “Follow your bliss.” – Joseph Campbell

            1 Reply Last reply
            0
            • F FotisSs

              Hi guys I have a simple question for your. Let say that I have CreateFile function that takes as a first argument a LPCTSTR pointer. Also there is a declared buffer as char* szBuffer . I' ve seen a lot of guys using a typecasting for calling CreateFile like this CreateFile((LPCTSTR)szBuffer,....). I simply use CreateFile(szBuffer,....) with no problems Is that typecasting needed, given that both char* and LPCTSTR are pointers. Are there any true gains by using typecasting? Thanx

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              FotisSs wrote:

              Is that typecasting needed, given that both char* and LPCTSTR are pointers.

              Unicode issues aside, no, and not because they are both pointers. It's because the function's signature will treat the pointer argument as a const and not change it. On the other hand, if you had a function that was expecting a LPTSTR and you were passing it a const pointer, then casting would be an issue.

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              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