Re: CString help



One of the odd rules is that if you have a trucating API call, then the deal is that if
you get a value n that is equal to (for non-string values) or equal to one less than (for
string values) the size of the buffer, you need to double the buffer and read again,
repeat until the size read is truly less than the buffer size.
joe

On Fri, 22 Jun 2007 17:03:03 -0500, "Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote:

On Thu, 21 Jun 2007 22:25:43 -0700, "Mihai N." <nmihai_year_2000@xxxxxxxxx>
wrote:

CString::GetBuffer() can increase efficiency in some situations, but is
best avoided, IMHO. You will notice that std::string has no such mechanism.

To be avoided, but still best to have it.
I think it is bad that std::string does not have such a thing.
See my "std::string internal buffer access" note at
http://www.mihai-nita.net/article.php?artID=20060430a

Practically speaking, you can do this:

const int N = 100;
std::string s;
s.resize(N);
int len = WindowsAPI(&s[0], N);
s.resize(len);

Though not guaranteed, I don't know any implementation where this won't
work. (I'd really like the non-contiguity, reference-counted, etc options
to just go away. People who need those things can't do without them, and
the possibility they exist complicates things for everyone else.)

Aside: MSDN has gotten a lot better about defining the meaning of N in the
code above. As a function parameter, it usually indicates the maximum
number of characters that may be written to the buffer, including the
terminating nul. As a return value, it usually indicates the number of
characters actually written, excluding the nul. This is true for functions
like GetWindowText, which truncate the returned text as necessary. What
does this imply for the return value?
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: CString help
    ... MSDN has gotten a lot better about defining the meaning of N in the ... As a function parameter, ... terminating nul. ... characters actually written, excluding the nul. ...
    (microsoft.public.vc.mfc)
  • Re: CString help
    ... As a function parameter, ... characters actually written, excluding the nul. ... string values) the size of the buffer, you need to double the buffer and read again, ...
    (microsoft.public.vc.mfc)
  • [UNIX] wu-ftpd fb_realpath() Off-by-One Bug
    ... Wu-ftpd FTP server contains remotely exploitable off-by-one bug. ... characters while the size of the buffer is MAXPATHLEN characters only. ... Following FTP commands may be used to cause buffer overflow: ...
    (Securiteam)
  • Re: Serial Port CE_OVERRUN errors
    ... SerialNG component. ... The main Input buffer is set to 32000, and I have a variable which is ... updated with the maximum number of characters waiting there each time the ... I tried boosting the baud rate from 57.6k to 115.2k, ...
    (comp.lang.pascal.delphi.misc)
  • Re: WaitCommEvent and ResetEvent for SerialPort
    ... This is extremely true when you say you are exhausting the input buffer, ... This receiver thread will then signal your main ... >> From the WaitCommEvent documentation, ... Since no characters are currently coming it, ...
    (microsoft.public.win32.programmer.kernel)