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(hConnection, AddressOf MyCallBack)
lRes = FtpGetFile(hConnection, RemoteFile, LocalFile, False, FTP_RELOAD, dwType, dContext)
End If
lRes = InternetCloseHandle(hConnection)
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("ProjectBackups.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
by: Da_WeaselPosted on 2005-10-11 at 18:04:40ID: 15065619
The reason your program gives you the not responding is because the MyCallBack is getting called anywhere from 400-1200 times each time the button is clicked...since VB is single threaded it becomes overwhelmed with the task of processing all of the MyCallBack fucntion calls and giving you what appears to be blocking. Good old DoEvents saves the day in the same way it always has, it passes control back to the OS and force VB to take a short break, and allow other things happen.
x.html?htt p://vbnet. mvps.org/a pi/ _api/ap ii.htm
In short your doing it the right way.
Sometimes you may want to limit the number of times you call DoEvents by adding a counter and only calling DoEvents every 10, 1000, 100,000 times. In this case I don't think limiting the number of times you call DoEvent will improve this in anyway.
As for making use of the info being passed into your MyCallBack fucntion, it will be a little more difficult than simply using the data since its passing your a LPVOID (Long Pointer to a void) which can't be used by VB. You have to use the MoveMemory and other memory management API calls to copy the data from the structure using the pointer address. It's not terribly complex to do, but can seem a bit daunting the first few times around. Here is a link to an excellent web page that addresses this issue and has a nicely designed example program that shows you just about everything you need to get started with WinInet API.
http://vbnet.mvps.org/inde