WriteFile issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?R2Vvcmdl?=

    WriteFile issue

    Hello everyone,


    The 3rd parameter of WriteFile is number of bytes to write,



    I am wondering if I want to write multi-byte character string or wide
    character string on Windows, how could I get the number of bytes?


    thanks in advance,
    George
  • David Wilkinson

    #2
    Re: WriteFile issue

    George wrote:
    Hello everyone,
    >
    >
    The 3rd parameter of WriteFile is number of bytes to write,
    >

    >
    I am wondering if I want to write multi-byte character string or wide
    character string on Windows, how could I get the number of bytes?
    George:

    MBCS is a non-issue; you are just writing the data.

    LPCTSTR str = _T("Test String");
    size_t nBytes = _tcslen(str)*si zeof(TCHAR);

    --
    David Wilkinson
    Visual C++ MVP

    Comment

    • =?Utf-8?B?R2Vvcmdl?=

      #3
      Re: WriteFile issue

      Thanks David,


      You mean MBCS is always ends with 0 and I can use strlen safely?

      And for the sizeof (TCHAR), could I assume it is always 2 bytes? Are there
      any exceptions?


      regards,
      George

      "David Wilkinson" wrote:
      George wrote:
      Hello everyone,


      The 3rd parameter of WriteFile is number of bytes to write,



      I am wondering if I want to write multi-byte character string or wide
      character string on Windows, how could I get the number of bytes?
      >
      George:
      >
      MBCS is a non-issue; you are just writing the data.
      >
      LPCTSTR str = _T("Test String");
      size_t nBytes = _tcslen(str)*si zeof(TCHAR);
      >
      --
      David Wilkinson
      Visual C++ MVP
      >

      Comment

      • Ben Voigt [C++ MVP]

        #4
        Re: WriteFile issue


        "George" <George@discuss ions.microsoft. comwrote in message
        news:66EF4843-7E1F-4EE2-98A1-B3E86CAC3595@mi crosoft.com...
        Hello everyone,
        >
        >
        The 3rd parameter of WriteFile is number of bytes to write,
        >

        >
        I am wondering if I want to write multi-byte character string or wide
        character string on Windows, how could I get the number of bytes?
        Just use StringCbLength, which always returns the number of bytes regardless
        of whether you are compiled for SBCS, MBCS, or UNICODE.
        >
        >
        thanks in advance,
        George

        Comment

        • David Wilkinson

          #5
          Re: WriteFile issue

          George wrote:
          Thanks David,
          >
          You mean MBCS is always ends with 0 and I can use strlen safely?
          >
          And for the sizeof (TCHAR), could I assume it is always 2 bytes? Are there
          any exceptions?
          George:

          The whole point of TCHAR is that it is one byte in ANSI build and 2
          bytes in Unicode build. This is what enables you to use a single code
          base for either kind of build.

          If you always use Unicode build, then sizeof(TCHAR) is always 2 (and you
          can just use wchar_t and L"" strings). But for me, old habits die hard,
          and I always use TCHAR and _T("") strings.

          --
          David Wilkinson
          Visual C++ MVP

          Comment

          • =?Utf-8?B?R2Vvcmdl?=

            #6
            Re: WriteFile issue

            Thanks David, your answer is clear.


            regards,
            George

            "David Wilkinson" wrote:
            George wrote:
            Thanks David,

            You mean MBCS is always ends with 0 and I can use strlen safely?

            And for the sizeof (TCHAR), could I assume it is always 2 bytes? Are there
            any exceptions?
            >
            George:
            >
            The whole point of TCHAR is that it is one byte in ANSI build and 2
            bytes in Unicode build. This is what enables you to use a single code
            base for either kind of build.
            >
            If you always use Unicode build, then sizeof(TCHAR) is always 2 (and you
            can just use wchar_t and L"" strings). But for me, old habits die hard,
            and I always use TCHAR and _T("") strings.
            >
            --
            David Wilkinson
            Visual C++ MVP
            >

            Comment

            • =?Utf-8?B?R2Vvcmdl?=

              #7
              Re: WriteFile issue

              Thanks Ben, the method is very helpful.


              regards,
              George

              "Ben Voigt [C++ MVP]" wrote:
              >
              "George" <George@discuss ions.microsoft. comwrote in message
              news:66EF4843-7E1F-4EE2-98A1-B3E86CAC3595@mi crosoft.com...
              Hello everyone,


              The 3rd parameter of WriteFile is number of bytes to write,



              I am wondering if I want to write multi-byte character string or wide
              character string on Windows, how could I get the number of bytes?
              >
              Just use StringCbLength, which always returns the number of bytes regardless
              of whether you are compiled for SBCS, MBCS, or UNICODE.
              >


              thanks in advance,
              George
              >
              >
              >

              Comment

              Working...