Hey all. I found this awesome source code online that covers basically all of the wininet.dll function calls for FTP commands. It doesn't, however, actually work! lol The code is below:
Source - http://www.answers.com/topic/ftp-fun...cat=technology
[CODE=vb]Const FTP_TRANSFER_TY PE_UNKNOWN = &H0
Const FTP_TRANSFER_TY PE_ASCII = &H1
Const FTP_TRANSFER_TY PE_BINARY = &H2
Const INTERNET_DEFAUL T_FTP_PORT = 21 ' default for FTP servers
Const INTERNET_SERVIC E_FTP = 1
Const INTERNET_FLAG_P ASSIVE = &H8000000 ' used for FTP connections
Const INTERNET_OPEN_T YPE_PRECONFIG = 0 ' use registry configuration
Const INTERNET_OPEN_T YPE_DIRECT = 1 ' direct to net
Const INTERNET_OPEN_T YPE_PROXY = 3 ' via named proxy
Const INTERNET_OPEN_T YPE_PRECONFIG_W ITH_NO_AUTOPROX Y = 4 ' prevent using java/script/INS
Const MAX_PATH = 260
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttribute s As Long
ftCreationTime As FILETIME
ftLastAccessTim e 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
Private Declare Function InternetCloseHa ndle Lib "wininet.dl l" (ByVal hInet As Long) As Integer
Private Declare Function InternetConnect Lib "wininet.dl l" Alias "InternetConnec tA" (ByVal hInternetSessio n 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.dl l" Alias "InternetOp enA" (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 FtpSetCurrentDi rectory Lib "wininet.dl l" Alias "FtpSetCurrentD irectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpGetCurrentDi rectory Lib "wininet.dl l" Alias "FtpGetCurrentD irectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDire ctory As String, lpdwCurrentDire ctory As Long) As Long
Private Declare Function FtpCreateDirect ory Lib "wininet.dl l" Alias "FtpCreateDirec toryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpRemoveDirect ory Lib "wininet.dl l" Alias "FtpRemoveDirec toryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dl l" Alias "FtpDeleteFileA " (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dl l" Alias "FtpRenameFileA " (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
Private Declare Function FtpGetFile Lib "wininet.dl l" Alias "FtpGetFile A" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttri butes As Long, ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dl l" Alias "FtpPutFile A" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFi le As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function InternetGetLast ResponseInfo Lib "wininet.dl l" Alias "InternetGetLas tResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLengt h As Long) As Boolean
Private Declare Function FtpFindFirstFil e Lib "wininet.dl l" Alias "FtpFindFirstFi leA" (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 InternetFindNex tFile Lib "wininet.dl l" Alias "InternetFindNe xtFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA ) As Long
Const PassiveConnecti on As Boolean = True
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net
'E-Mail: KPDTeam@allapi. net
Dim hConnection As Long, hOpen As Long, sOrgPath As String
'open an internet connection
hOpen = InternetOpen("A PI-Guide sample program", INTERNET_OPEN_T YPE_PRECONFIG, vbNullString, vbNullString, 0)
'connect to the FTP server
hConnection = InternetConnect (hOpen, "your ftp server", INTERNET_DEFAUL T_FTP_PORT, "your login", "your password", INTERNET_SERVIC E_FTP, IIf(PassiveConn ection, INTERNET_FLAG_P ASSIVE, 0), 0)
'create a buffer to store the original directory
sOrgPath = String(MAX_PATH , 0)
'get the directory
FtpGetCurrentDi rectory hConnection, sOrgPath, Len(sOrgPath)
'create a new directory 'testing'
FtpCreateDirect ory hConnection, "testing"
'set the current directory to 'root/testing'
FtpSetCurrentDi rectory hConnection, "testing"
'upload the file 'test.htm'
FtpPutFile hConnection, "C:\test.ht m", "test.htm", FTP_TRANSFER_TY PE_UNKNOWN, 0
'rename 'test.htm' to 'apiguide.htm'
FtpRenameFile hConnection, "test.htm", "apiguide.h tm"
'enumerate the file list from the current directory ('root/testing')
EnumFiles hConnection
'retrieve the file from the FTP server
FtpGetFile hConnection, "apiguide.h tm", "c:\apiguide.ht m", False, 0, FTP_TRANSFER_TY PE_UNKNOWN, 0
'delete the file from the FTP server
FtpDeleteFile hConnection, "apiguide.h tm"
'set the current directory back to the root
FtpSetCurrentDi rectory hConnection, sOrgPath
'remove the direcrtory 'testing'
FtpRemoveDirect ory hConnection, "testing"
'close the FTP connection
InternetCloseHa ndle hConnection
'close the internet connection
InternetCloseHa ndle hOpen
End Sub
Public Sub EnumFiles(hConn ection As Long)
Dim pData As WIN32_FIND_DATA , hFind As Long, lRet As Long
'set the graphics mode to persistent
Me.AutoRedraw = True
'create a buffer
pData.cFileName = String(MAX_PATH , 0)
'find the first file
hFind = FtpFindFirstFil e(hConnection, "*.*", pData, 0, 0)
'if there's no file, then exit sub
If hFind = 0 Then Exit Sub
'show the filename
Me.Print Left(pData.cFil eName, InStr(1, pData.cFileName , String(1, 0), vbBinaryCompare ) - 1)
Do
'create a buffer
pData.cFileName = String(MAX_PATH , 0)
'find the next file
lRet = InternetFindNex tFile(hFind, pData)
'if there's no next file, exit do
If lRet = 0 Then Exit Do
'show the filename
Me.Print Left(pData.cFil eName, InStr(1, pData.cFileName , String(1, 0), vbBinaryCompare ) - 1)
Loop
'close the search handle
InternetCloseHa ndle hFind
End Sub
Sub ShowError()
Dim lErr As Long, sErr As String, lenBuf As Long
'get the required buffer size
InternetGetLast ResponseInfo lErr, sErr, lenBuf
'create a buffer
sErr = String(lenBuf, 0)
'retrieve the last respons info
InternetGetLast ResponseInfo lErr, sErr, lenBuf
'show the last response info
MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
End Sub[/CODE]
As you can see it basically gives an example on how to do just about every FTP command using wininet.dll. The problem is, the commands don't work when I use them. I have, of course, edited the values so my parameters were put in for the spots such as "username", "password", "FTP_server ", etc. My question is, can anyone figure out where the bug is? It's some awesome code if it would work! Any help would be greatly appreciated. I think many can benefit from this code if we get it working.
Source - http://www.answers.com/topic/ftp-fun...cat=technology
[CODE=vb]Const FTP_TRANSFER_TY PE_UNKNOWN = &H0
Const FTP_TRANSFER_TY PE_ASCII = &H1
Const FTP_TRANSFER_TY PE_BINARY = &H2
Const INTERNET_DEFAUL T_FTP_PORT = 21 ' default for FTP servers
Const INTERNET_SERVIC E_FTP = 1
Const INTERNET_FLAG_P ASSIVE = &H8000000 ' used for FTP connections
Const INTERNET_OPEN_T YPE_PRECONFIG = 0 ' use registry configuration
Const INTERNET_OPEN_T YPE_DIRECT = 1 ' direct to net
Const INTERNET_OPEN_T YPE_PROXY = 3 ' via named proxy
Const INTERNET_OPEN_T YPE_PRECONFIG_W ITH_NO_AUTOPROX Y = 4 ' prevent using java/script/INS
Const MAX_PATH = 260
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttribute s As Long
ftCreationTime As FILETIME
ftLastAccessTim e 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
Private Declare Function InternetCloseHa ndle Lib "wininet.dl l" (ByVal hInet As Long) As Integer
Private Declare Function InternetConnect Lib "wininet.dl l" Alias "InternetConnec tA" (ByVal hInternetSessio n 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.dl l" Alias "InternetOp enA" (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 FtpSetCurrentDi rectory Lib "wininet.dl l" Alias "FtpSetCurrentD irectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpGetCurrentDi rectory Lib "wininet.dl l" Alias "FtpGetCurrentD irectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDire ctory As String, lpdwCurrentDire ctory As Long) As Long
Private Declare Function FtpCreateDirect ory Lib "wininet.dl l" Alias "FtpCreateDirec toryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpRemoveDirect ory Lib "wininet.dl l" Alias "FtpRemoveDirec toryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dl l" Alias "FtpDeleteFileA " (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dl l" Alias "FtpRenameFileA " (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
Private Declare Function FtpGetFile Lib "wininet.dl l" Alias "FtpGetFile A" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttri butes As Long, ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dl l" Alias "FtpPutFile A" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFi le As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function InternetGetLast ResponseInfo Lib "wininet.dl l" Alias "InternetGetLas tResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLengt h As Long) As Boolean
Private Declare Function FtpFindFirstFil e Lib "wininet.dl l" Alias "FtpFindFirstFi leA" (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 InternetFindNex tFile Lib "wininet.dl l" Alias "InternetFindNe xtFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA ) As Long
Const PassiveConnecti on As Boolean = True
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net
'E-Mail: KPDTeam@allapi. net
Dim hConnection As Long, hOpen As Long, sOrgPath As String
'open an internet connection
hOpen = InternetOpen("A PI-Guide sample program", INTERNET_OPEN_T YPE_PRECONFIG, vbNullString, vbNullString, 0)
'connect to the FTP server
hConnection = InternetConnect (hOpen, "your ftp server", INTERNET_DEFAUL T_FTP_PORT, "your login", "your password", INTERNET_SERVIC E_FTP, IIf(PassiveConn ection, INTERNET_FLAG_P ASSIVE, 0), 0)
'create a buffer to store the original directory
sOrgPath = String(MAX_PATH , 0)
'get the directory
FtpGetCurrentDi rectory hConnection, sOrgPath, Len(sOrgPath)
'create a new directory 'testing'
FtpCreateDirect ory hConnection, "testing"
'set the current directory to 'root/testing'
FtpSetCurrentDi rectory hConnection, "testing"
'upload the file 'test.htm'
FtpPutFile hConnection, "C:\test.ht m", "test.htm", FTP_TRANSFER_TY PE_UNKNOWN, 0
'rename 'test.htm' to 'apiguide.htm'
FtpRenameFile hConnection, "test.htm", "apiguide.h tm"
'enumerate the file list from the current directory ('root/testing')
EnumFiles hConnection
'retrieve the file from the FTP server
FtpGetFile hConnection, "apiguide.h tm", "c:\apiguide.ht m", False, 0, FTP_TRANSFER_TY PE_UNKNOWN, 0
'delete the file from the FTP server
FtpDeleteFile hConnection, "apiguide.h tm"
'set the current directory back to the root
FtpSetCurrentDi rectory hConnection, sOrgPath
'remove the direcrtory 'testing'
FtpRemoveDirect ory hConnection, "testing"
'close the FTP connection
InternetCloseHa ndle hConnection
'close the internet connection
InternetCloseHa ndle hOpen
End Sub
Public Sub EnumFiles(hConn ection As Long)
Dim pData As WIN32_FIND_DATA , hFind As Long, lRet As Long
'set the graphics mode to persistent
Me.AutoRedraw = True
'create a buffer
pData.cFileName = String(MAX_PATH , 0)
'find the first file
hFind = FtpFindFirstFil e(hConnection, "*.*", pData, 0, 0)
'if there's no file, then exit sub
If hFind = 0 Then Exit Sub
'show the filename
Me.Print Left(pData.cFil eName, InStr(1, pData.cFileName , String(1, 0), vbBinaryCompare ) - 1)
Do
'create a buffer
pData.cFileName = String(MAX_PATH , 0)
'find the next file
lRet = InternetFindNex tFile(hFind, pData)
'if there's no next file, exit do
If lRet = 0 Then Exit Do
'show the filename
Me.Print Left(pData.cFil eName, InStr(1, pData.cFileName , String(1, 0), vbBinaryCompare ) - 1)
Loop
'close the search handle
InternetCloseHa ndle hFind
End Sub
Sub ShowError()
Dim lErr As Long, sErr As String, lenBuf As Long
'get the required buffer size
InternetGetLast ResponseInfo lErr, sErr, lenBuf
'create a buffer
sErr = String(lenBuf, 0)
'retrieve the last respons info
InternetGetLast ResponseInfo lErr, sErr, lenBuf
'show the last response info
MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
End Sub[/CODE]
As you can see it basically gives an example on how to do just about every FTP command using wininet.dll. The problem is, the commands don't work when I use them. I have, of course, edited the values so my parameters were put in for the spots such as "username", "password", "FTP_server ", etc. My question is, can anyone figure out where the bug is? It's some awesome code if it would work! Any help would be greatly appreciated. I think many can benefit from this code if we get it working.