Solved

VB6 How to use the FTP functionality of IE6?

Posted on 2004-08-22
9
686 Views
Last Modified: 2012-06-21
G'day Experts

From within my VB6 app I want to harness the FTP functionalities of IE6 for upload and download. Code would be much appreciated, though I'd also be grateful for pointers to third-party controls.

I have one such control in use now, Catalyst's File Transfer Control, which is good, but it doesn't work with IE's functionality.

Thank you
0
Comment
Question by:Gordon_Atherley
  • 5
  • 4
9 Comments
 
LVL 8

Expert Comment

by:mladenovicz
Comment Utility
I am not sure if I understood you properly, but hope this code will be helpfull:

Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
Const FTP_TRANSFER_TYPE_ASCII = &H1
Const FTP_TRANSFER_TYPE_BINARY = &H2
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 INTERNET_OPEN_TYPE_PRECONFIG = 0                    ' use registry configuration
Const INTERNET_OPEN_TYPE_DIRECT = 1                        ' direct to net
Const INTERNET_OPEN_TYPE_PROXY = 3                         ' via named proxy
Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 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
    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
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 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
Const PassiveConnection 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("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    'connect to the FTP server
    hConnection = InternetConnect(hOpen, "your ftp server", INTERNET_DEFAULT_FTP_PORT, "your login", "your password", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
    'create a buffer to store the original directory
    sOrgPath = String(MAX_PATH, 0)
    'get the directory
    FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
    'create a new directory 'testing'
    FtpCreateDirectory hConnection, "testing"
    'set the current directory to 'root/testing'
    FtpSetCurrentDirectory hConnection, "testing"
    'upload the file 'test.htm'
    FtpPutFile hConnection, "C:\test.htm", "test.htm", FTP_TRANSFER_TYPE_UNKNOWN, 0
    'rename 'test.htm' to 'apiguide.htm'
    FtpRenameFile hConnection, "test.htm", "apiguide.htm"
    'enumerate the file list from the current directory ('root/testing')
    EnumFiles hConnection
    'retrieve the file from the FTP server
    FtpGetFile hConnection, "apiguide.htm", "c:\apiguide.htm", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0
    'delete the file from the FTP server
    FtpDeleteFile hConnection, "apiguide.htm"
    'set the current directory back to the root
    FtpSetCurrentDirectory hConnection, sOrgPath
    'remove the direcrtory 'testing'
    FtpRemoveDirectory hConnection, "testing"
    'close the FTP connection
    InternetCloseHandle hConnection
    'close the internet connection
    InternetCloseHandle hOpen
End Sub
Public Sub EnumFiles(hConnection 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 = FtpFindFirstFile(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.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
    Do
        'create a buffer
        pData.cFileName = String(MAX_PATH, 0)
        'find the next file
        lRet = InternetFindNextFile(hFind, pData)
        'if there's no next file, exit do
        If lRet = 0 Then Exit Do
        'show the filename
        Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
    Loop
    'close the search handle
    InternetCloseHandle hFind
End Sub
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
0
 

Author Comment

by:Gordon_Atherley
Comment Utility

Thank you for this code, mladenovicz. I've tested it and it works exactly as it should.

I have a two-part follow-up question, so I've increased the points -- if this is not the preferred way of presenting a follow-up question please let me know, and I'll be pleased to do it differently

I need now to mimic functionality that I use in IE6 when I go to the FTP site by ftp://IP address --

1. to delete an entire folder I've previously placed on the server when the folder has subfolders, each with multiple files
2. to publish the folder/subfolders/files set in fthe first place

From the code above, I'm thinking that I can achieve this by interatively calling the FTPDeleteFile and FTPRemoveDirectory. I'm assuming that there are no permissions problems.

Appreciated
0
 
LVL 8

Accepted Solution

by:
mladenovicz earned 500 total points
Comment Utility
Almost each API from above code returns TRUE if successful, or FALSE otherwise. To get the specific error code, call GetLastError. If the error code indicates that the FTP server denied the request to remove a directory, use InternetGetLastResponseInfo to determine why.

I don't have FTP server available at this moment, so I can not test the code.

I think that you can delete directory with this code:

    hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    'connect to the FTP server
    hConnection = InternetConnect(hOpen, "your ftp server", INTERNET_DEFAULT_FTP_PORT, "your login", "your password", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
    'create a buffer to store the original directory
    sOrgPath = String(MAX_PATH, 0)
    'get the directory
    FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
    FtpSetCurrentDirectory hConnection, "testing"
    FtpRemoveDirectory hConnection, "testing"
    'close the FTP connection
    InternetCloseHandle hConnection
    'close the internet connection
    InternetCloseHandle hOpen

I am not sure if directory has to be empty before deleting. If so, you should adapt EnumFiles method to delete files and subfolders recursively.

I am afraid that I don't understand your second question. can you provide additional info?
0
 

Author Comment

by:Gordon_Atherley
Comment Utility
Yes, the directory does have to be empty before deleting.

Here's where the functonality of IE6 becomes interesting. When I use IE6 to access the directory "Testing" I can delete it even when it's not empty and, indeed, even when it has subdirectories all stuffed with files.

Is this a matter of IE's doing the deleting iteratively as now seems necessary for me, or is there some additional functionality I can use, please?

If anyone has an answer to this, I'd be pleased to enter this as a new question and, therefore, points.

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 8

Expert Comment

by:mladenovicz
Comment Utility
I found this information on the web:

FtpRemoveDirectory      Deletes a directory and all sub items from the FTP site

Maybe it is permisions problem?

Try this:

If Not FtpRemoveDirectory (hConnection, "testing") Then Call ShowError
0
 

Author Comment

by:Gordon_Atherley
Comment Utility
Thanks.

I used thie ShowError code and got the following --

   Error 0: 550 Testing The directory is not empty.

0
 
LVL 8

Expert Comment

by:mladenovicz
Comment Utility
I am not sure why you can not delete non-empty dir. Try this basModule http://www.thevbzone.com/modFTP.bas

Maybe you should create function that will delete all files and sub dirs. Here is some code I just wrote(not tested)

Const FILE_ATTRIBUTE_DIRECTORY = &H10

Public Sub DeleteContents(hConnection As Long, DirName As String)
    Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
    Dim FileOrFolder As String
   
    'create a buffer
    pData.cFileName = String(MAX_PATH, 0)
    'find the first file
    hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
    'if there's no file, then exit sub
    If hFind = 0 Then Exit Sub
   
    'set current dir
    FtpSetCurrentDirectory hConnection, DirName
   
    FileOrFolder = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
   
    If (pData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
     ' folder call recursively
       Call DeleteContents(hConnection, FileOrFolder)
      'set old directory to be current
       FtpSetCurrentDirectory hConnection, DirName
    Else
       FtpDeleteFile hConnection, FileOrFolder
    End If
   
    Do
        'create a buffer
        pData.cFileName = String(MAX_PATH, 0)
        'find the next file
        lRet = InternetFindNextFile(hFind, pData)
        'if there's no next file, exit do
        If lRet = 0 Then Exit Do
       
        FileOrFolder = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
       
        If (pData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
         ' folder call recursively
           Call DeleteContents(hConnection, FileOrFolder)
          'set old directory to be current
           FtpSetCurrentDirectory hConnection, DirName
        Else
           FtpDeleteFile hConnection, FileOrFolder
        End If
    Loop
    'close the search handle
    InternetCloseHandle hFind
End Sub


So if you want to delete folder testing, your call will look like this:

Call DeleteContents(hConnection, "testing")
FtpSetCurrentDirectory hConnection, "testing"
FtpRemoveDirectory hConnection, "testing"

I am sure that you will have to change this code, but this is just an idea. HTH
0
 

Author Comment

by:Gordon_Atherley
Comment Utility
Many thanks, again

I'll keep working, and get back to you.
0
 

Author Comment

by:Gordon_Atherley
Comment Utility
I posted a comment earlier but it hasn't surfaced yet, so this note may be out of sequence.

~~~

I used MLADENOVICZ's  sub DeleteContents (much appreciated!), see above, for the following test sequence, see below, which funtions as it should.
I am now going to explore iterative deletion through sub directories.

~~~

If anyone would like me to post another question, please signal me with suggestions for the type of question I should ask.

~~~

Public Sub TestSequence()

    Dim hConnection As Long, hOpen As Long, sOrgPath  As String
'open an internet connection
    hOpen = InternetOpen("Tx Sample", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
   
'connect to the FTP server
    hConnection = InternetConnect(hOpen, "XX.XX3.XXX.XX", _
    INTERNET_DEFAULT_FTP_PORT, _
    "username", "password", _
    INTERNET_SERVICE_FTP, _
    IIf(PassiveConnection, _
    INTERNET_FLAG_PASSIVE, 0), 0)

'create a buffer to store the original directory
    sOrgPath = String(MAX_PATH, 0)
   
'get the directory
    FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
   
'create a new directory 'testing'
    FtpCreateDirectory hConnection, "testing"
   
'set testing as current directory
    FtpSetCurrentDirectory hConnection, "testing"
   
'upload file
    FtpPutFile hConnection, "C:\SunColRailAccidentInvest.pdf", _
        "SunColRailAccidentInvest.pdf", _
        FTP_TRANSFER_TYPE_UNKNOWN, 0
       
'delete the file or files
    Call DeleteContents(hConnection, "testing")
   
'set the current directory back to the root
    FtpSetCurrentDirectory hConnection, sOrgPath
   
'remove the directory 'testing'
    If Not FtpRemoveDirectory(hConnection, "testing") Then Call ShowError
   
'close the FTP connection
    InternetCloseHandle hConnection
   
'close the internet connection
    InternetCloseHandle hOpen


End Sub
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now