Page Options
You are here : Blog
...and the Geek shall inherit the Earth Blog... Minimize
Nov 14

Written by: Lance Larsen
11/14/2008 12:15 PM 

Created a library of helper methods for integrating AutoIt into a C# application.

It's a little difficult to see what's happening in this YouTube video based on their resolution - but it give you the idea that the example application brings up NotePad, enters text automatically, moves the mouse around, can read the text and then closes NotePad - all being controlled in the code.

It has a great deal of flexibility - and being embedded into C# gives us the best of both languages.

Download: The "Automator" example code and AutoItHelper library ( Visual Studio 2008 )

Recently, I had the need to perform timing / response analysis for a C# WinForms application, just as if a user were entering data through the UI. 

Found the best solution to be a freeware automation scripting language named AutoIt ( http://www.autoitscript.com/ ).

With AutoIt - you can write simple text scripts to accomplish many automation tasks - but - we had to do some deserialization of some particularly nasty legacy XML, so I looked into incorporating the AutoIt SDK into a C# project to further leverage the abilities of C#.

I was able to find a couple resources that pointed me in the correct direction - but nothing that did everything that I wanted it to, so I took what I could and from there - in the words of one of my fellow architects - I had to "roll my own"...

Wanted to get this up as a resource for everyone else that needs to do similar automation - or just want to play around with an excellent automation tool library incorporated into C#.

Here is an example C# Visual Studio 2008 project with the AutoItHelper library referenced - which as the video above shows, automates Notepad.

For more details of the code "under the hood" - here is the RunNotepad() method.

   1:  private void RunNotepad()
   2:  {
   3:      //----------------------------------------------------------------------
   4:      // Wait for initial window
   5:      //----------------------------------------------------------------------
   6:      AutoItX.WinWaitActiveWindow("Untitled - Notepad");
   7:   
   8:      //----------------------------------------------------------------------
   9:      // Move and Resize the window
  10:      //----------------------------------------------------------------------
  11:      NextActionDelay();
  12:      SetStatus("Move and Resize Window...");
  13:      AutoItX.WinMove("Untitled - Notepad", 100, 100, 500, 400);
  14:      NextActionDelay();
  15:      AutoItX.WinMove("Untitled - Notepad", 0, 100, 600, 500);
  16:   
  17:      //----------------------------------------------------------------------
  18:      // Now that the Notepad window is active type some text
  19:      //----------------------------------------------------------------------
  20:      SetStatus("Typing...");
  21:      SendKeys("This is a test of the AUTOMATOR.{ENTER}You will be AUTOMATED.{ENTER}");
  22:      NextActionDelay();
  23:      SendKeys("+{UP 2}");
  24:      NextActionDelay();
  25:   
  26:      //----------------------------------------------------------------------
  27:      // Get the text from the window and show tooltip
  28:      //----------------------------------------------------------------------
  29:      string sText = AutoItX.WinGetText("Untitled - Notepad");
  30:      AutoItX.Tooltip(string.Format("Text From Window: \"{0}\"", 
sText.Replace("\r", "").Replace("\n", "")));
  31:      AutoItX.Sleep(3000);
  32:   
  33:      //----------------------------------------------------------------------
  34:      // Move the mouse around
  35:      //----------------------------------------------------------------------
  36:      SetStatus("Moving the Mouse...");
  37:      AutoItX.MouseMove(0, 0, 50);
  38:      AutoItX.MouseMove(300, 300, 50);
  39:      AutoItX.MouseMove(600, 300, 50);
  40:      NextActionDelay();
  41:   
  42:      //----------------------------------------------------------------------
  43:      // Set message
  44:      //----------------------------------------------------------------------
  45:      SetStatus("Closing Notepad...");
  46:   
  47:      //----------------------------------------------------------------------
  48:      // Now quit by pressing Alt-f and then x (File menu -> Exit)
  49:      //----------------------------------------------------------------------
  50:      NextActionDelay();
  51:      SendKeys("!f");
  52:      NextActionDelay();
  53:      SendKeys("x");
  54:   
  55:      //----------------------------------------------------------------------
  56:      // Now a screen will pop up and ask to save the changes, the window is 
  57:      //  called "Notepad" and has some text "Yes" and "No"
  58:      //----------------------------------------------------------------------
  59:      AutoItX.WinWaitActiveWindow("Notepad");
  60:      SendKeys("n");
  61:   
  62:      //----------------------------------------------------------------------
  63:      // Set message
  64:      //----------------------------------------------------------------------
  65:      SetStatus("Automation Complete!");
  66:  }


Further looking at the AutoItHelper library - you'll see that I've tied directly into the AutoItX3.dll as follows:

   1:  //AU3_API long WINAPI AU3_WinWaitActive(const char *szTitle, /*[in,defaultvalue("")]*/const 
   2:  // char *szText, /*[in,defaultvalue(0)]*/long nTimeout);
   3:  [DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)]
   4:  private static extern int AU3_WinWaitActive([MarshalAs(UnmanagedType.LPStr)]string Title
   5:  , [MarshalAs(UnmanagedType.LPStr)] string Text, int Timeout);


This was an idea from one of the resources that I found online, and I extended and incorporated it.

The big advantage of using AutoItX3 like this is that you don't have to register the dll with windows and it is easily included in the project solution - downside is that it's more work on that had to be done in the AutoItHelper library - but that's already done, so no downside. :)

You'll see below, that I then wrapped all the methods - streamlining where possible - and adding extensive comments from AutoIt's CHM help file - which I included in project for future reference.

   1:  ///----------------------------------------------------------------------
   2:  /// <summary>
   3:  /// Pauses execution of the script until the requested window is active.
   4:  /// </summary>
   5:  /// <param name="vsTitle">The title of the window to check.</param>
   6:  /// <param name="vsText">[optional] The text of the window to check.</param>
   7:  /// <param name="iTimeout">[optional] Timeout in seconds</param>
   8:  /// <returns>
   9:  /// Success: Returns 1. 
  10:  /// Failure: Returns 0 if timeout occurred.
  11:  /// </returns>
  12:  ///----------------------------------------------------------------------
  13:  public static int WinWaitActive(string vsTitle, string vsText, int iTimeout)
  14:  {
  15:      return AU3_WinWaitActive(vsTitle, vsText, iTimeout);
  16:  }

There are a number of functions in AutoIt that I didn't need to utilize, so there is room for future enhancements - either by myself or others. 

If anyone else does extends this - please send those updates back to me and I will continue to integrate those into this library for everyone to utilize.

Good coding!

...Lance Larsen

Tags:

13 comment(s) so far...

Re: AutoIt C# Helper Library

This is really great, Im going to have to download and check this out when I have a chance. Thanks for putting this out in the world!

By Beav on   11/27/2008 8:31 PM

Re: AutoIt C# Helper Library

Absolutely fantastic. Thanks, very handy. I really didnt want to register that dll for the development setup!

By Berno on   1/1/2009 7:03 PM

Re: AutoIt C# Helper Library

This is fantastic. Combining the powerfull functions from a basic language with a oo language, perfect!
Thanks for sharing this wonderful wrapper :)

By Casper on   2/25/2009 12:43 PM

Re: AutoIt C# Helper Library

this made my day :)
I had the program I wanted built entirely in autoit from before and I was looking for the same functionality in C# without having to use sendmessage
this helps tremendously
(I looked for this stuff for around 80 hours before giving up and then someone linked me to this :) )

By jeanbern on   5/9/2009 12:18 PM

Re: AutoIt C# Helper Library

Seems great trying it ... might solve my lot of problems

By Abhinav on   6/10/2009 4:30 AM

Re: AutoIt C# Helper Library

Hi, sorry, but I am rather new to c#. I cannot get this to work.

I added the AutoItHelper project to my Solution is visual studios. Added reference to it in my main project and put "using AutoItHelper;" in my usings section.

Whenever I call a AutoitX3 function, it always gives dllnotfoundexception.

Any help is appreciated.

By j C on   8/3/2009 12:13 AM

Re: AutoIt C# Helper Library

For any of you noobs like me. Just copy the AutoItHelper.cs file and the AutoItX3.dll to your current project that you want to use it in.

By j C on   8/3/2009 12:39 AM

Problem with Autoit

Hi@all,
I try to use the Send function with the parameter downlen, but it doesn't work.

AutoItX.Send("{UP}",1,5000)

downlen should be the time in ms will pressed the key?!

Thx4help
Tanna

By Tanna on   8/16/2009 3:46 PM

Re: AutoIt C# Helper Library

THIS IS GREAT!

Thanks heaps for sharing. Really made my application much easier to develop using you library.

Keep up the good work :)

By Lorenzo on   9/28/2009 9:33 PM

Re: AutoIt C# Helper Library

the faaaroth out was serious.

This is very usefull, i need to take it a step further and was wondering if anyone knows how to call an instance of $ie = _IECreate from C# to Autoit as the functionality is not in the DLL its contained in a serperate UDF called IE.au3.

Essentially what im trying to do is access all the functions in IE.au3 from C#???

Thanks the Pohlecat.

By Nathan Pohle on   11/19/2009 6:50 PM

Re: AutoIt C# Helper Library

Thanks! This will save me a lot of time and stop me from worrying if the user or a virus unregister the AutoIt COM

By Martheen on   1/31/2010 7:39 AM

Re: AutoIt C# Helper Library

Hi, how can i use this with a x64 windows?

Thank you!

By iluminatii on   5/13/2010 9:38 AM

Re: AutoIt C# Helper Library

The article is good is about the AutoItHelper.cs file and the AutoItX3.dll to your current project that you want to use it in. I like the article very much as it is very informative and hope to see more of such articles.

By C# DEvelopment on   6/15/2010 4:40 AM

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 
Categories Minimize
Search Blog Minimize
Publish Dates Minimize

Copyright 2008 by Lance Larsen ( A.I. Labs )
Privacy StatementTerms Of Use