Jump to content
Jon

Using AutoItX from C# / .Net

Recommended Posts

Jon

The following files are provided to allow .NET use:

  • AutoItX3.Assembly.dll - The .NET Assembly for using AutoItX.
  • AutoItX3.Assembly.xml - The Visual Studio Intellisense help file for the .NET Assembly.
  • AutoItX3.dll - The main AutoItX DLL (x86)
  • AutoItX3_x64.dll - The main AutoItX DLL (x64)

Using the Assembly from VB/C# within in Visual Studio is very easy:

  • Add a reference to AutoItX3.Assembly.dll to your project
  • Add a using AutoIt; statement in the files you want to use AutoIt functions
  • Write code like this C# example:
using AutoIt;

...

// Wow, this is C#!
AutoItX.Run("notepad.exe");
AutoItX.WinWaitActive("Untitled");
AutoItX.Send("I'm in notepad");
IntPtr winHandle = AutoItX.WinGetHandle("Untitled");
AutoItX.WinKill(winHandle);
  • Distribute your final executable with the files AutoItX3.Assembly.dll, AutoItX3.dll, AutoItX3_x64.dll.
  • Like 2
  • Haha 1

Share this post


Link to post
Share on other sites
xhuyvn

nice and easy but got error when run, Autoitx3.Dll can not access.

 reg dll like this "regsvr32 c:\AutoitX\AutoitX3.dll" for use, got an error 

what can i do now for using autoitx in C#

i'm using VS community 2015.

autoit error run.jpg

autoit error reg.jpg

Edited by xhuyvn

Share this post


Link to post
Share on other sites
xhuyvn

nice and easy but got error when run, Autoitx3.Dll can not access.

 reg dll like this "regsvr32 c:\AutoitX\AutoitX3.dll" for use, got an error 

what can i do now for using autoitx in C#

i'm using VS community 2015.

autoit error run.jpg

autoit error reg.jpg

i was solved this problem. just copy AutoitX3.dll to bin directory and it work like a charm.

Share this post


Link to post
Share on other sites
nafberger

can you please help me, i have the same trouble. added references to dll files, put using statement. but error comes up, it can't find autoit3x dll, whats going on?

Share this post


Link to post
Share on other sites
Eternal21

The DllNotFoundException is caused by screwed up assembly dependencies when the dll package was built.  Nuget installer for AutoitX is not working for the same reason (more details here).  Bottom line - Visual Studio doesn't know it needs to copy AutoItX3.dll to the output directory.  It only copies AutoItX3.Assembly.dll, and forgets about the other one.

The easiest solution is ensuring AutoItX3.dll is in your output bin/Debug or bin/Release directory, when you run your program (basically the same directory as your exe).  You can do that by simply placing AutoItX3.dll in the root of your project in visual studio, and then setting its properties to:

Copy to Output Directory: 'Copy if newer'

  • Like 1

Share this post


Link to post
Share on other sites
antonioj84

Hi can someone enlighten me, I have a current application in autoit that calculate IP address. would I be able to port that to c# using autoitx3.dll then that application can be run off the browser. This is fascinating if true

thanks ahead for any answers

Edited by antonioj84

Share this post


Link to post
Share on other sites
JohnOne

I'm not sure what your post is getting at, with "run off the browser".

AutoIt3X allows you to reference the dll in C# and use its functions, which are a subset of AutoIt's native functions.

You wont find UDF functions such as _IE* etc...


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
antonioj84

Hi Johnone,

I understand that part, however I mean in that case can the application run from a IIS server, then user can access thru their browser. I want to be able to use the native udf that come with autoit.

Edited by antonioj84

Share this post


Link to post
Share on other sites
abdulrahmanok

Thank you very much I was looking for this ^_^

Share this post


Link to post
Share on other sites
dabaracus
On 01/02/2016 at 4:18 AM, Eternal21 said:

The DllNotFoundException is caused by screwed up assembly dependencies when the dll package was built.  Nuget installer for AutoitX is not working for the same reason (more details here).  Bottom line - Visual Studio doesn't know it needs to copy AutoItX3.dll to the output directory.  It only copies AutoItX3.Assembly.dll, and forgets about the other one.

The easiest solution is ensuring AutoItX3.dll is in your output bin/Debug or bin/Release directory, when you run your program (basically the same directory as your exe).  You can do that by simply placing AutoItX3.dll in the root of your project in visual studio, and then setting its properties to:

Copy to Output Directory: 'Copy if newer'

This resolved the issue when executing from visual studio, however when the build is put into an MTM build directory the error returns. Have confirmed that the dll is in the build directory.

 

Any thoughts?

Share this post


Link to post
Share on other sites
dafie
On 18.09.2015 at 6:21 AM, xhuyvn said:

nice and easy but got error when run, Autoitx3.Dll can not access.

 reg dll like this "regsvr32 c:\AutoitX\AutoitX3.dll" for use, got an error 

what can i do now for using autoitx in C#

i'm using VS community 2015.

autoit error run.jpg

autoit error reg.jpg

How can I list all windows with `AutoITX` static field? Seems most of methods are preserved, but `WinList` is missing. 

Share this post


Link to post
Share on other sites
Radios

Hi,

I tried to select an email in Outlook 2013 but without success. I succeeded with the below code to open the Outlook window and send a TAB

public void Outlook()
        {

            autoit.Run("C:\\Program Files (x86)\\Microsoft Office\\Office15\\outlook.exe");
            autoit.WinActivate(MessageText);
            
            string windowName = "Inbox - ";
            int found = autoit.WinWait(windowName, "", 10);
            if (found != 0)
            {
                autoit.WinActivate(windowName);
                
                autoit.Send("{ENTER}");
                autoit.Send("{TAB}");

            }
        }

Any idea or sugestion how to do this?

Share this post


Link to post
Share on other sites
LarsJ

Using C# from AutoIt and .Net

Now that we can run C# and VB code in AutoIt it could be interesting to see, if we can run the C# code from first post in AutoIt:

using AutoIt;
using System;
using System.Threading;

public class Au3Class
{
  public void MyMethod()
  {
    // Wow, this is C#!
    AutoItX.Run("notepad.exe", "", AutoItX.SW_SHOW);
    AutoItX.WinWaitActive("Untitled");
    AutoItX.Send("I'm in notepad{Enter}{Enter}");
    AutoItX.Send("From AutoItX in C{#}{Enter}{Enter}");
    AutoItX.Send("From C{#} in AutoIt through .NET Framework{Enter}");
    Thread.Sleep(5000);
    IntPtr winHandle = AutoItX.WinGetHandle("Untitled");
    AutoItX.WinKill(winHandle);
  }
}
#include "DotNetAll.au3"

Example()

Func Example()
  ; Wow, this is AutoIt!
  Local $oComErrFunc = ObjEvent( "AutoIt.Error", "ComErrFunc" )
  #forceref $oComErrFunc

  Local $oNetCode = DotNet_LoadCScode( FileRead( "AutoItXTest.cs" ), "System.dll | AutoItX3.Assembly.dll" )
  Local $oAu3Class = DotNet_CreateObject( $oNetCode, "Au3Class" )
  $oAu3Class.MyMethod()
EndFunc

You must copy AutoItX3.Assembly.dll, AutoItX3.dll and AutoItX3_x64.dll to @ScriptDir. And you must copy AutoItX3.Assembly.dll to AutoIt installation folder where AutoIt3.exe and AutoIt3_x64.exe are installed.

The code: AutoItXTest.7z

Edited by LarsJ
  • Like 1

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

  • Similar Content

    • PramodR
      By PramodR
       
      I see couple of ways to import module ,  one with specifying absolute path of the file to get imported, by this logic i believe to import a specific module will be like #include <c:\modulepath\duplicatemodule.au3>
      but i see some registry changes also made in this link , can someone explain why that registry change is really required.
       
    • PramodR
      By PramodR
       
      I am trying to execute a exe file in auto it as elevated previlage , but not successful till now . below are the methods i tried till now.
      Disabled UAC access from windows and  selected run only as administrator from executable properties.  i logged in as administrator but still exe needs to launch as run as administrator.
       
      Used Below methods from forum still no luck. anything i missing? i tried to use powershell.ps1 intead of exe as well.
      1.  Local $iPID = RunAs($sUserName, @ComputerName, $sPassword, $RUN_LOGON_NOPROFILE, "c:\Exec.exe", "", @SW_SHOWMAXIMIZED)
      2.  RunWait("C:\Automation\TestScripts\Writefilter.exe", @WindowsDir, @SW_MAXIMIZE)     - Std out error
       
      This command gives out put as running with admin rights
      #include <MsgBoxConstants.au3> #include <AutoItConstants.au3> #include <MsgBoxConstants.au3> #RequireAdmin Example() Func Example()     ; Change the username and password to the appropriate values for your system.     Local $sUserName = "Admin"     Local $sPassword = "PASSWORD"     Local $iPID = RunAs($sUserName, @ComputerName, $sPassword, 0, "noetpad.exe", "", @SW_SHOWMAXIMIZED)     ; Run Notepad with the window maximized. Notepad is run under the user previously specified. ;~     RunWait(@WindowsDir & "C:\automation\TestScripts\Writefilter.exe", @WindowsDir, @SW_MAXIMIZE) EndFunc   
    • PramodR
      By PramodR
      Hello,
      I have below code which execute well when all prerequisite met, when window pop up is not available its throwing an error. i want to capture error instead of error  out.
      error handling added in the code to capture it but it does not work still giving me error saying Variable must be of type "Object".
      $oCurWin.FindAll($treescope_subtree, $oCondition, $pElements)
      $oCurWin^ ERROR
      How to catch error properly . 
       
       
    • PramodR
      By PramodR
      Hello,
      I have to execute below block of code in elevated previllage, as that particular wmic class will work only with admin previlage.
      i am getting out put when i launch autoit as administrator,  but i will not be able to launch autoit as admin in this particular case but my user have admin rights.
      is there any better way i can run those command as administrator.?
      tried below steps:
      i have tried #RequireAdmin but that creates a user prompt  Tried using Runas command but gives error as wrong username or password , stuck with that step.  
       
    • IgImAx
      By IgImAx
      Hi
       
      UPDATED Notes: ===============================================================================
      UPDATED: My main and second questions answered. Here the answers:
      To add/access WinMenuSelectItem you need to [Thanks to Fernando_Marinho]: Add AutoItX.Dotnet in Manage NuGet Packages Right Click in your Project -> Add -> Reference... -> COM ( Type Libraries )than, check the option AutoItX3 1.0 Type Library  using AutoItX3Lib; AutoItX3 au3 = new AutoItX3(); au3.WinMenuSelectItem("", ...) My full source code in C# exists in 11 posts in below. How to access those overloaded methods in AutoitX3 that are not accessible via above method!? Or how to fix AutoitX3 DLL Registration need in target computers without Autoit pre-installed on them!? Please check my post at 14 posts below!
      =============================================================================== Original Post:
       
       
      I was writing a small app in Autoit to close µTorrent app. It was working. Then I try to import AutoItX into C#, but unfortunately this method
      WinMenuSelectItem Couldn't find by IntelliSense and If I typed completely it still give me this message:
      Please check the image. I Google it and I found this QA at stackoverflow: Autoit error within C# application I saw they use this line:
      au = new AutoItX3Lib.AutoItX3Class(); I figure it how to add 'AutoItX3Lib' to project (by adding 'AutoItX3.dll' to reference) but again! When I use this line:
      var au = new AutoItX3Class(); I got this error message: Interop type 'AutoItX3Class' cannot be embedded. Use the applicable interface instead.
      My system info:
      Visual Studio 2017 Enterprise - v15.5.4
      X64 Windows 10 Enterprise 1607
      Thanks in advanced
      IgImAx

×