473,322 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

FTP commands from VB6

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

Expand|Select|Wrap|Line Numbers
  1. Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
  2. Const FTP_TRANSFER_TYPE_ASCII = &H1
  3. Const FTP_TRANSFER_TYPE_BINARY = &H2
  4. Const INTERNET_DEFAULT_FTP_PORT = 21               ' default for FTP servers
  5. Const INTERNET_SERVICE_FTP = 1
  6. Const INTERNET_FLAG_PASSIVE = &H8000000            ' used for FTP connections
  7. Const INTERNET_OPEN_TYPE_PRECONFIG = 0                    ' use registry configuration
  8. Const INTERNET_OPEN_TYPE_DIRECT = 1                        ' direct to net
  9. Const INTERNET_OPEN_TYPE_PROXY = 3                         ' via named proxy
  10. Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4   ' prevent using java/script/INS
  11. Const MAX_PATH = 260
  12. Private Type FILETIME
  13.     dwLowDateTime As Long
  14.     dwHighDateTime As Long
  15. End Type
  16. Private Type WIN32_FIND_DATA
  17.     dwFileAttributes As Long
  18.     ftCreationTime As FILETIME
  19.     ftLastAccessTime As FILETIME
  20.     ftLastWriteTime As FILETIME
  21.     nFileSizeHigh As Long
  22.     nFileSizeLow As Long
  23.     dwReserved0 As Long
  24.     dwReserved1 As Long
  25.     cFileName As String * MAX_PATH
  26.     cAlternate As String * 14
  27. End Type
  28. Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
  29. 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
  30. 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
  31. Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
  32. Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
  33. Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
  34. Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
  35. Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
  36. Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
  37. 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
  38. 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
  39. Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
  40. 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
  41. Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
  42. Const PassiveConnection As Boolean = True
  43. Private Sub Form_Load()
  44.     'KPD-Team 2000
  45.     'URL: http://www.allapi.net
  46.     'E-Mail: KPDTeam@allapi.net
  47.     Dim hConnection As Long, hOpen As Long, sOrgPath  As String
  48.     'open an internet connection
  49.     hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
  50.     'connect to the FTP server
  51.     hConnection = InternetConnect(hOpen, "your ftp server", INTERNET_DEFAULT_FTP_PORT, "your login", "your password", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
  52.     'create a buffer to store the original directory
  53.     sOrgPath = String(MAX_PATH, 0)
  54.     'get the directory
  55.     FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
  56.     'create a new directory 'testing'
  57.     FtpCreateDirectory hConnection, "testing"
  58.     'set the current directory to 'root/testing'
  59.     FtpSetCurrentDirectory hConnection, "testing"
  60.     'upload the file 'test.htm'
  61.     FtpPutFile hConnection, "C:\test.htm", "test.htm", FTP_TRANSFER_TYPE_UNKNOWN, 0
  62.     'rename 'test.htm' to 'apiguide.htm'
  63.     FtpRenameFile hConnection, "test.htm", "apiguide.htm"
  64.     'enumerate the file list from the current directory ('root/testing')
  65.     EnumFiles hConnection
  66.     'retrieve the file from the FTP server
  67.     FtpGetFile hConnection, "apiguide.htm", "c:\apiguide.htm", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0
  68.     'delete the file from the FTP server
  69.     FtpDeleteFile hConnection, "apiguide.htm"
  70.     'set the current directory back to the root
  71.     FtpSetCurrentDirectory hConnection, sOrgPath
  72.     'remove the direcrtory 'testing'
  73.     FtpRemoveDirectory hConnection, "testing"
  74.     'close the FTP connection
  75.     InternetCloseHandle hConnection
  76.     'close the internet connection
  77.     InternetCloseHandle hOpen
  78. End Sub
  79. Public Sub EnumFiles(hConnection As Long)
  80.     Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
  81.     'set the graphics mode to persistent
  82.     Me.AutoRedraw = True
  83.     'create a buffer
  84.     pData.cFileName = String(MAX_PATH, 0)
  85.     'find the first file
  86.     hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
  87.     'if there's no file, then exit sub
  88.     If hFind = 0 Then Exit Sub
  89.     'show the filename
  90.     Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
  91.     Do
  92.         'create a buffer
  93.         pData.cFileName = String(MAX_PATH, 0)
  94.         'find the next file
  95.         lRet = InternetFindNextFile(hFind, pData)
  96.         'if there's no next file, exit do
  97.         If lRet = 0 Then Exit Do
  98.         'show the filename
  99.         Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
  100.     Loop
  101.     'close the search handle
  102.     InternetCloseHandle hFind
  103. End Sub
  104. Sub ShowError()
  105.     Dim lErr As Long, sErr As String, lenBuf As Long
  106.     'get the required buffer size
  107.     InternetGetLastResponseInfo lErr, sErr, lenBuf
  108.     'create a buffer
  109.     sErr = String(lenBuf, 0)
  110.     'retrieve the last respons info
  111.     InternetGetLastResponseInfo lErr, sErr, lenBuf
  112.     'show the last response info
  113.     MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
  114. End Sub
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.
Mar 1 '08 #1
0 2661

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Mark Wilson CPU | last post by:
A colleague has written a prototype program in PHP, using a MySQL database. It's a relatively simple app, with a restricted set of mysql commands used (see below). The MySQL DB is being replaced...
12
by: Moosebumps | last post by:
So, after reading some messages about os.system, and looking at the popen stuff and trying it a bit, I still have not found a way to keep a command window open for several commands (on Windows...
4
by: Avi Kak | last post by:
Is there a Python function in any of the standard-distribution modules that does what the backticks do in Perl? I want to run an external command and I'd like its output to be captured directly...
2
by: timdoyle05 | last post by:
Hi, I have a question relating to how Unix commands can be issued from Python programs. Im am currently writing a large test script in python and I need this script to call three other separate...
2
by: Raquel | last post by:
Why can we not give db2 System commands (commands starting with 'db2' like db2ilist, db2look, db2move etc.) through the Command Center? It is so convenient to retrieve and change commands in the...
8
by: Joanna Carter [TeamB] | last post by:
Hi Folks I am just trying to get my head around whether I can use a single SQLConnection for the life of the application or whether I should create it only when needed. I want to create...
13
by: =?Utf-8?B?VmVybm9uIFBlcHBlcnM=?= | last post by:
I am using VS2005. I created a Windows Appication project. Inside the Server Explorer, I created a new SQLExpress database, and then created a new table. I added the rows, making my first row an...
6
by: Peted | last post by:
Hi wondering what is the best way to do this Need a user to click a button, that sends 3 or 4 string based commands via a TCP/ip socket link I can connect to the ip device no problems, am...
15
by: tmp123 | last post by:
Hello, Thanks for your time. We have very big files with python commands (more or less, 500000 commands each file). It is possible to execute them command by command, like if the commands...
1
by: Riccardo Maria Bianchi | last post by:
Hello! :) I'm trying to run shell commands both with os.system() and subprocess.Popen() class. But I can't run aliases or function defined in my .bashrc file, like in the login interactive...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.