Results 1 to 6 of 6

Thread: InternetOpenURL(), InternetReadFile() problem... download running away (wininet dll)

  1. #1

    Thread Starter
    Hyperactive Member davem's Avatar
    Join Date
    Dec 2000
    Location
    Gainesville, FL
    Posts
    265

    InternetOpenURL(), InternetReadFile() problem... download running away (wininet dll)

    I have a program that downloads files from the web (automatically) and stores them. I do the downloading with the code below (give to me by someone else on this board!) and it works great... for no reason at all - sometimes the downloads just seem like they don't even try to DL and my program just goes through page after page (kinda like it runs away). Since these are compiled instances I can't see what's wrong... I'm trying to test it in the development environment to see what's up but it's not recreating the problem.

    I am on a cable modem and I have multiple instances of the software working... it'll work fine (sometimes hours after starting, and others just minutes after starting) and then the program will just start running away and going through pages it needs to download... acting like they downloaded but they definately didn't.

    I know I have give nothing to base a hypothesis of what's wrong on, but I was jsut wondering if anyone knows of an inherit problem with this code, something that the cable modem/ISP could do to trigger this behavior, or something I might be doing wrong that could cause this.

    Thanks

    VB Code:
    1. 'downloading for the 'download' bit
    2. Option Explicit
    3.  
    4. Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    5. Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal sURL As String, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
    6. Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
    7. Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
    8.  
    9. Private Const IF_FROM_CACHE = &H1000000
    10. Private Const IF_MAKE_PERSISTENT = &H2000000
    11. Private Const IF_NO_CACHE_WRITE = &H4000000
    12.        
    13. Private Const BUFFER_LEN = 256
    14. Dim source As String
    15.  
    16. Public Function GetURLSource(sURL As String)
    17. On Error GoTo ErrorHandler
    18.  
    19.     Dim sBuffer As String * BUFFER_LEN, iResult As Integer, sData As String
    20.     Dim hInternet As Long, hSession As Long, lReturn As Long
    21.  
    22.     hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)
    23.     If hSession Then hInternet = InternetOpenUrl(hSession, sURL, vbNullString, 0, IF_NO_CACHE_WRITE, 0)
    24.  
    25.     If hInternet Then
    26.         iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    27.         sData = sBuffer
    28.         Do While lReturn <> 0
    29.             iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    30.             sData = sData + Mid(sBuffer, 1, lReturn)
    31.         Loop
    32.     End If
    33.    
    34.     iResult = InternetCloseHandle(hInternet)
    35.  
    36.     GetURLSource = sData
    37.  
    38. Exit Function
    39. ErrorHandler:
    40. 'MsgBox "Sub/Function: GetURLSource" & vbCrLf & vbCrLf & "Encountered error #" & Err.Number & vbCrLf & Err.Description
    41. Resume Next
    42. End Function

    PS: When I unplug my ethernet cable they all 'wait' and stop where they are... then when I plug it back in they resume... this is a great feature but it means that it isn't because the internet connection might be lost or timeout.

  2. #2
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    You only check the lReturn value. If lReturn = 0 you assume the download is complete. I guess there are other circumstances where the number of bytes read can be zero, without reaching the eof (connection problems for instance).
    MSDN says eof is reached when the number of bytes read returns 0 and the function returns True (I guess this is a C++ True, so it will return a 1)

    Try if changing:
    Do While lReturn <> 0
    into
    Do Untill (lReturn = 0 And iResult = 1)
    will solve your problem.

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Oh, I just noticed you only close the urlhandle. You should close the sessionhandle as well. This could be the cause of your problem.

    After you close the URL handle, add this line:
    iResult = InternetCloseHandle(hSession)

    BTW, you could use the same session for other downloads as well, that way you don't need to open a new session every time.
    This could improve your performance as well. Just don't forget to close the handle when your done.

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    GetLastError does not work as expected from VB, because Visual Basic will call this function automatically after an API call. VB returns this value in Err.LastDLLError.
    Change:
    If GetLastError <> 0 Then
    to
    If Err.LastDllError <> 0

  5. #5

    Thread Starter
    Hyperactive Member davem's Avatar
    Join Date
    Dec 2000
    Location
    Gainesville, FL
    Posts
    265
    Ahhh! I see what you are saying... it's interesting because I guess I 'used up' all my sessions but there must be a ton of sessions given to a program because I went through like 2000 downloads before it happened! Are hSessions allocated per program instance... because if you started another instance of my download prog it would work.

    I have posted my new code below, tell me how it looks/if you would change anything. I noticed this code floating around this message board and that's how I got it but I guess it's wrong so hopefully this will help some other people out too :-).

    VB Code:
    1. Public mySession As Long
    2.  
    3. Public Function openInternetSession()
    4. ' This function opens the internet session when the program starts (instead of everytime)
    5. ' remember to close the session or you will get runaways!
    6.     Dim hSession As Long
    7.     hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)
    8.     If Err.LastDllError <> 0 Then
    9.         MsgBox "Download Module: GetURLSource, error opening internet session" & vbCrLf & vbCrLf & "Error:" & vbCrLf & GetLastError
    10.     End If
    11.    
    12.     If hSession Then
    13.         mySession = hSession ' now we will use the global variable 'mySession' to access this internet session
    14.     Else
    15.         MsgBox "Error: Couldn't open internet session!"
    16.     End If
    17. End Function
    18.  
    19. Public Function closeInternetSession()
    20. ' Close the internet session
    21.     Dim iResult As Integer
    22.     iResult = InternetCloseHandle(mySession)
    23. End Function
    24.  
    25. Public Function GetURLSource(sURL As String)
    26. On Error GoTo ErrorHandler
    27.  
    28.     Dim sBuffer As String * BUFFER_LEN, iResult As Integer, sData As String
    29.     Dim hInternet As Long, lReturn As Long
    30.  
    31.     hInternet = InternetOpenUrl(mySession, sURL, vbNullString, 0, IF_NO_CACHE_WRITE, 0)
    32.  
    33.     If hInternet Then
    34.         iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    35.         sData = sBuffer
    36.         Do While lReturn <> 0
    37.             iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    38.             sData = sData + Mid(sBuffer, 1, lReturn)
    39.         Loop
    40.     End If
    41.    
    42.     iResult = InternetCloseHandle(hInternet)
    43.     GetURLSource = sData
    44.    
    45. Exit Function
    46. ErrorHandler:
    47. MsgBox "Sub/Function: GetURLSource" & vbCrLf & vbCrLf & "Encountered error #" & Err.Number & vbCrLf & Err.Description
    48. Resume Next
    49. End Function

    That look about right?

    Thanks a lot franz!

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Originally posted by davem
    That look about right?

    Thanks a lot franz!
    I think it looks about right, but I guess the real question would be: Did it solve your problem?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width