[RESOLVED] VB6: Software freezes while downloading a file from web!
Hi.
I have a big software already running in many companies.
As a measure of security, I want to add to it a module that checks in a website (ftp with username & password) if I want the program to block it self, or send messages to user, or update it self, etc.
It download the file perfectly! The problem is it takes a little while to do it (like 3-5 seconds, and if it's offline, like a minute) and during it, the whole software freezes until it ends. I tried moving the routine to another form, but it's the same. I also thought of moving it to an independent exe that triggers when the main one runs, but nowadays Windows is so paranoic it would be very caotic (sandbox, firewall...) it has to do it quietly.
Any suggestions? I'm thinking of doing it when the program closes, or when it's inactive for 1 minute, but still, it's awkward and it just can't freeze like that, my clients are going to report it to me. TO SUM UP, I need it to do it quietly, silently, in background, no freezing (or at least that if it's offline, doesn't freeze for 1 minute).
The code I use is:
Code:
Option Explicit
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB 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
Function InternetGetFile(sURLFileName As String, sSaveToFile As String, Optional bOverwriteExisting As Boolean = False) As Boolean
Dim lRet As Long
Const S_OK As Long = 0, E_OUTOFMEMORY = &H8007000E
Const INTERNET_OPEN_TYPE_PRECONFIG = 0, INTERNET_FLAG_EXISTING_CONNECT = &H20000000
Const INTERNET_OPEN_TYPE_DIRECT = 1, INTERNET_OPEN_TYPE_PROXY = 3
Const INTERNET_FLAG_RELOAD = &H80000000
On Error Resume Next
'Create an internet connection
lRet = InternetOpen("", INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0) '---> in this line it freezes and I can't do anything
If bOverwriteExisting Then
If Len(Dir$(sSaveToFile)) Then
VBA.Kill sSaveToFile
End If
End If
'Check file doesn't already exist
If Len(Dir$(sSaveToFile)) = 0 Then
'Download file
lRet = URLDownloadToFile(0&, sURLFileName, sSaveToFile, 0&, 0)
If Len(Dir$(sSaveToFile)) Then
'File successfully downloaded
InternetGetFile = True
Else
'Failed to download file
If lRet = E_OUTOFMEMORY Then
EscribeLog "Error serv: The buffer length is invalid or there was insufficient memory to complete the operation."
Else
EscribeLog "Error serv: (this is probably a proxy server error)." 'Escribelog is a function on my own, it's "WriteLog" in spanish
End If
InternetGetFile = False
End If
End If
On Error GoTo 0
End Function
and I call it this way:
Code:
Public Sub RevWeb()
On Error Resume Next
Dim ppArch As String, pp As String, sn As String, accion As String, param As String, pplocal As String
ppArch = App.Path + "\" + Format(Date + Time, "ddmmyyhhmmss")
If InternetGetFile("adress/file.txt", ppArch, True) Then '---> in this line it freezes!
'it opens and processes the content of the file, now in hard disk
End If
End Sub
THANK YOU VERY MUCH.
PS: English is not my native language, please excuse my grammar or spelling errors.
Re: VB6: Software freezes while downloading a file from web!
if you can use a shell object for downloading it runs asyncronoulsy
also there is an example in code bank that uses an activex exe (.ocx) to do this
Re: VB6: Software freezes while downloading a file from web!
Quote:
Originally Posted by
westconn1
if you can use a shell object for downloading it runs asyncronoulsy
also there is an example in code bank that uses an activex exe (.ocx) to do this
First of all, thank you for your answer.
...I looked for it but I can't find that example, sorry.
Also I don't understand what you mean by "shell object".
Re: VB6: Software freezes while downloading a file from web!
Are you sure the InternetOpen function is necessary? The URLDownloadToFile function was able to successfully download a small zip attachment from this forum all by itself. However, it did freeze the Form and the IDE while downloading. That's probably due to not passing parameters to pCaller and lpfnCB. If a callback and an IUnknown interface was provided, the UI might not have frozen.
Re: VB6: Software freezes while downloading a file from web!
See Download files from the Web (No Winsock) for a UserControl approach to downloading files.
Re: VB6: Software freezes while downloading a file from web!
Re: VB6: Software freezes while downloading a file from web!
Quote:
Originally Posted by
westconn1
Waw, problem SUPER-solved!
That was exactly what I needed. Thank you so much!