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.

6 comments:

  1. do you know if it's possible make an api call from AutoIT using ChangeDisplaySettingsEx that will cycle through the current monitors and for those not connected to the desktop set them to "extend" the desktop? I'm having a heck of of time getting the DllStructCreate for the DEVMOD setup properly to pass in the DMPOSITION...

    ReplyDelete
  2. I would love access to the code. I am working on somthing like this but having problems. damod@vestas.com

    ReplyDelete
  3. Could you please send me the code? I'd love to have a look at it.

    Thanks!

    ReplyDelete
  4. Please, could you send me the code ? I'd like to make simple application for automated FTP receive.

    zeka215 gmail com

    Thanks

    ReplyDelete
  5. I would like to have the source code. Thank You

    ReplyDelete
  6. I created a similar script with AutoIt that also checks if the file has been uploaded correctly. Find the source here:

    http://getstreaming.wordpress.com/2011/07/06/ftp-upload-script/

    ReplyDelete