Sign in to follow this  
Followers 0

Using AutoItX3 in C#

53 posts in this topic

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
Piyu likes this

Share this post


Link to post
Share on other sites



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

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

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

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

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

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

Posted

Thanks!

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

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

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

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

Posted

mmm

how far did you go with it?

Share this post


Link to post
Share on other sites

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

Posted

oh :/

Share this post


Link to post
Share on other sites

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

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

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

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

Posted (edited)

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

If I had to guess, I would say the project failed because you are a jerk and no one wanted to work with you? I found the original post very useful and well intentioned, which is more than I can say for your responses.

A better person would have been more considerate and helpful in his criticisms. Although, I did find it humorous how slapped justdoit123 down and while padding your post count, that will teach him to ask clarification on one of your vague, but obviously superior posts.

Wow, you got my Gander up something fierce.

Anyone, if anyone needs some basic C# help getting this working message me and I can help answer some questions. I have been using C# for 8-9 years and autoit since v2

Edited by MichaelCowan

Share this post


Link to post
Share on other sites

Posted

The project has been a solo project from the beginning. I never said it failed either. I said I got rid of my current progress and started over.

My responses are accurate. They don't need to be polite. Now that I've said that, show me where I have been inconsiderate.

Share this post


Link to post
Share on other sites

Posted

The project has been a solo project from the beginning. I never said it failed either. I said I got rid of my current progress and started over.

My responses are accurate. They don't need to be polite. Now that I've said that, show me where I have been inconsiderate.

You are a big boy, check out the definition of inconsiderate, then look back over your last few posts. I am sure a smart guy like you can figure it out.

Adj. 1. inconsiderate - lacking regard for the rights or feelings of others; "shockingly inconsiderate behavior"

selfish - concerned chiefly or only with yourself and your advantage to the exclusion of others;

tactless, untactful - lacking or showing a lack of what is fitting and considerate in dealing with others; "in the circumstances it was tactless to ask her age"

thoughtless - showing lack of careful thought; "the debate turned into thoughtless bickering"

Share this post


Link to post
Share on other sites

Posted

Tell me how those posts are inconsiderate.

And if everyone is ignoring me, why do you feel the need to call me out?

Share this post


Link to post
Share on other sites

Posted

wow, dude - you ARE a jerk. The idea behind a forum is to help people - if you don't plan to provide anybody with a meaningful solution to their issue why post anything. It that aspect you are a jerk - and yes, I did ignore almost everything you had posted.

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




  • Recently Browsing   0 members

    No registered users viewing this page.