FTP Current Dir Call Crashes Excel

hamiltpj

New Member
Joined
Oct 29, 2017
Messages
13
Hi there,

I am building an application that uses wininet ftp calls, all of which are working fine except from my request for the current directory. The following is my (cut down) code...
Code:
    Option Explicit
    
    Private Const MAX_PATH = 260
    Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
    Private Const INTERNET_DEFAULT_FTP_PORT = 21
    Private Const INTERNET_SERVICE_FTP = 1
    
    Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal lpszAgent As String, ByVal dwAccessType As Long, ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal dwFlags As Long) As Long
    Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Long, 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 InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Long
    Private Declare Function FtpGetCurrentDirectory Lib "WinInet" Alias "FtpGetCurrentDirectoryA" (ByVal hFtp As Long, lpszDirectory As String, ByVal BuffLength As Long) As Long
    
    Private hConnect As Long
    Private hFtp As Long
    
    Public Sub Connect(Server As String, User As String, pwd As String)
        hConnect = InternetOpen("Microsoft Excel", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
        hFtp = InternetConnect(hConnect, Server, INTERNET_DEFAULT_FTP_PORT, User, pwd, INTERNET_SERVICE_FTP, 0, 0)
    End Sub
    
    Public Sub Disconnect()
        If hConnect <> 0 Then
            InternetCloseHandle hConnect
        End If
    End Sub
    
    Public Function CurrentDir() As String
        Dim RetVal As String * MAX_PATH
        FtpGetCurrentDirectory hFtp, RetVal, Len(RetVal)
        CurrentDir = Trim(RetVal)
    End Function
    

    Public Function CurrentDir() As String
        Dim RetVal As String * MAX_PATH
        FtpGetCurrentDirectory hFtp, RetVal, Len(RetVal)
        CurrentDir = Trim(RetVal)
    End Function

Please help!

Pete
 
Last edited by a moderator:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I have the same problem or question. I googled and find this page pinvoke.net: FtpGetCurrentDirectory (wininet)
It seems to tell us that we have to build up something which can receive string text, and code in the page is:
VBA Code:
Dim s As New System.Text.StringBuilder
But, when I put it into my VBA project, it gives me ' Compil error: User-defined type not defined'

I also tried something like below:
VBA Code:
  Dim sCrnD As Object
  Set sCrnD = CreateObject("System.Text.StringBuilder"): sCrnD.Capacity = MAX_PATH
  Dim hCrnD As Long: hCrnD = FtpGetCurrentDirectoryA(hConn, sCrnD, MAX_PATH)
but still error code happened in FtpGetCurrentDirectoryA line
Run-time error '5':
Invalid procedure call or argument

So, I eventually give it up, and use:
VBA Code:
'Check Current Directory (Send 'PWD' to server)
  Dim hCrnD As Long: hCrnD = FtpGetCurrentDirectoryA(hConn, "", MAX_PATH)
It can send PWD commands to the FTP server which satisfies me well.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top