FTP with AutoIT – API Programming
It is really easy to call Win32 API functions through AutoIT. All that one needs to know is how to use the “DllCall” function in AutoIT and the functions which are available in the particular dll that one intends to utilize. In order to create an FTP client, it is really easy if one uses AutoIT. We can make use of the “Wininet.dll” for the sake of creating FTP client software. The functions that are useful for us will be as follows:
•InternetOpen
•InternetConnect
•FtpGetFile
•FtpPutFile
•FtpFindFirstFile
•InternetFindNextFile
•InternetCloseHandle
There are many more functions which are available with wininet.dll, but in order to make the Ftp client software, the above mentioned files are more than enough. Now, let me describe the step by step process which needs to be followed in order to do an Ftp get.
1.Open an Internet Connection – This is to initialize the wininet API. One can use any connection name for achieving this
1.$InternetOpen = DllCall("wininet.dll", "long", "InternetOpen", "str", “My FTP Control”, "long", 1, "str", “”, "str", “”, "long", 0);We can keep the proxy name and proxy bypass as blank.
2.Connect to the server with the username and password – One need to have a username and password for the same, as well as the handle returned from the previous step
2.$InternetConnect = DllCall("wininet.dll", "long", "InternetConnect", "long", $InternetOpen, "str", $ServerName, "int", 0, "str", $Username, "str", $Password, "long", 1, "long", 0, "long", 0); Servername, Username, Password need to be provided.
3. Start the file transfer with FtpGetFile function – Filename and handle needs to be provided as the inputs
3.$FTPget = DllCall("wininet.dll", "int", "FtpGetFile", "long", $InternetConnect, "str", $RemoteFile, "str", $SaveLocalFile, "int", 0, "long", 0, "long", 0, "long", 0);This would save the file in the local computer with the name as provided in place of variable $SaveLocalFile.
4. Close both the handles
4.$CloseCC = DllCall("wininet.dll", "int", "InternetCloseHandle", "int", $InternetOpen)
4.$CloseCC = DllCall("wininet.dll", "int", "InternetCloseHandle", "int", $InternetConnect)
That’s all the one needs. The file would be saved in the local machine with the desired name. For more details on the functions in wininet.dll, one can visit http://msdn.microsoft.com/en-us/library/aa385473%28VS.85%29.aspx.
I have created Ftp client software with the same function, and you can download it freely by clicking here.
If you want to access the code, kindly post a request for it through the comments section. I will share the code with anyone who requires.
Posted by Kishore Krishnan at 9:30 PM
Labels: API Programming, FTP Client, Wininet.dll
wininet.dll
FtpCommand function
The FtpCommand function sends commands directly to an FTP server.
Syntax
C++
BOOL FtpCommand(
_In_ HINTERNET hConnect,
_In_ BOOL fExpectResponse,
_In_ DWORD dwFlags,
_In_ LPCTSTR lpszCommand,
_In_ DWORD_PTR dwContext,
_Out_ HINTERNET *phFtpCommand
);
Parameters
hConnect [in]
A handle returned from a call to InternetConnect.
fExpectResponse [in]
A Boolean value that indicates whether the application expects a data connection to be established by the FTP server. This must be set to TRUE if a data connection is expected, or FALSE otherwise.
dwFlags [in]
A parameter that can be set to one of the following values.
Value
|
Meaning
|
FTP_TRANSFER_TYPE_ASCII
|
Transfers the file using the FTP ASCII (Type A) transfer method. Control and formatting data is converted to local equivalents.
|
FTP_TRANSFER_TYPE_BINARY
|
Transfers the file using the FTP Image (Type I) transfer method. The file is transferred exactly with no changes. This is the default transfer method.
|
lpszCommand [in]
A pointer to a string that contains the command to send to the FTP server.
dwContext [in]
A pointer to a variable that contains an application-defined value used to identify the application context in callback operations.
phFtpCommand [out]
A pointer to a handle that is created if a valid data socket is opened. The fExpectResponse parameter must be set to TRUE for phFtpCommand to be filled.
Return value
Returns TRUE if successful, or FALSE otherwise. To get a specific error message, call GetLastError.
Remarks
GetLastError can return ERROR_INTERNET_NO_DIRECT_ACCESS if the client application is offline. If one or more of the parameters are invalid, GetLastError will return ERROR_INVALID_PARAMETER.
Like all other aspects of the WinINet API, this function cannot be safely called from within DllMain or the constructors and destructors of global objects.
Note WinINet does not support server implementations. In addition, it should not be used from a service. For server implementations or services use Microsoft Windows HTTP Services (WinHTTP).