WinInet ftpCommand Demo1

in Visual Basic® (requires IE5+)



by Rick Meyer       Home

If you have Internet Explorer 5 or greater, a new procedure called ftpCommand is available in the new WinInet.DLL allowing direct FTP commands. This very brief VB project demonstrates how to send a command and how to get the output (if expected).
DO NOT ATTEMPT THIS PROJECT IF IE5+ IS NOT INSTALLED.
The first command sent is CWD for changing the current directory which expects no response.

The second command sent is the LIST command. Notable is the simplicity and speed of acquiring the directory listing. The output contains the permissions which can not be obtained with the FtpFindFirstFile and InternetFindNextFile API's.

Note: This project has no error handling. Normally if no handle is returned (or other disaster) then you check if: Err.LastDllError = ERROR_INTERNET_EXTENDED_ERROR (ERROR_INTERNET_EXTENDED_ERROR = 12003). If true then InternetGetLastResponseInfo API gets the info. Here if an error occurs, the procedure is aborted.

  Instructions for Building this Project:

1. Start a new standard exe.
2. On Form1 put a CommandButton named Command1.
3. On Form1 put a Textbox named Text1.
4. Set the Text1.Multiline property to True.
5. Set the Text1.Scrollbars property to 3-Both.

There is no need to position or size the above controls
on the form since that is all done in the Form_Load.


  Operation:

1. Press F5
2. Make sure your are online
3. Click Command1

Now you are ready for the code. Select all of the following code (by clicking on the word 'Option' three times) and copy it to the clipboard [Ctrl][Insert]. Then paste it into the code window of Form1 with [Shift][Insert]. Variable Key
% As Integer
& As Long
! As Single
# As Double
$ As String
Option Explicit

Const scUserAgent = "vb wininet"

Const INTERNET_SERVICE_FTP = 1
Const INTERNET_OPEN_TYPE_DIRECT = 1
Const INTERNET_INVALID_PORT_NUMBER = 0
Const INTERNET_FLAG_PASSIVE = &H8000000
Const FTP_TRANSFER_TYPE_ASCII = 1
        
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 InternetCloseHandle _
    Lib "wininet.dll" (ByVal hInet As Long) As Integer

Private Declare Function ftpCommand _
    Lib "wininet.dll" Alias "FtpCommandA" ( _
        ByVal hConnect As Long, _
        ByVal fExpectResponse As Boolean, _
        ByVal dwFlags As Long, _
        ByVal lpszCommand As String, _
        dwContext As Long, _
        phFtpCommand As Long) As Boolean

Private Declare Function InternetReadFile _
    Lib "wininet.dll" ( _
        ByVal hConnect As Long, _
        ByVal lpBuffer As String, _
        ByVal dwNumberOfBytesToRead As Long, _
        lpdwNumberOfBytesRead As Long) As Boolean

'=======================================================
' End Declarations - Begin Procedures
'=======================================================
Private Function TrimNull$(ByVal s$)
    Dim pos%
    
    pos = InStr(s, Chr$(0))
    If pos Then s = Left$(s, pos - 1)
    
    TrimNull = Trim$(s)
End Function

Private Sub Command1_Click()
    Const NUMBYTES& = 1020
    Dim hOpen&, hConn&, hOutConn&, buffer$, bytesRead&
    
    hOpen = InternetOpen( _
                scUserAgent, _
                INTERNET_OPEN_TYPE_DIRECT, _
                vbNullString, _
                vbNullString, 0)
    
    DoEvents
    If hOpen = 0 Then Exit Sub
    
    'Note: This is coded for the anonymous Microsoft site
    '  See the Declare above for replacing the three
    '  parameters with your site, username and password.
    hConn = InternetConnect( _
                hOpen, _
                "ftp.microsoft.com", _
                INTERNET_INVALID_PORT_NUMBER, _
                "", _
                "", _
                INTERNET_SERVICE_FTP, _
                INTERNET_FLAG_PASSIVE, 0)
    
    DoEvents
    If hConn = 0 Then GoTo out2
   
' Command to change a Directory
    ftpCommand hConn, _
                False, _
                FTP_TRANSFER_TYPE_ASCII, _
                "CWD developr", _
                0, _
                hOutConn
    
' Command to LIST the Directory
    ftpCommand hConn, _
                True, _
                FTP_TRANSFER_TYPE_ASCII, _
                "LIST", _
                0, _
                hOutConn
    
    DoEvents
    If hOutConn = 0 Then GoTo out1
    
    Text1 = ""
    buffer = Space$(NUMBYTES + 4)

    Do
        InternetReadFile _
                hOutConn, _
                buffer, _
                NUMBYTES, _
                bytesRead
        
        If bytesRead = 0 Then Exit Do
        Text1 = Text1 & TrimNull(buffer)
    Loop

    InternetCloseHandle hOutConn
out1: InternetCloseHandle hConn
out2: InternetCloseHandle hOpen
End Sub

Private Sub Form_Load()
    Move 1000, 1000, 5400, 3600
    Text1.Move 120, 120, 5000, 2500
    Command1.Move 2000, 2760, 1200, 375
End Sub