Ticket #1228: _FTP_SetStatusCallback Example.au3

File _FTP_SetStatusCallback Example.au3, 5.9 KB (added by anonymous, 15 years ago)
Line 
1#include <FTPEx.au3>
2;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
3; This var would need to be addded to FTPEx.au3. It is used to store the dll "handle" from the callback
4; so that it can be freed later during _FTP_Close().
5Global $__ghCallback_FTP = -1
6;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7
8
9$server = 'ftp.mozilla.org'
10$username = ''
11$pass = ''
12
13$Open = _FTP_Open('MyFTP Control')
14$Callback = _FTP_SetStatusCallback($Open, '_FTPStatus')
15$Conn = _FTP_Connect($Open, $server, $username, $pass, 0, 21, 1, 0, $Callback)
16$Ftpc = __FTP_Close($Open)
17
18
19;Example Callback Function
20Func _FTPStatus($hInternet, $dwContent, $dwInternetStatus, $lpvStatusInformation, $dwStatusInformationLength)
21
22    Switch $dwInternetStatus
23        Case 50; $INTERNET_STATUS_CLOSING_CONNECTION
24            ConsoleWrite('CLOSING_CONNECTION.....'& @CRLF)
25        Case 51; $INTERNET_STATUS_CONNECTION_CLOSED
26            ConsoleWrite('CONNECTION_CLOSED' & @CRLF)
27        Case 20; $INTERNET_STATUS_CONNECTING_TO_SERVER
28            ConsoleWrite('CONNECTING_TO_SERVER.....'& @CRLF)
29        Case 21; $INTERNET_STATUS_CONNECTED_TO_SERVER
30            ConsoleWrite('CONNECTED_TO_SERVER' & @CRLF)
31        Case 42; $INTERNET_STATUS_CTL_RESPONSE_RECEIVED
32            ConsoleWrite('CTL_RESPONSE_RECEIVED' & @CRLF)
33        Case 120; $INTERNET_STATUS_INTERMEDIATE_RESPONSE
34            ConsoleWrite('INTERMEDIATE_RESPONSE' & @CRLF)
35        Case 43; $INTERNET_STATUS_PREFETCH
36            ConsoleWrite('PREFETCH' & @CRLF)
37        Case 110;$INTERNET_STATUS_REDIRECT
38            ConsoleWrite('REDIRECT' & @CRLF)
39        Case 100; $INTERNET_STATUS_REQUEST_COMPLETE
40            ConsoleWrite('REQUEST_COMPLETE' & @CRLF)
41        Case 60 ;$INTERNET_STATUS_HANDLE_CREATED
42            ConsoleWrite('HANDLE_CREATED ' & @CRLF)
43        Case 70 ;$INTERNET_STATUS_HANDLE_CLOSING
44            ConsoleWrite('HANDLE_CLOSING' & @CRLF)
45        Case 30; $INTERNET_STATUS_SENDING_REQUEST
46            ConsoleWrite('SENDING_REQUEST.....' & @CRLF)
47        Case 31; $INTERNET_STATUS_REQUEST_SENT
48            Local $Size, $iBytesRead
49            $Size = DllStructCreate('dword')
50            _WinAPI_ReadProcessMemory(_WinAPI_GetCurrentProcess(), $lpvStatusInformation, DllStructGetPtr($Size), $dwStatusInformationLength, $iBytesRead)
51            ConsoleWrite('REQUEST_SENT | Size = ' & DllStructGetData($Size, 1) & ' Bytes    Bytes read = ' & $iBytesRead  & @CRLF)
52        Case 40; $INTERNET_STATUS_RECEIVING_RESPONSE
53            ConsoleWrite('RECEIVING_RESPONSE.....'& @CRLF)
54        Case 41;$INTERNET_STATUS_RESPONSE_RECEIVED
55            Local $Size, $iBytesRead
56            $Size = DllStructCreate('dword')
57            _WinAPI_ReadProcessMemory(_WinAPI_GetCurrentProcess(), $lpvStatusInformation, DllStructGetPtr($Size), $dwStatusInformationLength, $iBytesRead)
58            ConsoleWrite('REQUEST_SENT | Size = ' & DllStructGetData($Size, 1) & ' Bytes    Bytes read = ' & $iBytesRead  & @CRLF)
59        Case 200; $INTERNET_STATUS_STATE_CHANGE
60            ConsoleWrite('STATE_CHANGE' & @CRLF)
61        Case 10; $INTERNET_STATUS_RESOLVING_NAME
62            ConsoleWrite('RESOLVING_NAME.....'& @CRLF)
63        Case 11; $INTERNET_STATUS_NAME_RESOLVED
64            ConsoleWrite('NAME_RESOLVED' & @CRLF)
65        Case Else
66            ConsoleWrite($dwInternetStatus & @CRLF)
67    EndSwitch
68EndFunc   ;==>_InternetStatusCallback
69#EndRegion
70
71
72;FUNCTION# ====================================================================================================================
73; Name...........: _FTP_SetStatusCallback
74; Description ...: Registers callback function that WinINet functions can call as progress is made during an operation.
75; Syntax.........: _InternetSetStatusCallback($l_InternetSession, $sFunctionName)
76; Parameters ....: $l_FTPSession        - as returned by _FTP_Open().
77;                  $sFunctionName       - Function Name
78; Return values .: Success - Pointer to callback function
79;                  Failure - 0
80; Author ........: Beege
81; Modified.......:
82; Remarks .......: User Callback function MUST have five parameters. Ex: _Callback(parm1,parm2,parm3,parm4,parm5)
83;                                  Because callbacks are made during processing of the request, the application should spend little time in
84;                                  the callback function to avoid degrading data throughput on the network. For example, displaying a dialog
85;                                  box in a callback function can be such a lengthy operation that the server terminates the request.
86; Related .......:
87; Link ..........: @@MsdnLink@@ InternetSetStatusCallback
88; Example .......: Yes
89; ===============================================================================================================================
90Func _FTP_SetStatusCallback($l_FTPSession, $s_FunctionName)
91        If $__ghWinInet_FTP = -1 Then Return SetError(-2, 0, 0)
92
93    Local $CallBack_Register = DllCallbackRegister($s_FunctionName, "none", "ptr;ptr;dword;ptr;dword")
94        If @error Then Return SetError(-1, 0, 0)
95
96    Local $ah_CallBackFunction = DllCall($__ghWinInet_FTP, "ptr", "InternetSetStatusCallback", "ptr", $l_FTPSession, "ulong_ptr", DllCallbackGetPtr($CallBack_Register))
97        If @error Then Return SetError(-1, 0, 0)
98
99        $__ghCallback_FTP = $ah_CallBackFunction[1]
100    Return $ah_CallBackFunction[1]
101
102EndFunc   ;==>_InternetSetStatusCallback
103
104;A modified _FTP_Close() that releases Dllcallback if it has been registered. (Function taken from v3.3.1.3 beta)
105Func __FTP_Close($l_InternetSession)
106        If $__ghWinInet_FTP = -1 Then Return SetError(-2, 0, 0)
107        Local $ai_InternetCloseHandle = DllCall($__ghWinInet_FTP, 'bool', 'InternetCloseHandle', 'handle', $l_InternetSession)
108        If @error Or $ai_InternetCloseHandle[0] = 0 Then Return SetError(-1, _WinAPI_GetLastError(), 0)
109
110        If $__ghCallback_FTP <> -1 Then DllCallbackFree($__ghCallback_FTP);<<<<< This is the only part that has been changed from the original.
111
112        Return $ai_InternetCloseHandle[0]
113
114EndFunc   ;==>_FTP_Close