WriteFile issue

D

David Wilkinson

George said:
Hello everyone,


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

http://msdn2.microsoft.com/en-us/library/aa365747.aspx

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)*sizeof(TCHAR);
 
G

Guest

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
 
B

Ben Voigt [C++ MVP]

George said:
Hello everyone,


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

http://msdn2.microsoft.com/en-us/library/aa365747.aspx

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

David Wilkinson

George said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top