Dll od podstaw
FTP dla VB!
Chyba każdy z
internałtów wie co to FTP. FTP to skrót od File Transfer Protocol, czyli protokół
transmisji plików. I pomyśleć, że w VB da się stworzyć klienta FTP, a co za tym
idzie nawet Chat'a !!! (mam nawet taki projekt). Jak i w wielu sprawach i tu przydadzą
się sztywne deklaracje API:
Global Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
Global Const FTP_TRANSFER_TYPE_ASCII = &H1
Global Const FTP_TRANSFER_TYPE_BINARY = &H2
Global Const INTERNET_DEFAULT_FTP_PORT = 21 ' default for FTP servers
Global Const INTERNET_SERVICE_FTP = 1
Global Const INTERNET_FLAG_PASSIVE = &H8000000 ' used for FTP connections
Global Const INTERNET_OPEN_TYPE_PRECONFIG = 0 ' use registry configuration
Global Const INTERNET_OPEN_TYPE_DIRECT = 1 ' direct to net
Global Const INTERNET_OPEN_TYPE_PROXY = 3 ' via named proxy
Global Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4 ' prevent using
java/script/INS
Global Const MAX_PATH = 260
Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
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 * 14
End Type
Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As
Integer
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
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
Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias
"FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As
String) As Boolean
Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias
"FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory
As String, lpdwCurrentDirectory As Long) As Long
Declare Function FtpCreateDirectory Lib "wininet.dll" Alias
"FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String)
As Boolean
Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias
"FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String)
As Boolean
Declare Function FtpDeleteFile Lib "wininet.dll" Alias
"FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As
Boolean
Declare Function FtpRenameFile Lib "wininet.dll" Alias
"FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal
lpszNew As String) As Boolean
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
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
Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias
"InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String,
lpdwBufferLength As Long) As Boolean
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
Declare Function InternetFindNextFile Lib "wininet.dll" Alias
"InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As
Long
Global Const PassiveConnection As Boolean = True
I co biedny programisto
miałbyś z tym zrobić? Powiem Ci, wkleić do modułu i się nie martwić ogromem
funkcji. Przygotowałem prosty Engine do obsługi FTP. Oto i on:
Global FTPConnection As Long
Global FTPOpen As Long
Global FTPROOT As String
Public Sub InitFTP()
'// Initializacja połączenia
DoEvents
FTPOpen = InternetOpen("Doogie FTP Client Module", INTERNET_OPEN_TYPE_PRECONFIG,
vbNullString, vbNullString, 0)
DoEvents
End Sub
Public Sub FTPConnect(strFTPadress As String, strFTPuser As String, strFTPpassword As
String)
'// połączenie z danym serwerem
On Error GoTo fin
FTPConnection = InternetConnect(FTPOpen, strFTPadress, INTERNET_DEFAULT_FTP_PORT,
strFTPuser, strFTPpassword, INTERNET_SERVICE_FTP, IIf(PassiveConnection,
INTERNET_FLAG_PASSIVE, 0), 0)
FTPROOT = String(255, 0)
FtpGetCurrentDirectory FTPConnection, FTPROOT, Len(FTPROOT)
fin:
End Sub
Public Sub FTPCreateDir(strDirName As String)
On Error GoTo fin
FtpCreateDirectory FTPConnection, strDirName
fin:
End Sub
Public Sub FTPSetCDir(strDirName As String)
On Error GoTo fin
FtpSetCurrentDirectory FTPConnection, strDirName
fin:
End Sub
Public Sub FTPUpload(strFrom As String, strTo As String)
On Error GoTo fin
FtpPutFile FTPConnection, strFrom, strTo, FTP_TRANSFER_TYPE_UNKNOWN, 0
fin:
End Sub
Public Sub FTPRenameFTPFile(strOrgName As String, strNewName As String)
On Error GoTo fin
FtpRenameFile FTPConnection, strOrgName, strNewName
fin:
End Sub
Public Sub FTPDownload(strFrom As String, strTo As String)
On Error GoTo fin
FtpGetFile FTPConnection, strFrom, strTo, False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0
fin:
End Sub
Public Sub FTPKillFile(strFile As String)
On Error GoTo fin
FtpDeleteFile FTPConnection, strFile
fin:
End Sub
Public Function FTPGetCDir() As String
On Error GoTo fin
Dim Directory As String * 255
FtpGetCurrentDirectory FTPConnection, Directory, Len(Directory)
FTPGetCDir = Directory
fin:
End Function
Public Sub FTPKillDir(strDirName As String)
On Error GoTo fin
FtpRemoveDirectory FTPConnection, strDirName
fin:
End Sub
Public Sub FTPLogOut()
On Error GoTo fin
InternetCloseHandle FTPConnection
fin:
End Sub
Public Sub UnloadFTP()
On Error GoTo fin
InternetCloseHandle FTPOpen
fin:
End Sub
To też sobie wklej do
modułu. Nie będę się zajmował teorią, bo jeśli miałbym tu pisać o protokołach,
systemach i podsystemach, serwerach itp. to dokument ten mógłby zapełnić nie jedną
skrzynkę pocztową.
Jak użyć
Najpierw: InitFtp, potem
FtpConnect i to co chcesz zrobić, na koniec FtpLogOut i jeśli nie chcesz się łączyć
z innym serwerem to UnloadFtp.
Funkcje są tak napisane,
że każdy powinien je zrozumieć.
Niema tu już nic do
wyjaśniania. Dodam tylko, że nie testowałem w praktyce tego Enginu.
<-DoogiE->
marcin.porebski@interia.pl
Wyszukiwarka
Podobne podstrony:
T 14Rzym 5 w 12,14 CZY WIERZYSZ EWOLUCJIustawa o umowach miedzynarodowych 14 00990425 14ART2 (10)foto (14)DGP 14 rachunkowosc i audytPlakat WEGLINIEC Odjazdy wazny od 14 04 27 do 14 06 14022 14 (2)index 14Program wykładu Fizyka II 14 15więcej podobnych podstron