views:

335

answers:

1

Hi, I want to implement an progress bar in my C++ windows application when downloading a file using WinHTTP. Any idea how to do this? It looks as though the WinHttpSetStatusCallback is what I want to use, but I don't see what notification to look for... or how to get the "percent downloaded"...

Help!

Thanks!

+2  A: 

Per the docs:

WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE

Data is available to be retrieved with WinHttpReadData. The lpvStatusInformation parameter points to a DWORD that contains the number of bytes of data available. The dwStatusInformationLength parameter itself is 4 (the size of a DWORD).

and

WINHTTP_CALLBACK_STATUS_READ_COMPLETE

Data was successfully read from the server. The lpvStatusInformation parameter contains a pointer to the buffer specified in the call to WinHttpReadData. The dwStatusInformationLength parameter contains the number of bytes read.

There may be other relevant notifications, but these two seem to be the key ones. Getting "percent" is not necessarily trivial because you may not know how much data you're getting (not all downloads have content-length set...); you can get the headers with:

WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE

The response header has been received and is available with WinHttpQueryHeaders. The lpvStatusInformation parameter is NULL.

and if Content-Length IS available then the percentage can be computed by keeping track of the total number of bytes at each "data available" notification, otherwise your guess is as good as mine;-).

Alex Martelli