Professional Documents
Culture Documents
applications
by Lamont Adams | Jul 09, 2002 7:00:00 AM
Takeaway: Adding simple send/receive FTP functionality to a Visual Basic application isn't difficult. Lamont
Adams shows you how to do it.
I once saw a demo of a commercial banking application that could update itself on the fly. As the user moved
around the interface, the app would use a live Internet connection to check the company's FTP server for new
versions of components that required frequent updates, such as those having to do with legal compliance, an
area in a constant state of change in the banking industry. Of course, this made for a pretty slow user experience
at times, but it seemed to me to be a sound idea in principle. Putting together and sending updated software to
customers on a semiregular basis occupied a large chunk of our time at that particular company, so this seemed
like the proverbial Holy Grail.
While live updating may be impractical for a variety of reasons, FTP services offer a simple way to give an
application access to frequently updated data files in very widely distributed systems. In this article, I'll go over
an easy way to implement FTP functionality in Microsoft Visual Basic 6 using wininet.dll. I’ll also provide you
with a class library, SimpleFTP, that can give an application simple file-level put and get functionality.
InternetOpen opens an Internet session on behalf of a particular application and returns a session handle
needed to open a connection to an Internet server. It must be called before any of the other WinInet functions
can be used, but don't be fooled into thinking that it actually connects the machine to the Internet, as in opening
a dialup connection. Instead it simply allocates the internal resources the library needs to manage itself and any
server connections you may make later. The declare statement for InternetOpen is:
After opening an Internet session with InternetOpen, you can call InternetConnect to open a connection to a
given server. InternetConnect returns a connection handle that is used with other functions that interact with
the server. The declare statement for InternetConnect looks like:
Ok, so you've opened connections to all these servers. Now how do you close them? Pass the connection (or
session) handle to InternetCloseHandle, which takes care of closing the actual connections and freeing any
resources used by them.
Finally, we have FTPGetFile and FTPPutFile, which are used to download and upload files from and to an FTP
server. These are both high-level, easy-to–use functions, taking care of creating a new file on the appropriate
end and streaming the file's data to or from the server. FTPGetFile includes an option to cancel the transfer if
the local destination file already exists (pass True for fFailIfExists.
The actual dirty work of sending and receiving files is performed in the class' GetFile and PutFile methods,
which you can set to automatically close Internet sessions and connections for you after a transfer via the
Boolean AutoCloseInet and AutoCloseFTP properties. Rudimentary error handling is also provided: If either
method returns false, check the ErrorInfo property for the last few responses sent by the FTP server. This can
help you diagnose the problem.
As advertised by its name, SimpleFTP provides only very simple FTP functionality. Among the other useful
features it lacks are the ability to retrieve a directory listing from the FTP server, the ability to delete or rename
remote files, and support for passive FTP. To learn how to add support for these missing items, plus a bit more,
check out the second installment in this series, coming soon to a Builder site near you.
My Updates
My Contacts
Popular on CBS sites: Fantasy Football | Madden NFL10 | Notebooks | iPhone | Video Game Reviews | Big
Brother | Antivirus Software
© 2009 CBS Interactive Inc. All rights reserved. | Privacy Policy | Terms of Use