?
Solved

2 questions regarding WinInet in C++

Posted on 2007-10-20
7
Medium Priority
?
388 Views
Last Modified: 2013-12-14
Hi,

I'm using WinInet to download a file in C++ (no MFC). Here is the general structure of the calls I make:

HINTERNET hNet = InternetOpen(m_szClientAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
...
HINTERNET hFile = InternetOpenUrl(hNet, m_szUrl, lpszHeaders, 0, dwFlags, 0);
...
do { InternetReadFile(hFile, szBuffer, m_nBufferSize, &lBytesRead) } while (lBytesRead && !m_bAbort)

1. I'm using InternetSetStatusCallback to set a callback function, but it never gets called. This is my call: InternetSetStatusCallback(hNet, _OnWininetStatusCallBack). It doesn't return INTERNET_INVALID_STATUS_CALLBACK.

2. I'm supporting download resuming by passing Range: bytes=x- in lpszHeaders if relevant. In some cases the request is not supportive of download resumes, and after I used seek operation on the local file, the whole file from the server is being written from that point, making the file unusable. How can I determine prior to the actual file reading if the current request supports resuming using WinInet? (cases for example are several servers/server configurations, .net webservices - these will ignore the Range: header).
Also, if the Range: header was not ignored, HttpQueryInfo(hInternet, HTTP_QUERY_CONTENT_LENGTH, szContentLength, &dwSize, NULL) will return the remaining bytes to read. How can I determine what is the complete file size? What will it return if the Range: header requested the exact anount of bytes the file is, or more than it (this will be the case when trying to resume completed files, or files different than the one the user tries to download)?

Stilgar.
0
Comment
Question by:_Stilgar_
  • 4
  • 2
6 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 20127541
To get the callbacks, you need to set the fifth parameter of
    InternetOpen
(the dwFlags value to) INTERNET_FLAG_ASYNC

...AND...
In your call to
   InternetOpenUrl
set the sixth parameter (the dwContext value) to any non-zero value (e.g., 1).  This is not just a good idea -- it is a requirement :-)

I've never tried programming for download resuming -- so many servers just can't cope.  I'll see if I can find out the best way to do it.

-- Dan
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 1500 total points
ID: 20127582
To find out if the server will be able to handle a
   Range: bytes n-m
header, you should look for a response header named
   Accept-Ranges: bytes

It appears that you don't need to know the actual length.  just send a header like:

   Range: bytes 500-

(with nothing after the dash).

-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 20127629
This looks like an excellent discussion with code examples:

   (exerpt from) The Art of C++ By Herbert Schildt
   http://books.google.com/books?id=Pqz1hEPOfVQC&pg=PA159&sig=Hy7WZBOmjZ5xVpJqekXP4hFVBZM
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Author Comment

by:_Stilgar_
ID: 20168847
For the callback question, I found this link very useful: http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnarwebtool/html/msdn_inet.asp.

As it seems, InternetOpenUrl, as a wrapper function for all the other little function, re-uses dwContext in its calls. You would want to use dwContext to pass the pointer to your object. Oh, and there is no need to set INTERNET_FLAG_ASYNC at all - the callback function should work in an synchronous mode as well.

As for HTTP resuming, as it appears it is indeed tricky. As per http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.5, I MAY know whether the server I'm accessing MAY support resuming using the Accept-Range response. I will keep digging, and see what I can find. See the last link for reference on Range, btw, it will help you understand the Range: header.

Stilgar.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 20173817
Indeed, the dwContext value appears to be critical to getting callbacks.  From your first link:

>> The last parameter, dwContext, is a DWORD context value that will be
      sent to the status callback function if one is specified. If this value is zero,
      information will not be sent to the status callback function.

A common use for it to to provide a pointer or an index that can be used to give the callback some related info.  But it appears that the callbacks don't happen at all if the value is 0.
0
 
LVL 14

Author Comment

by:_Stilgar_
ID: 20377470
DanRollins didn't address all my questions, nor did he provided sufficient information to resolve the issues I had, but he sure deserves the points for what he did helped.

Stilgar.
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question