Newsgroups : Microsoft : microsoft.public.inetsdk.programming.wininet : 2006 Jan : InternetReadFile returns additional characters at the end when reading several b
| Subject: | InternetReadFile returns additional characters at the end when reading several b |
| Posted by: | "Peter Meier" (blackicecu..@gmx.net) |
| Date: | 5 Jan 2006 03:41:49 |
I have the following problem: I am using wininet's InternetReadFile to
read an XML file from a server (find the code snippet below). I have
set a BLOCK_SIZE of 512 bytes to read with each InternetReadFile call.
Sometimes the XML which is read into my lpBuffer looks wellformed, but
sometimes I can see at the end of the file some additional characters,
e.g. if my XML would look correctly like this:
...
</Inspection>
</Task>
0
I sometimes see this:
...
</Inspection>
</Task>ction>
</Task>
0
So it looks like some memory problem and that the buffer is not filled
up correctly? Anyhow if I set the BLOCK_SIZE to 1 then I don't get this
effect anymore, actually I will receive the following result:
...
</Inspection>
</Task>
So here my last character '0' is missing. Does anyone have an
explanation why I have this effect when reading several bytes with
InternetReadFile? How can I solve this problem if I don't want to read
each byte by byte (if I do so my last byte doesn't get read anymore)?
Thanks a lot for any suggestions!
Peter
Here's my code snippet (remark: m_hHttpOpenRequest is the handle
returned by 'HttpOpenRequest'):
BOOL CTest::ReadStream()
{
int BLOCK_SIZE = 512;
DWORD dwBufferLen;
DWORD dwSize = sizeof(DWORD);
if (HttpQueryInfo(m_hHttpOpenRequest, HTTP_QUERY_CONTENT_LENGTH |
HTTP_QUERY_FLAG_NUMBER,
&dwBufferLen, &dwSize, 0))
{
dwBufferLen = dwBufferLen + 1; // the content-length does not
include the '\0' byte!
}
BYTE* lpBuffer = new BYTE[dwBufferLen];
memset(lpBuffer, 0, dwBufferLen);
BYTE *p = lpBuffer;
DWORD dwRemainingBytes, dwBytesToRead;
DWORD dwBytesRead = 1;
while (dwBytesRead > 0)
{
dwRemainingBytes = dwBufferLen - (p - lpBuffer);
dwBytesToRead = min(BLOCK_SIZE, dwRemainingBytes);
if (!InternetReadFile(m_hHttpOpenRequest, (LPVOID)p,
dwBytesToRead, &dwBytesRead))
{
return FALSE;
}
p += dwBytesRead;
}
return TRUE;
}