Sign in to follow this  
Followers 0

Using AutoItX3 in C#

53 posts in this topic

#1 ·  Posted (edited)

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
1 person likes this

Share this post


Link to post
Share on other sites



#2 ·  Posted

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.

Share this post


Link to post
Share on other sites

#3 ·  Posted

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.

Share this post


Link to post
Share on other sites

#4 ·  Posted

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

Share this post


Link to post
Share on other sites

#5 ·  Posted

All I was saying is that this is an AutoIt forum, not a .Net forum. They shouldn't need to come here for .Net questions.

Don't bring multithreading into this. Valik has clearly explained, several times, why multithreading is not going to happen until a major revamp, if ever.

Share this post


Link to post
Share on other sites

#6 ·  Posted

This was a nice help for me because I was wondering how hard is it to embed AutoIt in other languages. Apparently it isn't!

Share this post


Link to post
Share on other sites

#7 ·  Posted

This is a very good tutorial.

Now it is easy to access Autoit features from other language.

Share this post


Link to post
Share on other sites

#8 ·  Posted

Thanks!

Share this post


Link to post
Share on other sites

#9 ·  Posted

This works great! but how would i do #include blah.au3 ???

Share this post


Link to post
Share on other sites

#10 ·  Posted

You can't include AutoIt source when you're not using AutoIt.

Share this post


Link to post
Share on other sites

#11 ·  Posted

For an alternate way of using AutoItX3 in C# that does not require registering have a look at this.

On page 2 you will see an updated version for 3.3.0.0 which returns Unicode data.

Regards,

John.

Share this post


Link to post
Share on other sites

#12 ·  Posted

looks cool.

I wish AutoIt was .NET by nature,

that way allllll functions and code libraries(UDFs) could be accessed.

Share this post


Link to post
Share on other sites

#13 ·  Posted

The development team has no interest in doing that. I've been working on an AutoIt.Net variation but it's a side project and I just really haven't had the time.

Share this post


Link to post
Share on other sites

#14 ·  Posted

mmm

how far did you go with it?

Share this post


Link to post
Share on other sites

#15 ·  Posted

As of right now, the project was recently scrapped and restarted. So you tell me.

Share this post


Link to post
Share on other sites

#16 ·  Posted

oh :/

Share this post


Link to post
Share on other sites

#17 ·  Posted

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?

Share this post


Link to post
Share on other sites

#18 ·  Posted

You can't reference COM directly in .Net. You have to build a wrapper assembly around it or call the COM interop methods yourself.

Share this post


Link to post
Share on other sites

#19 ·  Posted

You can't reference COM directly in .Net. You have to build a wrapper assembly around it or call the COM interop methods yourself.

come on,i don't know how to do it,tell more...

Share this post


Link to post
Share on other sites

#20 ·  Posted

Try being more polite and using correct sentence structure.

Have you tried searching the MSDN for accessing COM in .Net languages?

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0