Título: ayuda con wininet - solucionado
Publicado por: burbu_1 en 16 Junio 2009, 17:52 pm
Buenas, trato de hacer un cliente FTP usando el API wininet y me he quedado un poco atascado... En un form tengo 4 textbox para introducir el server, el puerto, el user y el pass, un botón para conectar al server y un listbox para listar los archivos y carpetas que contiene. Cuando conecto, me lista correctamente los archivos y carpetas que contiene el server FTP en la carpeta principal, pero cuando intento abrir una de estas carpetas y listar su contenido (haciendo doble click sobre ella) me devuelve 0, como si estuviese vacía, cuando no lo está. Dejo el código que tengo y si alguno me pudiese orientar le estaría muy agradecido. Option Explicit
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 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 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
Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0 Public Const INTERNET_SERVICE_FTP = 1 Public Const INTERNET_FLAG_PASSIVE = &H8000000
Dim hOpen As Long Dim hConnection As Long Dim server As String Dim port As Integer Dim user As String Dim pass As String Dim first_file As Long Dim next_file As Long Dim datos As WIN32_FIND_DATA
Private Sub Command1_Click()
hOpen = InternetOpen("MiFTP", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0) DoEvents
server = ftp_server.Text port = CInt(ftp_port.Text) user = ftp_user.Text pass = ftp_pass.Text
hConnection = InternetConnect(hOpen, server, port, user, pass, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0) DoEvents
first_file = FtpFindFirstFile(hConnection, vbNullString, datos, 0, 0) If first_file <> 0 Then List1.AddItem datos.cFileName next_file = InternetFindNextFile(first_file, datos) Do While next_file <> 0 List1.AddItem datos.cFileName next_file = InternetFindNextFile(first_file, datos) DoEvents Loop End If End Sub
Private Sub List1_DblClick()
Label6.Caption = Label6.Caption & "/" & List1.List(List1.ListIndex) List1.Clear If FtpSetCurrentDirectory(hConnection, Label6.Caption & "/") <> False Then
first_file = FtpFindFirstFile(hConnection, vbNullString, datos, 0, 0) If first_file <> 0 Then List1.AddItem datos.cFileName next_file = InternetFindNextFile(first_file, datos) Do While next_file <> 0 List1.AddItem datos.cFileName next_file = InternetFindNextFile(first_file, datos) DoEvents Loop End If End If End Sub
Título: Re: ayuda con wininet
Publicado por: burbu_1 en 17 Junio 2009, 08:23 am
buenas de nuevo, era una tontería pero no acababa de verlo. Tenía que cerrar los handles abiertos a findfirstfile y findnext file antes de vovlver a usarlos. Private Declare Function InternetCloseHandle Lib "wininet.dll" _ (ByVal hInet As Long) As Integer
InternetCloseHandle (first_file) InternetCloseHandle (next_file)
saludos
|