elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado:


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  [Vb6] WinInet Asynchronous FTP
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [Vb6] WinInet Asynchronous FTP  (Leído 5,971 veces)
scapula

Desconectado Desconectado

Mensajes: 10


Ver Perfil
[Vb6] WinInet Asynchronous FTP
« en: 3 Noviembre 2011, 03:57 am »



Asynchronous Wininet Api (Tested on XP & Seven)
Send Big File on FTP without Application Freez



Exemple To Use:
Código
  1. '===========================================
  2. '           WinInet Async CallBack
  3. '===========================================
  4. '       Author: (Erik L)
  5. '       Init.   (EGL)
  6. '       Email:  egl1044@gmail.com
  7. '
  8. '===========================================
  9.  
  10. Dim IWnet As New WinInetAsync
  11.  
  12.  
  13. Private Sub Form_Load()
  14. IWnet.Server = ""   ' FTP
  15. IWnet.UserName = "" ' USER
  16. IWnet.Password = "" ' PASS
  17.  
  18. Call IWnet.Connect(True) 'True = ACTIVE ASYNCHRONOUS
  19. Call IWnet.FtpDirectoryCreate(Environ$("Computername"))
  20. Call IWnet.FtpSetDirectory(Environ$("Computername"))
  21. Call IWnet.FtpUpload("Calc.exe", Environ$("WINDIR") & "\SYSTEM32\calc.exe", FTP_TRANSFER_TYPE_BINARY)
  22. Call IWnet.Disconnect
  23. End Sub







Module: CallBackProc.BAS
Código
  1. Option Explicit
  2.  
  3. Private Declare Sub BalanceMemoryAny Lib "kernel32" Alias "RtlMoveMemory" ( _
  4.    lpDest As Any, _
  5.    lpSource As Any, _
  6.    ByVal nBytes As Long)
  7.  
  8. Public Enum InternetStatusVals
  9.  ResolvingName = 10
  10.  NameResolved = 11
  11.  ConnectingToServer = 20
  12.  ConnectedToServer = 21
  13.  SendingRequest = 30
  14.  RequestSent = 31
  15.  ReceivingResponse = 40
  16.  ResponseReceived = 41
  17.  PreFetch = 43
  18.  ClosingConnection = 50
  19.  ConnectionClosed = 51
  20.  HandleCreated = 60
  21.  HandleClosing = 70
  22.  DetectingProxy = 80
  23.  RequestComplete = 100
  24.  Redirecting = 110
  25.  IntermediateResponse = 120
  26.  UserInputRequired = 140
  27.  StateChange = 200
  28. End Enum
  29.  
  30. Private mIAR As INERNET_ASYNC_RESULT
  31.  
  32. Public Sub INTERNET_STATUS_CALLBACK( _
  33.    ByVal hInternet As Long, _
  34.    ByVal dwContext As Long, _
  35.    ByVal dwInternetStatus As InternetStatusVals, _
  36.    ByVal lpvStatusInformation As Long, _
  37.    ByVal dwStatusInformationLength As Long)
  38.  
  39.  Dim dwRead As Long
  40.  Dim cBuffer As String
  41.  
  42.  cBuffer = Space$(dwStatusInformationLength)
  43.  
  44.  Select Case dwInternetStatus
  45.    Case ResolvingName
  46.      BalanceMemoryAny ByVal cBuffer, ByVal lpvStatusInformation, dwStatusInformationLength
  47.      Debug.Print RipNulls(cBuffer)
  48.    Case NameResolved
  49.      BalanceMemoryAny ByVal cBuffer, ByVal lpvStatusInformation, dwStatusInformationLength
  50.      Debug.Print RipNulls(cBuffer)
  51.    Case ConnectingToServer
  52.      Debug.Print "Connecting"
  53.    Case ConnectedToServer
  54.      Debug.Print "Connected"
  55.    Case SendingRequest
  56.    Case RequestSent
  57.      BalanceMemoryAny dwRead, ByVal lpvStatusInformation, dwStatusInformationLength
  58.    Case ReceivingResponse
  59.    Case ResponseReceived
  60.    Case ClosingConnection
  61.      Debug.Print "Closing Connection"
  62.    Case ConnectionClosed
  63.      Debug.Print "Closed Connection"
  64.    Case HandleCreated
  65.      BalanceMemoryAny mIAR.dwAddress, ByVal lpvStatusInformation, dwStatusInformationLength
  66.    Case HandleClosing
  67.    Case UserInputRequired
  68.      Debug.Print "User Input"
  69.    Case RequestComplete
  70.      Debug.Print "Request Complete"
  71.    Case Else
  72.  End Select
  73.  DoEvents
  74.  
  75. End Sub
  76.  
  77. Public Function ReturnAddress() As Long
  78.  ReturnAddress = mIAR.dwAddress
  79. End Function
  80.  







Module: mInet.BAS
Código
  1. Option Explicit
  2.  
  3. Public Type INERNET_ASYNC_RESULT
  4.  dwResult        As Long
  5.  dwError         As Long
  6.  dwAddress       As Long              'Address Handle created by callback
  7. End Type
  8.  
  9. Public Declare Function InternetOpenA Lib "WININET.DLL" ( _
  10.    ByVal lpszAgent As String, _
  11.    ByVal dwAccessType As Long, _
  12.    ByVal lpszProxyName As String, _
  13.    ByVal lpszProxyBypass As String, _
  14.    ByVal dwFlags As Long) As Long
  15.  
  16. Public Declare Function InternetSetStatusCallback Lib "WININET.DLL" ( _
  17.    ByVal hInternet As Long, _
  18.    ByVal lpfnInternetCallback As Long) As Long
  19.  
  20. Public Declare Function InternetConnectA Lib "WININET.DLL" ( _
  21.    ByVal hConnect As Long, _
  22.    ByVal lpszServerName As String, _
  23.    ByVal nServerPort As Long, _
  24.    ByVal lpszUsername As String, _
  25.    ByVal lpszPassword As String, _
  26.    ByVal dwService As Long, _
  27.    ByVal dwFlags As Long, _
  28.    ByVal dwContext As Long) As Long
  29.  
  30. Public Declare Function InternetReadFile Lib "WININET.DLL" ( _
  31.    ByVal hFile As Long, _
  32.    ByVal sBuffer As Long, _
  33.    ByVal lNumBytesToRead As Long, _
  34.    lNumberOfBytesRead As Long) As Long
  35.  
  36. Public Declare Function InternetOpenUrl Lib "WININET.DLL" Alias "InternetOpenUrlA" ( _
  37.    ByVal hInternet As Long, _
  38.    ByVal lpszUrl As String, _
  39.    lpszHeaders As Any, _
  40.    ByVal dwHeadersLength As Long, _
  41.    ByVal dwFlags As Long, _
  42.    ByVal dwContext As Long) As Long
  43.  
  44. Public Declare Function InternetQueryDataAvailable Lib "WININET.DLL" ( _
  45.    ByVal hFile As Long, _
  46.    lpdwNumberOfBytesAvailable As Long, _
  47.    ByVal dwFlags As Long, _
  48.    ByVal dwContext As Long) As Long
  49.  
  50. Public Declare Function InternetCloseHandle Lib "WININET.DLL" ( _
  51.    ByVal hInternet As Long) As Long
  52.  
  53. Public Declare Function SleepEx Lib "kernel32" ( _
  54.    ByVal dwMilliseconds As Long, _
  55.    ByVal bAlertable As Long) As Long
  56.  
  57. Public Declare Function WriteFile Lib "kernel32" ( _
  58.    ByVal hFile As Long, lpBuffer As Any, _
  59.    ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, _
  60.    ByVal lpOverlapped As Any) As Long
  61.  
  62. Public mOpen        As Long            '// InternetOpen         Handle
  63. Public mConn        As Long            '// InternetConnect      Handle
  64.  
  65. Public Function RipNulls(ByVal AnyBuffer As String) As String
  66.  RipNulls = Left$(AnyBuffer, InStr(AnyBuffer, vbNullChar) - 1)
  67. End Function
  68.  







Class: WinInetAsync.CLS
Código
  1. Option Explicit
  2. Option Base 0
  3.  
  4. 'FTP Const
  5. Private Const FTP_RELOADS& = &H80000000
  6. Private Const FTP_PASSIVE& = &H8000000
  7. Private Const FTP_SERVICE& = 1
  8. Private Const FTP_PORTNUM& = 21
  9. Private Const FTP_DIRECT& = 1
  10. Private Const FTP_READ& = &H80000000
  11. Private Const FTP_ASYNC& = &H1
  12. 'HTTP Const
  13. Private Const HTTP_ASYNC& = &H1
  14. Private Const HTTP_NO_CACHE_WRITE& = &H4000000
  15. Private Const HTTP_RESYNCHRONIZE& = &H800
  16. Private Const HTTP_DIRECT = 1
  17. 'CreateFile,WriteFile Const
  18. Private Const FILE_SHARE_WRITE = &H2
  19. Private Const FILE_GENERIC_WRITE = &H40000000
  20. Private Const FILE_CREATE_ALWAYS = 2
  21. 'Other Const
  22. Private Const DW_CONTEXT& = 2
  23. Private Const INVALID_CALLBACK& = -1
  24. Private Const MAX_PATH As String = 260
  25.  
  26.  
  27. Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" ( _
  28.  ByVal lpFileName As String, _
  29.  ByVal dwDesiredAccess As Long, _
  30.  ByVal dwShareMode As Long, _
  31.  ByVal lpSecurityAttributes As Long, _
  32.  ByVal dwCreationDisposition As Long, _
  33.  ByVal dwFlagsAndAttributes As Long, _
  34.  ByVal hTemplateFile As Long) As Long
  35.  
  36. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  37.  
  38. 'FtpGetFile:
  39. '   Retrieves a file from the FTP serverand stores
  40. '   it under the specified file name, creating a
  41. '   new local file in the process.
  42. Private Declare Function FtpGetFile Lib "WININET.DLL" Alias "FtpGetFileA" ( _
  43.    ByVal hConnect As Long, _
  44.    ByVal lpszRemoteFile As String, _
  45.    ByVal lpszNewFile As String, _
  46.    ByVal fFailIfExists As Long, _
  47.    ByVal dwFlagsAndAttributes As Long, _
  48.    ByVal dwFlags As Long, _
  49.    ByVal dwContext As Long) As Long
  50. 'FtpPutFile:
  51. '   Stores a file on the FTP server
  52. Private Declare Function FtpPutFileA Lib "WININET.DLL" ( _
  53.    ByVal hConnect As Long, _
  54.    ByVal lpszLocalFile As String, _
  55.    ByVal lpszNewRemoteFile As String, _
  56.    ByVal dwFlags As Long, _
  57.    ByVal dwContext As Long) As Long
  58. 'FtpSetCurrentDirectory:
  59. '   Changes to a different working directory on the FTP server.
  60. Private Declare Function FtpSetCurrentDirectoryA Lib "WININET.DLL" ( _
  61.    ByVal hConnect As Long, _
  62.    ByVal lpszDirectory As String) As Long
  63. 'FtpGetCurrentDirectory:
  64. '   Retrieves the current directory for the specified FTP session.
  65. Private Declare Function FtpGetCurrentDirectoryA Lib "WININET.DLL" ( _
  66.    ByVal hConnect As Long, _
  67.    ByVal lpszCurrentDirectory As String, _
  68.    ByRef lpdwCurrentDirectory As Long) As Long
  69. 'FtpOpenFile:
  70. '   Initiates access to a remote file on an FTP server for reading or writing.
  71. Private Declare Function FtpOpenFile Lib "WININET.DLL" Alias "FtpOpenFileA" ( _
  72.    ByVal hConnect As Long, _
  73.    ByVal lpszFileName As String, _
  74.    ByVal dwAccess As Long, _
  75.    ByVal dwFlags As Long, _
  76.    ByVal dwContext As Long) As Long
  77. 'FtpGetFileSize:
  78. ' Returns FileSize on FTP server
  79. Private Declare Function FtpGetFileSize Lib "WININET.DLL" ( _
  80.    ByVal hFile As Long, _
  81.    ByRef lpdwFileSizeHigh As Long) As Long
  82. 'FtpDeleteFile:
  83. '   Deletes a file stored on the FTP server.
  84. Private Declare Function FtpDeleteFileA Lib "WININET.DLL" ( _
  85.    ByVal hConnect As Long, _
  86.    ByVal lpszFileName As String) As Long
  87. 'FtpCreateDirectory:
  88. '   Creates a new directory on the FTP server.
  89. Private Declare Function FtpCreateDirectoryA Lib "WININET.DLL" ( _
  90.    ByVal hConnect As Long, _
  91.    ByVal lpszDirectory As String) As Long
  92. 'FtpRemoveDirectory
  93. '   Removes the specified directory on the FTP server.
  94. Private Declare Function FtpRemoveDirectory Lib "WININET.DLL" Alias "FtpRemoveDirectoryA" ( _
  95.    ByVal hFtpSession As Long, _
  96.    ByVal lpszDirectory As String) As Long
  97. 'FtpRenameFile
  98. '   Renames a file stored on the FTP server.
  99. Private Declare Function FtpRenameFileA Lib "WININET.DLL" ( _
  100.    ByVal hConnect As Long, _
  101.    ByVal lpszExisting As String, _
  102.    ByVal lpNewFileName As String) As Long
  103.  
  104. Public Enum TransferTypes
  105.  FTP_TRANSFER_TYPE_UNKNOWN = &H0
  106.  FTP_TRANSFER_TYPE_ASCII = &H1
  107.  FTP_TRANSFER_TYPE_BINARY = &H2
  108. End Enum
  109.  
  110. Public Server$                         '// Ftp Servername
  111. Public UserName$                       '// Ftp Username
  112. Public Password$                       '// Ftp Password
  113.  
  114. Public AmIRegistered$
  115.  
  116. Private m_AsyncResult As INERNET_ASYNC_RESULT
  117.  
  118. Public Sub Connect(Optional AsyncMode As Boolean = True)
  119.  
  120.  Dim Result As Long
  121.  
  122.  mOpen = InternetOpenA( _
  123.      App.ProductName, _
  124.      FTP_DIRECT, _
  125.      vbNullString, _
  126.      vbNullString, _
  127.      FTP_ASYNC&)
  128.  
  129.  If AsyncMode = True Then
  130.    Result = InternetSetStatusCallback(mOpen, AddressOf INTERNET_STATUS_CALLBACK)
  131.  End If
  132.  
  133.  mConn = InternetConnectA( _
  134.      mOpen, _
  135.      Server, _
  136.      FTP_PORTNUM&, _
  137.      UserName, _
  138.      Password, _
  139.      FTP_SERVICE&, _
  140.      FTP_PASSIVE&, DW_CONTEXT&)
  141.  
  142. End Sub
  143.  
  144. Public Function Disconnect() As Boolean
  145.  
  146.  'Clean up
  147.  Call InternetCloseHandle(mConn)
  148.  Call InternetCloseHandle(mOpen)
  149.  Call InternetCloseHandle(ReturnAddress)
  150.  
  151. End Function
  152.  
  153. Public Function FtpDownload(ByVal RemoteFile As String, _
  154.    ByVal LocalFile As String, _
  155.    ByVal TransferMode As TransferTypes) As Boolean
  156.  
  157.  Dim Success As Boolean
  158.  Success = FtpGetFile(mConn, _
  159.      RemoteFile, _
  160.      LocalFile, _
  161.      False, ByVal 0&, _
  162.      TransferMode, DW_CONTEXT&)
  163.  FtpDownload = Success
  164. End Function
  165.  
  166. Public Function FtpUpload(ByVal RemoteFile As String, _
  167.    ByVal LocalFile As String, _
  168.    ByVal TransferMode As TransferTypes) As Boolean
  169.  
  170.  Dim Success As Boolean
  171.  Success = FtpPutFileA(mConn, _
  172.      LocalFile, _
  173.      RemoteFile, _
  174.      TransferMode, DW_CONTEXT&)
  175.  FtpUpload = Success
  176. End Function
  177.  
  178. Public Function FtpGetDirectory() As String
  179.  Dim DirBuff As String
  180.  Dim strTemp As String
  181.  Dim Success As Boolean
  182.  
  183.  DirBuff = String$(MAX_PATH, vbNullChar)
  184.  Success = FtpGetCurrentDirectoryA(mConn, DirBuff, Len(DirBuff))
  185.  FtpGetDirectory = RipNulls(DirBuff)
  186. End Function
  187.  
  188. Friend Function FtpSetDirectory(ByVal SSetDir As String) As Boolean
  189.  Dim Success As Boolean
  190.  Success = FtpSetCurrentDirectoryA(mConn, SSetDir)
  191.  FtpSetDirectory = Success
  192. End Function
  193.  
  194. Friend Function FtpFileDelete(ByVal sFileName As String) As Boolean
  195.  Dim Success As Boolean
  196.  Success = FtpDeleteFileA(mConn, sFileName)
  197.  FtpFileDelete = Success
  198. End Function
  199.  
  200. Friend Function FtpFileRename(ByVal ExistingFileName As String, _
  201.    ByVal RenameFile As String) As Boolean
  202.  Dim Success As Boolean
  203.  Success = FtpRenameFileA(mConn, ExistingFileName, RenameFile)
  204.  FtpFileRename = Success
  205. End Function
  206.  
  207. Friend Function FtpDirectoryCreate(ByVal CreateNewDirName As String) As Boolean
  208.  Dim Success As Boolean
  209.  Success = FtpCreateDirectoryA(mConn, CreateNewDirName)
  210.  FtpDirectoryCreate = Success
  211. End Function
  212.  
  213. Friend Function FtpDirectoryRemove(ByVal RemoveDirectoryName As String) As Boolean
  214.  Dim Success As Boolean
  215.  Success = FtpRemoveDirectory(mConn, RemoveDirectoryName)
  216.  FtpDirectoryRemove = Success
  217. End Function
  218.  
  219. Public Function Http_DownloadFile(ByVal FileName As String, _
  220.    ByVal WebURL As String, _
  221.    ByVal TransferMode As TransferTypes, _
  222.    Optional ChunkSize As Long = 8192)
  223.  
  224.  Dim hLocalFile As Long
  225.  Dim Buffer() As Byte
  226.  Dim bytesRead As Long
  227.  Dim bytesWritten As Long
  228.  Dim bytesTransferred As Long
  229.  Dim boolCancel As Boolean
  230.  Dim lOpen As Long
  231.  Dim lHandle As Long
  232.  Dim Result As Long
  233.  
  234.  lOpen = InternetOpenA(App.ProductName, _
  235.      HTTP_DIRECT, _
  236.      vbNullString, _
  237.      vbNullString, _
  238.      HTTP_ASYNC&)
  239.  
  240.  Result = InternetSetStatusCallback(lOpen, AddressOf INTERNET_STATUS_CALLBACK)
  241.  
  242.  lHandle = InternetOpenUrl(lOpen, _
  243.      WebURL, _
  244.      vbNullString, _
  245.      ByVal 0&, _
  246.      TransferMode, _
  247.      HTTP_NO_CACHE_WRITE& Or HTTP_RESYNCHRONIZE&)
  248.  
  249.  hLocalFile = CreateFile(FileName, _
  250.      FILE_GENERIC_WRITE, _
  251.      FILE_SHARE_WRITE, _
  252.      ByVal 0&, FILE_CREATE_ALWAYS, 0, 0)
  253.  
  254.  If hLocalFile <> 0 Then
  255.  
  256.    ReDim Buffer(ChunkSize)
  257.  
  258.    Do
  259.  
  260.      If InternetReadFile(lHandle, _
  261.          ByVal VarPtr(Buffer(0)), _
  262.          ChunkSize, _
  263.          bytesRead) Then
  264.  
  265.        If WriteFile(hLocalFile, _
  266.            ByVal VarPtr(Buffer(0)), _
  267.            bytesRead, _
  268.            bytesWritten, _
  269.            ByVal 0&) Then
  270.  
  271.          bytesTransferred = bytesTransferred + bytesWritten
  272.  
  273.        End If
  274.      Else
  275.        boolCancel = True
  276.      End If
  277.      DoEvents
  278.    Loop While bytesRead = ChunkSize And Not boolCancel
  279.  
  280.  End If
  281.  
  282.  Call CloseHandle(hLocalFile)
  283.  Call InternetCloseHandle(lHandle)
  284.  Call InternetCloseHandle(lOpen)
  285.  
  286. End Function
  287.  





En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Declaraciones de WinInet
Programación Visual Basic
Slasher-K 2 4,876 Último mensaje 3 Septiembre 2005, 21:25 pm
por NYlOn
Conectarme a FTP mediante wininet.dll en eVB
Programación Visual Basic
ENVIROMENT 1 3,553 Último mensaje 30 Septiembre 2005, 21:36 pm
por casaviella
Problema con Wininet
Programación C/C++
Riki_89D 5 3,330 Último mensaje 11 Agosto 2011, 10:10 am
por Riki_89D
Wininet PHP Post
Programación C/C++
Høl¥ 1 1,784 Último mensaje 21 Junio 2013, 15:58 pm
por Høl¥
C++ WinInet
Programación C/C++
XKC 1 1,880 Último mensaje 7 Agosto 2017, 15:01 pm
por fary
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines