Hello, By Default the WinInet API is Sync which doesn't allow your form to be responsive at all until the file is downloaded, I have managed to finally get WinInet API working in Async operation using the Microsoft article below. I need to know if I set this up correctly, I need expert input on my code. Initially there is always a small delay until it finally gets into the callback proc. But this is much better than it blocking it all together, after about a 3 second delay it will download async and you can perform other operations on the same form, etc.. populate listbox's and such while its downloading..
INFO: Using WinInet APIs Asynchronously Within Visual Basic
http://support.microsoft.com/kb/q189850/When leaving the callback proc empy it seems the form errors our with (not responding) but its still performs async operations. So I had to figure out someway to make the form stop from saying (Not Responding). By adding Doevents call into the CallBack Proc seems to work without problems.
If anyone has done this before could you shed some light on issue. As for now it works perfect when adding DoEvents into the Callback proc, but there has to be something I am doing wrong. Thanks - egl
* Feel free to try out this code and see for yourself *
'------------ FTPasync.cls ----------------
Option Explicit
'
http://support.microsoft.com/kb/q189850/Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet As Long) As Long
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
Private Declare Function InternetConnect Lib "WININET.dll" Alias "InternetConnectA" _
(ByVal hInternetSession As Long, _
ByVal sServerName As String, _
ByVal nServerPort As Integer, _
ByVal sUsername As String, _
ByVal sPassword As String, _
ByVal lService As Long, _
ByVal lFlags As Long, _
ByVal lcontext As Long) As Long
Private Declare Function InternetSetStatusCallback Lib "WININET.dll" _
(ByVal hInternetSession As Long, _
ByVal lpfnInternetCallback As Long) As Long
Private Declare Function FtpGetFile Lib "WININET.dll" Alias "FtpGetFileA" _
(ByVal hFtpSession As Long, _
ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, _
ByVal fFailIfExists As Boolean, _
ByVal dwFlagsAndAttributes As Long, _
ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean
Event AsyncDownloadComplete()
Sub DownloadAsync(RemoteFile As String, _
LocalFile As String, _
HostAddr As String, _
UserID As String, _
Password As String)
Const nFlag As Long = 0
Const dwType As Long = 0
Const lcontext As Long = 2
Const lAccess As Long = 1
Const dContext As Long = 2
Const FTP_DIRECT As Long = 1
Const FTP_SERVICE As Long = 1
Const FTP_RELOAD As Long = &H80000000
Const uAgent As String = "ASYNC"
hOpen = InternetOpen(uAgent, lAccess, vbNullString, vbNullString, FTP_DIRECT)
hConnection = InternetConnect(hOpen, HostAddr, 21, UserID, Password, FTP_SERVICE, nFlag, lcontext)
If hConnection <> 0 Then
lRet = InternetSetStatusCallback(
hConnectio
n, AddressOf MyCallBack)
lRes = FtpGetFile(hConnection, RemoteFile, LocalFile, False, FTP_RELOAD, dwType, dContext)
End If
lRes = InternetCloseHandle(hConne
ction)
lRes = InternetCloseHandle(hOpen)
RaiseEvent AsyncDownloadComplete
End Sub
'----------- modCallback.bas -----------------
Option Explicit
Public hOpen As Long
Public hConnection As Long
Public lRet As Long
Public lRes As Long
Public Sub MyCallBack(ByVal hOpen As Long, _
ByVal dwContext As Long, _
ByVal dwInternetStatus As Long, _
ByVal lpdwStatusInformation As Long, _
ByVal dwStatusInformationLength As Long)
Form1.Caption = "Downloading ASYNC NOW!"
DoEvents 'This works to avoid form error NOT RESPONDING!
End Sub
'Enter FTP settings below:
'------------- Form1 ----------------
Option Explicit
Private WithEvents FA As FTPasync
Private Sub Command1_Click()
Call FA.DownloadAsync("ProjectB
ackups.zip
", _
"D:\ProjectBackups.zip", _
"
ftp.server.com", _
"username", _
"password")
End Sub
Private Sub FA_AsyncDownloadComplete()
MsgBox "Download Complete"
End Sub
Private Sub Form_Load()
If FA Is Nothing Then Set FA = New FTPasync
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not FA Is Nothing Then Set FA = Nothing
End Sub