473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

WriteFile issue

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?
thanks in advance,
George
Jul 26 '07 #1
6 1899
George wrote:
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);

--
David Wilkinson
Visual C++ MVP
Jul 26 '07 #2
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,

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);

--
David Wilkinson
Visual C++ MVP
Jul 26 '07 #3

"George" <Ge****@discussions.microsoft.comwrote in message
news:66**********************************@microsof t.com...
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.
>

thanks in advance,
George

Jul 26 '07 #4
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
Jul 26 '07 #5
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
Jul 27 '07 #6
Thanks Ben, the method is very helpful.
regards,
George

"Ben Voigt [C++ MVP]" wrote:
>
"George" <Ge****@discussions.microsoft.comwrote in message
news:66**********************************@microsof t.com...
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.


thanks in advance,
George


Jul 27 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chuck Rittersdorf | last post by:
Hi There I am having a problem using the win32 API from VB6. I am trying to send a command string to a printer(zebra TLP 2742) on LPT1 using the folowing API functions CreateFile and...
1
by: Paul | last post by:
Hi, I am extending an existing MFC app to use Unicode (for a Japanese version of the interface elements). The app's purpose is to control a peripheral device through the serial port, and the...
2
by: Carter | last post by:
i have a treeview on a webform (.aspx). when the user selects an appropriate node on the tvw. and clicks on a link button, i'm downloading a corresponding file to the client (from the server). so far...
8
by: Scott C. Reynolds | last post by:
I want to serve a PDF right to a web page (cannot link browser directly to PDF file). Stumbled across Response.WriteFile this morning. On my machine (XP Pro) this worked fine: private void...
11
by: Random | last post by:
I have tried all the Response methods I can think of (WriteFile, BinaryWrite, OutputStream) to write the byte array of a pdf file to the response. The result looks like it's trying, it comes up as...
3
by: Buddy Ackerman | last post by:
I'm trying to write files directly to the client so that it forces the client to open the Save As dialog box rather than display the file. On some occasions the files are very large (100MB+). On...
1
by: Cindy H | last post by:
Hi I used the code below to automatically open a pdf file when the user clicks on a menu option. It works great on local machine, but when I uploaded it to hosting site and then tried it, I...
0
by: Jonathan | last post by:
Hi everyone, I'm having an issue with my Excel streaming. I run my procedure and if I "Save As" when the dialog opens and then open the file I generate, it's perfect. Also, if I choose "Open" when...
3
by: MJP | last post by:
I have a button which kicks off the generation of a report after which the file will be downloaded. The report generation can take a long time, so client side onclick event of the button also...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.