Discussion:
InternetReadFile buffer size
(too old to reply)
Mo
2006-09-29 23:47:26 UTC
Permalink
Hello All,
i have a basic question, i am using InternetReadFile() to download
images from a server, the images size is variant, currently i am using
a 4K buffer and passing a ptr to it in InternetReadFile

BYTE bDownloadBuffer[4096];
InternetReadFile(hResourceHandle, bDownloadbuffer,
sizeof(bDownloadbuffer), &dwRead )

it seems to be working fine, i am downloading images as big as 30k,
however, i want to avoid getting In suffiecient buffer error so should
i make my buffer size depending on the image size (i can get the image
size using HttpQueryInfo(hHttpOpenRequest, HTTP_QUERY_CONTENT_LENGTH
....))

this means i have to make my download buffer as big as 30k sometimes,
on the stack (in a mobile device is that a good idea)?

i dont understand how InternetReadFile actually works, msdn help
states that "WinINet attempts to write the HTML to the lpBuffer buffer
a line at a time" so does that mean even if the image size is 30k and
my buffer size is 4k, InternetReadFile will not fail because my buffer
is small, and it will read 4k each time until the image is fully
downloaded?

Appreciate the help
Mo
Vladimir Scherbina
2006-10-02 09:57:53 UTC
Permalink
Hi Mo!

[..]
Post by Mo
i have a basic question, i am using InternetReadFile() to download
images from a server, the images size is variant, currently i am using
a 4K buffer and passing a ptr to it in InternetReadFile
BYTE bDownloadBuffer[4096];
InternetReadFile(hResourceHandle, bDownloadbuffer,
sizeof(bDownloadbuffer), &dwRead )
it seems to be working fine, i am downloading images as big as 30k,
however, i want to avoid getting In suffiecient buffer error so should
You would not get this error, because you can call InternetReadFile in a
loop to make sure all data is recieved.
Post by Mo
i make my buffer size depending on the image size (i can get the image
size using HttpQueryInfo(hHttpOpenRequest, HTTP_QUERY_CONTENT_LENGTH
....))
this means i have to make my download buffer as big as 30k sometimes,
on the stack (in a mobile device is that a good idea)?
No. You cannot dynamically allocate memory space in stack, instead use
memory from heap - i.e. new/malloc/VirtuaAlloc. Another thing is: stack size
is limited and if you want to allocate huge buffers stack is bad idea.
Post by Mo
i dont understand how InternetReadFile actually works, msdn help
states that "WinINet attempts to write the HTML to the lpBuffer buffer
a line at a time" so does that mean even if the image size is 30k and
my buffer size is 4k, InternetReadFile will not fail because my buffer
is small, and it will read 4k each time until the image is fully
downloaded?
Yes. InternetReadFile will read 4k of data into your buffer and report you
that you should call it once because there is more data avialable. Here is
the explanation from msdn:

"To ensure all data is retrieved, an application must continue to call the
InternetReadFile function until the function returns TRUE and the
lpdwNumberOfBytesRead parameter equals zero."
--
Vladimir
Loading...