Showing posts with label FTP Client. Show all posts
Showing posts with label FTP Client. Show all posts

Thursday, February 25, 2010

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 
    $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
    $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
    $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
    $CloseCC = DllCall("wininet.dll", "int", "InternetCloseHandle", "int", $InternetOpen)
    $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.