Create an FTP component (get) with VB

Source: Internet
Author: User
Tags anonymous ftp
To create a basic step:

1, with a internetopen call to set up the environment.

2, call the InternetConnect function to connect with the host.

3, call FtpGetFile to reach the file.

4, close the 1th, 2 steps to create the handle, with the InternetCloseHandle function.

Now take a closer look at each step:

1, the environment is set by calling the InternetOpen function. The following is a declaration of VB-specific invocation of this function:


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



Parameter sagent is used to specify the application or entity that calls the WinInet function. To achieve the goal, you can set up FTP control.



Parameter Laccesstype Specifies whether we are connected directly to a host or using a proxy server. If you pass a value of 1, you are connected directly to the host. If 3 is passed, the proxy server is passed. If you pass 0, the connection will be based on

Registry values under HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

ProxyEnable, ProxyServer and ProxyOverride.



We can use parameters Sproxyname and Sproxybypass instead of registering settings to provide proxy servers and IP addresses and names that do not use proxy servers. The basic format for listing agents is Rotocol=protocol://proxy_name:access_port? For example, to specify that port 21 on Proxy1 is a proxy server, use ftp=ftp://proxy1:21 as the sproxyname. Want to spare the OV? Start any host, sproxybypass string should be ov*?



Finally, Lflags is used to show the different choices that affect the result of the function. In our example, we pass 0.

So, when you open an Internet session without using a proxy, we call this:

Lnginet = InternetOpen ("FTP control", 1, vbNullString, vbNullString, 0)

If the function call fails, lnginet is 0. Otherwise, Lnginet will be saved in the next step and will be passed to InternetConnect

The value of the handle to the function.

2, by calling the InternetConnect function to connect with the host. VB specific call to this function declaration is:

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



The first parameter hinternetsession is the handle value returned by the InternetOpen call.

sServerName is the IP address or hostname of the FTP server we are about to connect to.

The nserverport indicates which port to connect to. The value used in our example is 0, which indicates the default port 21.

sUserName and Spassword Pass the user name and password separately.

Lservice is used to indicate the type of service used, such as HTTP, FTP, and so on. Typically, a value of 1 is passed, representing the FTP service.

If you pass x8000000 to the lflags parameter, the connection uses passive FTP semantics. Or, in our case, the transitive 0来 uses the passive semantics.



Finally, when using callback signals, Lcontext is used to identify the relationship between the applications. Because the callback signal is not used in our example, the value is 0.



Now connect to the host FTP.MICROSOFT.COM using an anonymous e-mail user name:

Lnginetconn = InternetConnect (lnginet, "ftp.microsoft.com", 0, _

"Anonymous", "ally@wallyworld.com", 1, 0, 0)



If the function call fails, the Lnginetconn is 0. Instead, Lnginetconn saves the value of the handle that will be passed to FtpGetFile in the next step.



3, now we have implemented the connection, and then we need to invoke ftpgetfile. This function completes all administrative functions related to reading files from an FTP server and storing them locally. VB specific call to this function declaration is:



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



The first parameter hftpsession is the handle value returned by the InternetConnect call.

Lpszremotefile and Lpsznewfile are the file names on the FTP server and the names of the files that will be created on the local machine.

The ffailifexists flag is 0 (replace local file) or 1 (canceled if local file already exists).

dwFlagsAndAttributes is used to specify the file attributes of a local file. Ignored in our example, passing only 0.

The dwflags parameter is specified as 1 to transfer files in ASCII (Class A transmission method), and 2 is to transfer files in binary (1 class transmission methods). Since DIRMAP.TXT is an ASCII text file, we pass the value 1.

Finally, when the callback signal is used, lcontext is used to identify the relationship between the applications. Because the callback signal is not used in our example, the value is 0.

So, here's the DIRMAP.TXT file and the presence of the C:\DIRMAP. TXT call. If the local file already exists, overwrite it.

BLNRC = FtpGetFile (Lnginetconn, "Dirmap.txt", "C:\dirmap.txt", 0, 0, 1, 0)

If the function call succeeds, the BLNRC is True, and false instead.

4, now the file has been received, using InternetCloseHandle to close the connection and session handle. VB specific call to this function declaration is:



Private Declare Function internetclosehandle Lib "Wininet.dll" (ByVal hInet as Long) as Integer.



As indicated, this function has only one parameter hinet, which is the value of the handle to be closed or discarded. Because there are handles in internetconnection and InternetOpen, you need to call this shutdown function two times. In addition, because the internetconnection handle is determined by the InternetOpen handle, the order to close them is the reverse of the creation.

Here is the call function:

InternetCloseHandle Lnginetconn

InternetCloseHandle lnginet



Use this short four steps to complete the FTP get.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.