Salat
Regular
I used an FTP example from KPD-Team...
then I've compiled this code to AcitveX DLL and in my VB.NET solution add as a reference. To connect via FTP i'm useing such a code:
executing of VB.NET code alweys stops on line r = c.GetFirstFile("*.*") or on line r = c.GetNextFile with the System.NullReferenceExpection (this is my favorite one ). I realy don't know what is wrong. One maybe two times this code run correctly. I supose I did a mistake in declaring 'c', or the code in DLL is coruped. I spend whole day to find out what is wrong... please help me
Visual Basic:
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 MAX_PATH = 260
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String
End Type
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
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, 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" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, 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, lpvFindData As WIN32_FIND_DATA) As Long
Public Enum typeInternetOpenType
Preconfig = 0
Direct = 1
Proxy = 2
PreconfigNoAutoProxy = 4
End Enum
Public Enum typeTransferType
Unknown = 0
Ascii = 1
Binary = 2
End Enum
Public Enum typeItemTypes
Directory = 0
file = 1
End Enum
Public FD_ItemName As String
Public FD_ItemType As typeItemTypes
Public FD_FileAtributes As Long
'Public FD_CreationTime As FILETIME
'Public FD_LastAccessTime As FILETIME
'Public FD_LastWriteTime As FILETIME
Public FD_FileSizeHigh As Long
Public FD_FileSizeLow As Long
Public FD_Reserved0 As Long
Public FD_Reserved1 As Long
Public FD_Alternate As String
Public TransferType As typeTransferType
Public InternetOpenType As typeInternetOpenType
Public hConnection As Long
Public hOpen, hFind As Long
Const PassiveConnection As Boolean = True
Public Function OpenConnection(ByVal Adres As String, ByVal Username As String, ByVal Password As String) As Long
Dim hOpen As Long
hOpen = InternetOpen("FTP Connect Lib -" & Adres, InternetOpenType, vbNullString, vbNullString, 0)
hConnection = InternetConnect(hOpen, Adres, INTERNET_DEFAULT_FTP_PORT, Username, Password, INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
OpenConnection = hConnection
End Function
Public Function CloseConnection()
InternetCloseHandle hConnection
InternetCloseHandle hOpen
End Function
Public Function GetCurrentDir() As String
sOrgPath = String(MAX_PATH, 0)
FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
GetCurrentDir = sOrgPath
End Function
Public Function CreateDir(ByVal DirName As String)
FtpCreateDirectory hConnection, DirName
End Function
Public Function SetCurrentDir(ByVal DirPath As String)
FtpSetCurrentDirectory hConnection, DirPath
End Function
Public Function RemoveDir(ByVal DirPath As String)
FtpRemoveDirectory hConnection, DirPath
End Function
Public Function PutFile(ByVal srcPath As String, DstPath As String)
FtpPutFile hConnection, srcPath, DstPath, TransferType, 0
End Function
Public Function GetFile(ByVal hostPath As String, localPath As String)
FtpGetFile hConnection, hostPath, localPath, False, 0, TransferType, 0
End Function
Public Function RenameFile(ByVal oldName As String, ByVal newName As String)
FtpRenameFile hConnection, oldName, newName
End Function
Public Function RemoveFile(ByVal filePath As String)
FtpDeleteFile hConnection, filePath
End Function
Public Function GetFirstFile(Optional Filter As String = "*.*") As String
Dim r As String
Dim pData As WIN32_FIND_DATA, lRet As Long
pData.cFileName = String(MAX_PATH, 0)
hFind = FtpFindFirstFile(hConnection, Filter, pData, 0, 0)
r = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
FD_ItemName = r
FD_Alternate = pData.cAlternate:
'FD_CreationTime = pData.ftCreationTime
FD_FileAtributes = pData.dwFileAttributes
FD_FileSizeHigh = pData.nFileSizeHigh
FD_FileSizeLow = pData.nFileSizeLow
'FD_LastAccessTime = pData.ftLastAccessTime
'FD_LastWriteTime = pData.ftLastWriteTime
FD_Reserved0 = pData.dwReserved0
FD_Reserved1 = pData.dwReserved1
If InStr(1, r, ".") <> 0 Then FD_ItemType = 1
If InStr(1, r, ".") = 0 Then FD_ItemType = 0
GetFirstFile = r
End Function
Public Function GetNextFile() As String
Dim r As String
Dim pData As WIN32_FIND_DATA, lRet As Long
pData.cFileName = String(MAX_PATH, 0)
lRet = InternetFindNextFile(hFind, pData)
r = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
FD_ItemName = r
FD_Alternate = pData.cAlternate:
'FD_CreationTime = pData.ftCreationTime
FD_FileAtributes = pData.dwFileAttributes
FD_FileSizeHigh = pData.nFileSizeHigh
FD_FileSizeLow = pData.nFileSizeLow
'FD_LastAccessTime = pData.ftLastAccessTime
'FD_LastWriteTime = pData.ftLastWriteTime
FD_Reserved0 = pData.dwReserved0
FD_Reserved1 = pData.dwReserved1
If InStr(1, r, ".") <> 0 Then FD_ItemType = 1
If InStr(1, r, ".") = 0 Then FD_ItemType = 0
GetNextFile = r
End Function
Sub ShowError()
Dim lErr As Long, sErr As String, lenBuf As Long
'get the required buffer size
InternetGetLastResponseInfo lErr, sErr, lenBuf
'create a buffer
sErr = String(lenBuf, 0)
'retrieve the last respons info
InternetGetLastResponseInfo lErr, sErr, lenBuf
'show the last response info
MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
End Sub
Visual Basic:
Dim c As FTPExplorer.FTPLibClass
c = New FTPExplorer.FTPLibClass()
c.InternetOpenType = FTPExplorer.typeInternetOpenType.Preconfig
c.OpenConnection(Adres, User, Password)
If c.hConnection = 0 Then c.ShowError() : Exit Sub
ListView1.Items.Clear()
Dim r As String
Dim picInx As Integer
r = c.GetFirstFile("*.*")
Do While r <> ""
If c.FD_ItemType = FTPExplorer.typeItemTypes.Directory Then picInx = 1
If c.FD_ItemType = FTPExplorer.typeItemTypes.file Then picInx = 0
If r <> "" Then ListView1.Items.Add(r, picInx)
r = c.GetNextFile
Loop
c.CloseConnection()