Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using wininet.dll api for ftp in VB.NET

Status
Not open for further replies.

mlowe9

Programmer
Apr 3, 2002
221
US
I found this link


explaining how to use wininet.dll api for FTP in VB. However, I believe this example is VB6, and I am now using VB.NET (although not using it very well yet).

Everything seems to work ok, except I am not getting anything back in my pData variable (the WIN32_FIND_DATA data type). Here is my code, can anyone help me?

Code:
Public Class frm_SMATDownload
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btn_DownloadNow As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btn_DownloadNow = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'btn_DownloadNow
        '
        Me.btn_DownloadNow.Location = New System.Drawing.Point(64, 192)
        Me.btn_DownloadNow.Name = "btn_DownloadNow"
        Me.btn_DownloadNow.Size = New System.Drawing.Size(88, 40)
        Me.btn_DownloadNow.TabIndex = 0
        Me.btn_DownloadNow.Text = "Download Now"
        '
        'frm_SMATDownload
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 373)
        Me.Controls.Add(Me.btn_DownloadNow)
        Me.Name = "frm_SMATDownload"
        Me.Text = "SMAT Download"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
        Const FTP_TRANSFER_TYPE_ASCII = &H1
        Const FTP_TRANSFER_TYPE_BINARY = &H2
        Const INTERNET_DEFAULT_FTP_PORT = 21               ' default for FTP servers
        Const INTERNET_SERVICE_FTP = 1
        Const INTERNET_FLAG_PASSIVE = &H8000000            ' used for FTP connections
        Const INTERNET_OPEN_TYPE_PRECONFIG = 0                    ' use registry configuration
        Const INTERNET_OPEN_TYPE_DIRECT = 1                        ' direct to net
        Const INTERNET_OPEN_TYPE_PROXY = 3                         ' via named proxy
        Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4   ' prevent using java/script/INS
        Const MAX_PATH = 260

    Private Structure FILETIME
        Dim dwLowDateTime As Long
        Dim dwHighDateTime As Long
    End Structure

    Private Structure WIN32_FIND_DATA
        Dim dwFileAttributes As Long
        Dim ftCreationTime As FILETIME
        Dim ftLastAccessTime As FILETIME
        Dim ftLastWriteTime As FILETIME
        Dim nFileSizeHigh As Long
        Dim nFileSizeLow As Long
        Dim dwReserved0 As Long
        Dim dwReserved1 As Long
        Dim cFileName As String
        Dim cAlternate As String
    End Structure

    Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet 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 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 FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, ByVal lpdwCurrentDirectory As Long) As Long
    Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
    Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
    Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean
    Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
    Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (ByVal lpdwError As Long, ByVal lpszBuffer As String, ByVal lpdwBufferLength As Long) As Boolean
    Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, ByVal lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
    Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, ByVal lpvFindData As WIN32_FIND_DATA) As Long
    Const PassiveConnection As Boolean = True

    Private Sub btn_DownloadNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_DownloadNow.Click
        Dim lngInet As Long
        lngInet = InternetOpen("MyFTP Control", 1, vbNullString, vbNullString, 0)
        If lngInet = 0 Then
            MsgBox("InternetOpen FAILED.")
            Exit Sub
        End If

        Dim lngInetConn As Long
        lngInetConn = InternetConnect(lngInet, "cloverleaf2", 0, "hci", "old.maid", 1, 0, 0)
        If lngInetConn = 0 Then
            MsgBox("InternetConnect FAILED.")
            Exit Sub
        End If

        Dim lngHInet As Long
        Dim pData As WIN32_FIND_DATA
        lngHInet = FtpFindFirstFile(lngInetConn, "*", pData, 0, 0)
        If lngHInet = 0 Then
            MsgBox("FtpFindFirstFile FAILED.")
            Exit Sub
        End If

        MsgBox(pData.cFileName)
    End Sub
End Class
 
Writing your own FTP code would be time consuming (read: expensive). Why not buy a 3rd party control that does it for you? It'll be cheaper over the long term.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
1. I'm a programmer (although maybe not a great one), and I like to learn how to do things I don't know already.
2. I don't want to have to go through the bureaucracy of asking permission to buy it.

Thanks for the response, though, it's more than I've gotten from anyone else.

After doing some more research, it looks like I could do something with System.Net.Sockets, but again, I can't seem to find any examples that will work on my machine...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top