Problems with ftp access with wininet.dll

ramrodcar

New member
Joined
Nov 7, 2008
Messages
1
Programming Experience
Beginner
Hello, new user. hope this is pretty easy.
I've wrote an app and am using the functions when using wininet.dll.
I can get the files to write to the root directory, but if i try and create the directory i want, it never creates it.
I can't figure out the way to get the functions to spit out the reason for the error. any help would be greatly appreciated.
no matter what i do, it writes to the root directory.
it's not my user permissions as i can create/delete folders at will through command line.
i can also give a path in ftpputfile and it write to that directory.
I just can't navigate and create new directories.
VB.NET:
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal HINet As Integer) As Integer
    Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Integer, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Integer) As Integer
    Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Integer, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Integer, ByVal lFlags As Integer, ByVal lContext As Integer) As Integer
    Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Integer, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Integer, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean
    Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Integer, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean
    Public Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" _
    (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    Private Declare Sub FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByRef hConnect As Long, ByVal lpszCurrentDirectory As String, ByRef lpdwCurrentDirectory As Long)
    Private Declare Sub FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByRef hConnect As Long, ByVal lpszDirectory As String)
    Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    Private Declare Sub FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByRef hConnect As Long, ByVal lpszFileName As String)
    Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (ByVal lpdwError As Long, ByVal lpszBuffer As String, ByVal lpdwBufferLength As Long) As Boolean
    Private Function PutFile(ByVal file As String, ByVal remoteFile As String) As String
        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
        Const PassiveConnection As Boolean = True

        ' Open an FTP connection to the machine and get myfile.txt
        ' Connect (for now) to user1 only    
        Dim INet, INetConn As Integer
        Dim RC As Boolean
        INet = InternetOpen("DESCRIPTION HERE - you can ignore this bit", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)

                INetConn = InternetConnect(INet, "x.x.x.x", 21, "anonymous", "xxxx", 1, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)

        'get current directory

        Dim i As Boolean

        'FtpGetCurrentDirectory(INetConn, Chr(0), 1024)
        'FtpSetCurrentDirectory(INetConn, "development")
        i = FtpCreateDirectory(INetConn, "testing")
        Console.WriteLine(i)
        If Not RC = FtpSetCurrentDirectory(INetConn, "testing") Then
            Console.WriteLine("Cannot enter testing directory")
        End If

        RC = FtpPutFile(INetConn, file, remoteFile, 0, 0)
        If RC Then
            Console.WriteLine("Transfer succesfull!")
        Else
            Console.WriteLine("Transfer un-succesfull!")
        End If

        InternetCloseHandle(INetConn)
        InternetCloseHandle(INet)
    End Function
 
You can use .Net functionality to interact with FTP, for example My.Computer.Network.UploadFile method or WebClient class or FtpWebRequest class, for the latter you can specify method listed in the Net.WebRequestMethods.Ftp enumeration.
 
Back
Top