Option Explicit
Const INTERNET_OPEN_TYPE_DIRECT = 1
Const INTERNET_OPEN_TYPE_PROXY = 3
Const INTERNET_FLAG_RELOAD = &H80000000
Private Const FLAG_ICC_FORCE_CONNECTION = &H1
Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
Private Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer
Private Declare Function InternetReadFile Lib "wininet" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Private Declare Function InternetOpenUrl Lib "wininet" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Public timeoutcount as long
Private Sub GetFile(sourcefile As String, destfile As String, buffersize As Long, timeoutlevel as long)
Dim hOpen As Long, hFile As Long, sBuffer As String, Ret As Long, oldbuffer As String
'Create a buffer for the file we're going to download
sBuffer = Space(buffersize)
'Create an internet connection
hOpen = InternetOpen(App.EXEName, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
'Open the url
hFile = InternetOpenUrl(hOpen, sourcefile, vbNullString, ByVal 0&, INTERNET_FLAG_RELOAD, ByVal 0&)
Open destfile For Output As #1
Do
If InternetCheckConnection(sourcefile, FLAG_ICC_FORCE_CONNECTION, 0) = False Then
timeoutcount = 0
timeouttimer.enabled = True
timeouttimer.interval = 100
Do Until InternetCheckConnection(sourcefile, FLAG_ICC_FORCE_CONNECTION, 0) = True
Doevents
If timeoutcount = timeoutlevel then
msgbox("Connection Timed Out")
timeouttimer.enabled = False
Exit Sub
End If
Loop
timeouttimer.enabled = False
End If
oldbuffer = sBuffer
InternetReadFile hFile, sBuffer, buffersize, Ret
If sBuffer = oldbuffer Then GoTo cleanup
Print #1, sBuffer
Loop
cleanup:
InternetCloseHandle hFile
InternetCloseHandle hOpen
Close
End Sub
Sub timeouttimer_Timer()
timeoutcount = timeoutcount + 1
End sub