Jump to content

Using AutoItX3 in C#


Outshynd
 Share

Recommended Posts

I wrote this for another forum but I figured I'd post it here as well. Hopefully it'll help some poor, lost soul.

C# is a much faster and more complete language than AutoIt. That said, it's also far more difficult to do simple tasks that AutoIt does quite well. You have to write your own MouseMove functions, your own WinActivate functions, your own everything functions (nearly). This is because AutoIt is a Windows automation tool and C# is an actual programming language. Well, can we have the best of both worlds? Sure.

This describes how to add AutoItX (the AutoIt COM Library DLL) to your C# (or other .NET) project and then execute a few simple instructions.

Posted Image

Posted Image

Posted Image

http://www.nomorepasting.com/paste.php?pasteID=72980

using System;
using System.Threading;

namespace AutoItXTest
{
    class Program
    {
        static AutoItX3Lib.AutoItX3Class au3;           //our au3 class that gives us au3 functionality
        static Thread thread;                           //our thread
        static bool threadshouldexecute = true;         //only execute thread 2 while this equals true
        static int i = 0;                               //our incrementer

        /// <summary>
        /// The entry point, or main thread / main loop, of our program
        /// </summary>
        static void Main(string[] args)
        {
            au3 = new AutoItX3Lib.AutoItX3Class();                              //initialize our au3 class library

            au3.AutoItSetOption("WinTitleMatchMode", 4);                        //advanced window matching

            thread = new Thread(new ThreadStart(threadtest));                   //initialize and start our thread
            thread.Start();

            if (au3.WinExists("Untitled - Notepad", "") == 0)                   //if an Untitled - Notepad document doesn't exist
                au3.Run(@"C:\WINDOWS\SYSTEM32\notepad.exe", "", au3.SW_SHOW);   //run notepad
            else
                au3.WinActivate("Untitled - Notepad", "");                      //otherwise activate the window

            string hWnd = "";                                                   //let's use a window handle

            while (hWnd.Length == 0)                                            //try to get a handle to notepad until it succeeds
                hWnd = au3.WinGetHandle("Untitled - Notepad", "");

            while (au3.WinActive("handle=" + hWnd, "") == 0)                    //loop while it's not active
            {
                au3.WinActivate("handle=" + hWnd, "");                          //and activate it
                Thread.Sleep(100);
            }

            while (au3.WinExists("handle=" + hWnd, "") != 0)                    //while the window exists, loop
            {
                //send our incrementing variable, i, to notepad, with a trailing |
                au3.ControlSend("handle=" + hWnd, "", "Edit1", i.ToString() + "|", 0);

                i++;                                                            //increment i

                Thread.Sleep(100);                                              //short sleep so we don't burn CPU
            }

            //if the while loop exited--because there's no Untitled - Notepad--make the other thread stop executing
            threadshouldexecute = false;

            Console.Write("Press [ENTER] to continue...");                      //tell the user to press ENTER to quit
            Console.ReadLine();                                                 //pause until enter is pressed
        }

        /// <summary>
        /// our void function to execute thread #2
        /// </summary>
        static void threadtest()
        {
            while (threadshouldexecute)                             //loop while this thread should execute
            {
                au3.ToolTip("Thread 2\ni: " + i.ToString(), 0, 0);  //display a tooltip with the incrementing variable i in it

                Thread.Sleep(50);                                   //sleep to free up CPU
            }

            au3.ToolTip("", 0, 0);                                  //clear the tooltip after loop is done
        }
    }
}
Edited by Outshynd
Link to comment
Share on other sites

I fail to see the point in the second thread just for a tool tip. Better programmers could have done both in the main loop.

Good job on the walkthrough, but I don't know how many people have trouble referencing the component.

I fail to see the point in a program that spams keypresses in notepad. Maybe, just maybe, it was an example. How many people love the functionality of AutoIt but want multithreading? Holy shit, now maybe someone who doesn't have as much expertise can figure out how to do it.

Referencing the component may not be super difficult but for someone who's just picking up a C# IDE--especially someone whose only coding experience is in AutoIt, as with many of the people who visit this site--may find it slightly helpful to know how to add AutoItX3 to their project without having to come to the forums and ask. Did I have a problem with it? No. Am I so arrogant to think that no one else will have a problem with it? No. Do I really want to read and answer multiple threads/questions on how to reference the DLL? No. Did it hurt to take three small screenshots and upload them to a website? No. I don't see the problem here.

While I appreciate the commendation, the negativity isn't needed.

Link to comment
Share on other sites

  • 3 weeks later...

It really was helpful for me. Exactly as Outshynd described I had quite some experience with AutoIt but just learned the basics of C++ some weeks ago and had no idea how to implement a dll and how to use it, so this tutorial is very very usful for me.

Thanks Outshynd

Link to comment
Share on other sites

  • 3 weeks later...
  • 3 weeks later...
  • 1 year later...
  • 3 weeks later...
  • 1 month later...
  • 3 weeks later...

I use SharpDevelop and when I try to add reference I have no "Browse" tab. I have only "GAC", "Projects", ".NetAssembly browser" and "COM" tabs. Could you please to instruct me how to add reference to the AutoItX3.dll?

Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...