Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FTP with wininet.dll problems

Status
Not open for further replies.

jebenson

Technical User
Feb 4, 2002
2,956
US
Hello all,

I am having some trouble using wininet.dll fot FTP in VB.NET. Here is my code:

Code:
Module Module1

    Private Structure FILETIME
        Dim dwLowDateTime As Long
        Dim dwHighDateTime As Long
    End Structure

    Private Structure WIN32_FIND_DATA
        Dim dwFileAttributes As Long
        Dim ftCreationTime As FILETIME
        Dim ftLastAccessTime As FILETIME
        Dim ftLastWriteTime As FILETIME
        Dim nFileSizeHigh As Long
        Dim nFileSizeLow As Long
        Dim dwReserved0 As Long
        Dim dwReserved1 As Long
        Dim cFileName As String
        Dim cAlternate As String
    End Structure

    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
    Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, ByVal 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, ByVal lpFindFileData As WIN32_FIND_DATA) 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, ByVal lpdwCurrentDirectory As Long) As Long

    Sub Main()
        Dim INet As Long
        Dim INetConn As Long
        Dim RC As Boolean
        Dim FileData As WIN32_FIND_DATA
        Dim FileList As String
        Dim MAX_PATH As Long = 0

        INet = InternetOpen("FTP_Backup", 1, vbNullString, vbNullString, 0)
        INetConn = InternetConnect(INet, "my.ftp.server.name", 0, "MyUsername", "MyPassword", 1, 0, 0)
        RC = FtpGetFile(INetConn, "/notes.ini", "D:\Temp\VB .NET Temp\Notes Data\notes.ini", False, 0, 0, 0)

        If RC Then Console.WriteLine("Transferred Notes.ini")

        RC = FtpSetCurrentDirectory(INetConn, "/Data/mail")
        MsgBox(RC)

        RC = FtpGetCurrentDirectory(INetConn, FileList, MAX_PATH)


        MsgBox(MAX_PATH)
        MsgBox(FileList)

        FileData.cFileName = New String("", 0, MAX_PATH)

        RC = FtpFindFirstFile(INetConn, "*.*", FileData, 0, 0)

        MsgBox(RC)

        FileList = FileData.cFileName & ","

        'Do While RC = True
        '    RC = InternetFindNextFile(INetConn, FileData)
        '    FileList = FileList & FileData.cFileName & ","
        '    Console.WriteLine(FileList)
        'Loop

        FileList = Mid(FileList, 1, Len(FileList) - 1)

        MsgBox(FileList)

        InternetCloseHandle(INetConn)
        InternetCloseHandle(INet)
    End Sub


End Module

The problem is that FtpSetCurrentDirectory doesn't work (returns False always) and I know that the remote directory exists (I can navigate to it using WS_FTP). Also, FtpGetCurrentDirectory returns True, but the FileList string is empty, the current path is not returned. The FtpGetFile function works just fine, strangely. The FtpFindFirstFile function also returns true, but when I check the values of the FindData structure, it is empty.

Any ideas? This is driving me nuts!

Thanks,
JEB

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Try replacing "As Long" with "As Int32".

__________________________________________
Try forum1391 for lively discussions
 
Hi, and thanks for the reply.

I changed all instances of "Long" to "Int32", and now the FtpSetCurrentDirectory call returns True. However, it now dies on this line:

RC = FtpGetCurrentDirectory(INetConn, FileList, MAX_PATH)

with the following error message:

An unhandled exception of type 'System.NullReferenceException' occurred in FTP_Backup.exe

Additional information: Object reference not set to an instance of an object.


Did you mean for me to change all of the Long references to Int32, including those in the function declarations?


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
>Did you mean for me to change all of the Long references to Int32, including those in the function declarations?

I think this worked for me before (I'll have to double check that code later on today):
Code:
Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Int32, ByVal lpszCurrentDirectory As String, ByVal lpdwCurrentDirectory [b]As IntPtr[/b]) As Int32
Basically, change all pointer datatypes to IntPtr in the declarations.


Go here to see what parameters are pointers:
Most of the VB examples that are circulating will not work in VB.NET. This is due to a mix-up in the data types, I think. The best way to declare those functions is to look at the types used by C#.


__________________________________________
Try forum1391 for lively discussions
 
Dimandja,

First off let me say again thanks for taking the time to help with this.

Well, I changed the parameter to IntPtr, and changed MAX_PATH to IntPtr as well, and the function call to FtpGetCurrentDirectory no longer throws an error. However, the call returns False.

Any other ideas?

Thanks,
JEB

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Yes. I couldn't find my old code. But, I'll try your code over the weekend.

As I mentioned earlier, Microsoft forgot to offer working VB.NET examples with their APIs. I have had to reverse engineer quite often -- by taking the lead from C#. Quite unfortunate.

__________________________________________
Try forum1391 for lively discussions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top