Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Using AutoItX3 in C#


  • Please log in to reply
52 replies to this topic

#1 Outshynd

Outshynd

    Wayfarer

  • Active Members
  • Pip
  • 99 posts

Posted 11 January 2007 - 12:37 PM

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
Plain Text         
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, 14 July 2007 - 10:14 PM.

  • Piyu likes this







#2 Richard Robertson

Richard Robertson

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 10,333 posts

Posted 12 January 2007 - 05:29 AM

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.

#3 Outshynd

Outshynd

    Wayfarer

  • Active Members
  • Pip
  • 99 posts

Posted 13 January 2007 - 01:34 AM

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.

#4 Xenthalon

Xenthalon

    Seeker

  • Active Members
  • 17 posts

Posted 30 January 2007 - 09:12 PM

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

#5 Richard Robertson

Richard Robertson

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 10,333 posts

Posted 30 January 2007 - 11:54 PM

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.

#6 amokoura

amokoura

    AutoIt fanatic

  • Active Members
  • PipPipPipPipPipPip
  • 315 posts

Posted 21 February 2007 - 01:14 AM

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!

#7 korifg

korifg

    Seeker

  • Active Members
  • 15 posts

Posted 12 March 2007 - 09:41 AM

This is a very good tutorial.
Now it is easy to access Autoit features from other language.

#8 Jace

Jace

    Seeker

  • Active Members
  • 12 posts

Posted 12 March 2007 - 10:42 AM

Thanks!

#9 Harusal

Harusal

    Seeker

  • Active Members
  • 21 posts

Posted 11 February 2009 - 01:47 AM

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

#10 Richard Robertson

Richard Robertson

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 10,333 posts

Posted 11 February 2009 - 02:04 AM

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

#11 jcddcjjcd

jcddcjjcd

    Seeker

  • Active Members
  • 30 posts

Posted 13 February 2009 - 01:40 AM

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.

#12 Zohar

Zohar

  • Active Members
  • PipPipPipPipPipPip
  • 659 posts

Posted 02 March 2009 - 09:05 AM

looks cool.
I wish AutoIt was .NET by nature,
that way allllll functions and code libraries(UDFs) could be accessed.

#13 Richard Robertson

Richard Robertson

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 10,333 posts

Posted 02 March 2009 - 05:04 PM

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.

#14 Zohar

Zohar

  • Active Members
  • PipPipPipPipPipPip
  • 659 posts

Posted 12 April 2009 - 05:01 AM

mmm
how far did you go with it?

#15 Richard Robertson

Richard Robertson

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 10,333 posts

Posted 12 April 2009 - 09:14 AM

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

#16 Zohar

Zohar

  • Active Members
  • PipPipPipPipPipPip
  • 659 posts

Posted 13 April 2009 - 04:31 AM

oh :/

#17 Gwen2

Gwen2

    Seeker

  • New Members
  • 1 posts

Posted 30 April 2009 - 01:58 PM

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?

#18 Richard Robertson

Richard Robertson

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 10,333 posts

Posted 30 April 2009 - 02:10 PM

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

#19 justdoit123

justdoit123

    Seeker

  • Normal Members
  • 1 posts

Posted 16 June 2009 - 02:25 AM

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...

#20 Richard Robertson

Richard Robertson

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 10,333 posts

Posted 16 June 2009 - 03:04 AM

Try being more polite and using correct sentence structure.

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users