S
Sjaakie Helderhorst
Hi all,
I'm trying to create a class which handles FTP. Knowing little of classes
I'm having some trouble making things work. Also having trouble finding the
meaning of some error-codes. The code below results in a lastdllerror 6 (and
no transfer)... can't find what this error means and how to solve it (yes I
already tried google). Could someone please help me out?
This code should, on button-click, connect to ftp-server (192.168.20.1), get
a file 'test.txt' from its root and store this file into 'c:\temp\test.txt'.
Result-string to 'label1'.
I created a class 'Ftp' to achieve this, without any luck sofar...
Thanks in advance!
### [ Class ]
Public Class Ftp
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 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
Dim strServer, strLogin, strPassword As String
Dim INet, INetConn As Long
Property Server() As String
Get
Return strServer
End Get
Set(ByVal Value As String)
strServer = Value
End Set
End Property
Property Login() As String
Get
Return strLogin
End Get
Set(ByVal Value As String)
strLogin = Value
End Set
End Property
Property Password() As String
Get
Return strPassword
End Get
Set(ByVal Value As String)
strPassword = Value
End Set
End Property
Public Function showError()
Dim lErr As Long, sErr As String, lenBuf As Long
InternetGetLastResponseInfo(lErr, sErr, lenBuf)
sErr = lenBuf.ToString
InternetGetLastResponseInfo(lErr, sErr, lenBuf)
Return ("Error " + CStr(lErr) + ": " + sErr)
End Function
Public Sub openConnection()
INet = InternetOpen("AceGroup FTP", 0, vbNullString, vbNullString,
0)
INetConn = InternetConnect(INet, Server, 21, Login, Password, 1, 0,
0)
End Sub
Public Sub closeConnection()
InternetCloseHandle(INetConn)
InternetCloseHandle(INet)
INet = 0
INetConn = 0
End Sub
Public Function putFile(ByVal localFile As String, ByVal remoteFile As
String) As String
Dim result As String
Try
If INet = 0 Then openConnection()
result = FtpPutFile(INetConn, localFile, remoteFile, 0,
0).ToString
Catch ex As Exception
result = ex.ToString()
Finally
closeConnection()
End Try
Return result
End Function
Public Function getFile(ByVal remoteFile As String, ByVal localFile As
String) As String
Dim result As String
Try
If INet = 0 Then openConnection()
result = FtpGetFile(INetConn, remoteFile, localFile, 0, 0, 1,
0).ToString()
Catch ex As Exception
result = ex.ToString()
Finally
closeConnection()
End Try
Return result
End Function
End Class
### [Webform]
[ ... ]
Dim cFtp As Ftp = New Ftp
Dim Result As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
cFtp.Server = "192.168.20.1"
cFtp.Login = "[ my login ]"
cFtp.Password = "[ my pass ]"
cFtp.openConnection()
Result = cFtp.getFile("test.txt", "c:\temp\test.txt")
cFtp.closeConnection()
' output result to label
label1.text = Result
End Sub
I'm trying to create a class which handles FTP. Knowing little of classes
I'm having some trouble making things work. Also having trouble finding the
meaning of some error-codes. The code below results in a lastdllerror 6 (and
no transfer)... can't find what this error means and how to solve it (yes I
already tried google). Could someone please help me out?
This code should, on button-click, connect to ftp-server (192.168.20.1), get
a file 'test.txt' from its root and store this file into 'c:\temp\test.txt'.
Result-string to 'label1'.
I created a class 'Ftp' to achieve this, without any luck sofar...
Thanks in advance!
### [ Class ]
Public Class Ftp
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 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
Dim strServer, strLogin, strPassword As String
Dim INet, INetConn As Long
Property Server() As String
Get
Return strServer
End Get
Set(ByVal Value As String)
strServer = Value
End Set
End Property
Property Login() As String
Get
Return strLogin
End Get
Set(ByVal Value As String)
strLogin = Value
End Set
End Property
Property Password() As String
Get
Return strPassword
End Get
Set(ByVal Value As String)
strPassword = Value
End Set
End Property
Public Function showError()
Dim lErr As Long, sErr As String, lenBuf As Long
InternetGetLastResponseInfo(lErr, sErr, lenBuf)
sErr = lenBuf.ToString
InternetGetLastResponseInfo(lErr, sErr, lenBuf)
Return ("Error " + CStr(lErr) + ": " + sErr)
End Function
Public Sub openConnection()
INet = InternetOpen("AceGroup FTP", 0, vbNullString, vbNullString,
0)
INetConn = InternetConnect(INet, Server, 21, Login, Password, 1, 0,
0)
End Sub
Public Sub closeConnection()
InternetCloseHandle(INetConn)
InternetCloseHandle(INet)
INet = 0
INetConn = 0
End Sub
Public Function putFile(ByVal localFile As String, ByVal remoteFile As
String) As String
Dim result As String
Try
If INet = 0 Then openConnection()
result = FtpPutFile(INetConn, localFile, remoteFile, 0,
0).ToString
Catch ex As Exception
result = ex.ToString()
Finally
closeConnection()
End Try
Return result
End Function
Public Function getFile(ByVal remoteFile As String, ByVal localFile As
String) As String
Dim result As String
Try
If INet = 0 Then openConnection()
result = FtpGetFile(INetConn, remoteFile, localFile, 0, 0, 1,
0).ToString()
Catch ex As Exception
result = ex.ToString()
Finally
closeConnection()
End Try
Return result
End Function
End Class
### [Webform]
[ ... ]
Dim cFtp As Ftp = New Ftp
Dim Result As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
cFtp.Server = "192.168.20.1"
cFtp.Login = "[ my login ]"
cFtp.Password = "[ my pass ]"
cFtp.openConnection()
Result = cFtp.getFile("test.txt", "c:\temp\test.txt")
cFtp.closeConnection()
' output result to label
label1.text = Result
End Sub