trancexx

Access AutoIt

41 posts in this topic

#1 ·  Posted (edited)

Majority of users of AutoIt language likes it because it's easy to learn and very powerful when it comes to automating some basically very complicated stuff on windows. AutoIt often does it in just one line of code while doing it in other languages would be writing a full book of code.

How to use AutoIt with other languages?

One way would be to use AutoItX. Dll version of AutoIt. In this case you can access it through COM or exported functions. Downside surely is limited number of available functions and user rights for COM option.

Other way is... no wait, there is no other way.

But, what If I add some magic salt to the soup? Let's say I use some of the code from (thanks Manadar) and, yes you guessed AutoItObject, and make a simple (the most basic) COM server that exports almost the whole AutoIt functionality to anyone wanting to use it.

AutoItObject 1.2.8.0 makes objects published to ROT (running object table) more accessible than ever. Accessing the object is as easy as GetObject(...) in any scripting language and CoGetObject() through Windows API.

An example of VBS would be:

'Start AutoIt server script first

Set oAutoIt = GetObject("AutoIt.Application") ' the magic

WS_OVERLAPPEDWINDOW = &H00CF0000

hGui = oAutoIt.Call("GUICreate", "VBS AutoIt GUI test", -1, -1, -1, -1, WS_OVERLAPPEDWINDOW)
hButton = oAutoIt.Call("GUICtrlCreateButton", "Click", 100, 100, 100, 30)
hButton2 = oAutoIt.Call("GUICtrlCreateButton", "Click me too", 100, 300, 100, 30)

oAutoIt.Call "WinSetOnTop", "VBS AutoIt GUI test", "", 1

AW_FADE_IN  = &H00080000
oAutoIt.Call "DllCall", "user32.dll", "bool", "AnimateWindow", "hwnd", hGui, "dword", 1000, "dword", AW_FADE_IN

oAutoIt.Call "GUISetState"

Do
    Select Case oAutoIt.Call("GUIGetMsg")
        Case -3
            Exit Do
        Case hButton
            oAutoIt.Call "MsgBox", 262144+32+3, "Title", "Bzzz bzz bzzzz", 0, hGUI
        Case hButton2
            oAutoIt.Call "Beep", 500, 700
    End Select
    Wscript.Sleep(10)
Loop

oAutoIt.Call "GUIDelete"

If oAutoIt.Call("MsgBox", 4 + 48 + 262144, "?", "Kill server?") = 6 Then oAutoIt.Quit

Javascript could be:

// Start AutoIt server script first

// Let's go...
try
{
    oAutoIt = GetObject("AutoIt.Application");
    oAutoIt.Call("Beep", 1000, 500)
    oAutoIt.Call("MsgBox", 64 + 262144, "Hi there!", "Is this something or what?")
    if (oAutoIt.Call("MsgBox", 4 + 48 + 262144, "?", "Kill server?") == 6)
    {
        oAutoIt.Quit;
    }
}
catch(err)
{
    WScript.echo("!Ermmm... is server script runing?");
}

C#

using System;
using System.Reflection;
using System.Runtime.InteropServices;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            dynamic myAu3Object = Marshal.BindToMoniker("AutoIt.Application");

            myAu3Object.Call("ToolTip", "Some cool text");
            myAu3Object.Call("Beep", 500, 700);
            myAu3Object.Call("Sleep", 1000);
            myAu3Object.Call("ToolTip", "");
            
            if (myAu3Object.Call("MsgBox", 4 + 48 + 262144, "?", "Kill Server?") == 6)
            {
                myAu3Object.Quit();
            }
        }
        catch
        {
            System.Console.WriteLine("Something is wrong. Double-check everything.");
        }
    }
}

PowerShell (based on ptrex's example)

# Start server script first

$oAutoIt = [System.Runtime.InteropServices.Marshal]::BindToMoniker("AutoIt.Application")

$oAutoItType = $oAutoIt.GetType()

$im = [System.Reflection.BindingFlags]::InvokeMethod
# $gp = [System.Reflection.BindingFlags]::GetProperty

$oAutoItType.InvokeMember("Call", $im, $null, $oAutoIt, ("ToolTip", "Some cool text",600, 300))
$oAutoItType.InvokeMember("Call", $im, $null, $oAutoIt, ("Beep", 500, 700))
$oAutoItType.InvokeMember("Call", $im, $null, $oAutoIt, ("Sleep", "2000"))
$oAutoItType.InvokeMember("Call", $im, $null, $oAutoIt, ("ToolTip", ""))

Python (AdmiralAlkex)

import win32com.client

try:
    oAutoIt = win32com.client.GetObject("AutoIt.Application")
    oAutoIt.Call("Tooltip", "Some cool text")
    oAutoIt.Call("Beep", 500, 700)
    oAutoIt.Call("Sleep", 3000)
    oAutoIt.Call("Tooltip", "")

    if oAutoIt.Call("MsgBox", 4 + 48 + 262144, "?", "Kill Server?") == 6:
        oAutoIt.Quit
except:
    print ("+Ermmm... is server script running?")   #Lets have a green output on this example (SciTE)

...and so on.

Server script is really minimalistic:

#include "AutoitObject.au3"

Opt("MustDeclareVars", 1)

; Error monitoring
Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc")
Func _ErrFunc()
;~  ConsoleWrite("! COM Error !  Number: 0x" & Hex($oError.number, 8) & "   ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF)
    Return
EndFunc   ;==>_ErrFunc

; Initialize AutoItObject
_AutoItObject_StartUp()

; Create object
Global $oObject = _SomeObject()

; Register it globaly with some identifier
Global $sMyCLSID = "AutoIt.Application"
Global $hObj = _AutoItObject_RegisterObject($oObject, $sMyCLSID)

While 1
    Sleep(100)
WEnd

_QuitObject(0)

; Define object
Func _SomeObject()
    Local $oClassObject = _AutoItObject_Class()
    $oClassObject.AddMethod("Call", "_Obj_CallAny")
    $oClassObject.AddMethod("Quit", "_QuitObject")
    Return $oClassObject.Object
EndFunc   ;==>_SomeObject

Func _Obj_CallAny($oSelf, $sFunc, _
        $vParam1 = 0, $vParam2 = 0, $vParam3 = 0, $vParam4 = 0, $vParam5 = 0, $vParam6 = 0, _
        $vParam7 = 0, $vParam8 = 0, $vParam9 = 0, $vParam10 = 0, $vParam11 = 0, $vParam12 = 0, _
        $vParam13 = 0, $vParam14 = 0, $vParam15 = 0, $vParam16 = 0, $vParam17 = 0, $vParam18 = 0, _
        $vParam19 = 0, $vParam20 = 0, $vParam21 = 0, $vParam22 = 0, $vParam23 = 0, $vParam24 = 0, _
        $vParam25 = 0, $vParam26 = 0, $vParam27 = 0, $vParam28 = 0, $vParam29 = 0, $vParam30 = 0)
    Local $sExec = $sFunc & "("
    If @NumParams = 3 And IsArray($vParam1) And UBound($vParam1, 0) = 1 Then
        For $n = 0 To UBound($vParam1) - 1
            $sExec &= "$vParam1[" & $n & "],"
        Next
    Else
        If @NumParams = 2 Then
            $sExec &= ")"
        Else
            For $n = 1 To @NumParams - 2
                $sExec &= "$vParam" & $n & ","
            Next
        EndIf
    EndIf
    Local $vRet = Execute(StringTrimRight($sExec, 1) & ")")
    If IsPtr($vRet) Then Return Number($vRet)
    For $i = 0 To UBound($vRet) - 1
        If IsPtr($vRet[$i]) Then $vRet[$i] = Number($vRet[$i])
    Next
    Return $vRet
EndFunc   ;==>_Obj_CallAny

Func _QuitObject($oSelf)
    MsgBox(262144 + 64, "Server says...", "bye bye", 3)
    _AutoItObject_UnregisterObject($hObj)
    $oObject = 0
    Exit
EndFunc   ;==>_QuitObject

All packed in ZIP:

AccessAutoIt.zip

What you'll find there is folder named AccessAutoIt. Unzip it somewhere and inside there will be five files: AutoItObject.au3 (1.2.8.0), AutoItServer.au3, Csharp.cs, JS.js and VBS.vbs.

Run AutoItServer.au3 and then either vbs or js script or compile Csharp.cs and run resulting executable.

I have chosen AutoIt.Application object moniker for no particular reason. Can be anything.

Latest AutoItObject, if you want the whole thing, is here.

AutoIt on your palm.

edit: Added c# example.

Don't mind about my usage of other languages. I'm not quite familiar with them.

Edited by trancexx
2 people like this

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites



Amazing! :unsure:


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

If this is capable of what I think it's capable of, then: Way Hey! :unsure:

Share this post


Link to post
Share on other sites

Indeed, this is awesome. Good job! :unsure:


When the words fail... music speaks

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

If this is capable of what I think it's capable of, then: Way Hey! :unsure:

This reminds me on one episode of Malcolm in the Middle when he's babysitting this old lady with broken arm. She falls asleep at some point and he invites his geeks over knowing she'll be sleeping for few hours. One "tough" geek says something like: "Do you guys think what I think?", they all go "Yeahhhh!!!".

...If you ever see me laughing with no reason, it's probably because I remembered the next scene from that episode. OMG hilarious! :> It's completely above me. Needless to say, they didn't think what he thought.

I've added c# example. First post.

Edited by trancexx

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

CoGetObject. :unsure:

The code is beautiful.

Could be reduced to:

object myAu3Object = Marshal.BindToMoniker("AutoIt.Application");
;...
Would that be more correct?

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

...If you ever see me laughing with no reason, it's probably because I remembered the next scene from that episode. OMG hilarious! :> It's completely above me. Needless to say, they didn't think what he thought.

Hmm unfortunately I'm not familiar with the episode, so I'll have to imagine what the others were thinking. On the other hand, Access AutoIt may do much more than I think it does. Either way I like the examples. :unsure:

Share this post


Link to post
Share on other sites

Could be reduced to:

object myAu3Object = Marshal.BindToMoniker("AutoIt.Application");
;...
Would that be more correct?

Native .NET calls are preferred over WinAPI / external calls for a number of reasons. Even if it's just a wrapper around an external call.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

@trancexx

This is great !

Now AutoIT has a bi-directional COM approach, which is a new milestone. Very nice implementation indead :unsure:

Here is the Powershell example tested on windows 7 - X64.

# No GetObject in Powershell !! 
# http://technet.microsoft.com/en-us/library/ee176862.aspx
cls

# AutoIT Object instantition
[reflection.assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$oAutoIt = [Microsoft.VisualBasic.Interaction]::GetObject("AutoIt.Application")
$oAutoItType = $oAutoIt.GetType() 

$im = [reflection.bindingflags]::InvokeMethod
$gp = [reflection.bindingflags]::GetProperty 

$oAutoItType.InvokeMember("Call", $im, $null, $oAutoIt, ("ToolTip", "Some cool text",600, 300))
#$oAutoItType.InvokeMember("Call", $im, $null, $oAutoIt, ("Beep", 500, 700))
$oAutoItType.InvokeMember("Call", $im, $null, $oAutoIt, ("Sleep", "2000"))
$oAutoItType.InvokeMember("Call", $im, $null, $oAutoIt, ("ToolTip", ""))

So the only thing that leaves a black spot on AU3 is that it can't access Powershell or .Net classes.

Ps:

Forgot to mention save this example as .PS1 file and you can run this on any windows 7 OS natively.

Don't need to install any additional software dev. environment.

Rgds,

ptrex

Edited by ptrex

Share this post


Link to post
Share on other sites

That's new to me ptrex. I've tested it and works really fine. Thank you. Added to first post,

Thanks again.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

@trancexx

This is pretty damn awesome! :unsure:

Here's a Python example

(needs pywin32)

import win32com.client

try:
    oAutoIt = win32com.client.GetObject("AutoIt.Application")
    oAutoIt.Call("Tooltip", "Some cool text")
    oAutoIt.Call("Beep", 500, 700)
    oAutoIt.Call("Sleep", 3000)
    oAutoIt.Call("Tooltip", "")

    if oAutoIt.Call("MsgBox", 4 + 48 + 262144, "?", "Kill Server?") == 6:
        oAutoIt.Quit
except:
    print ("+Ermmm... is server script running?")   #Lets have a green output on this example (SciTE)

Edit: Updated example to work on both Python 2 and 3 (tested on 2.6.5 and 3.2)

Edited by AdmiralAlkex

Share this post


Link to post
Share on other sites

Posted Image

On another matter, is it my shirt that's electric?


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

50 thumbs up, then some more. You make me wish I had thought of it first every time you post something mate, absolutely fantabulous.


[center][/center]Working on the next big thing.Currently Playing: Halo 4, League of LegendsXBL GT: iRememberYhslaw

Share this post


Link to post
Share on other sites

Yes, your COMlet (Autolet?) way works very good.

:unsure:

and make a simple (the most basic) COM server that exports almost the whole AutoIt functionality to anyone wanting to use it.

Have you thoughts about WEB server creation like your AutoItServer.au3?

So the only thing that leaves a black spot on AU3 is that it can't access Powershell or .Net classes.

You have to build COMlet way (through ROT) from Powershell with help of new AutoIt facility, I think

The point of world view

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

In the scripting language LUA is closed with an error. :unsure:

require("luacom")
oAutoIt = luacom.GetObject("AutoIt.Application")
print(oAutoIt)
Edited by Armin

Share this post


Link to post
Share on other sites

Excellent work trancexx,

Ii have a one question, can we use this approach for IPC ?


73 108 111 118 101 65 117 116 111 105 116

Share this post


Link to post
Share on other sites

Excellent work trancexx,

Ii have a one question, can we use this approach for IPC ?

That goes without saying.

Objects have properties.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

That goes without saying.

Objects have properties.

I am new in objects. can you please post a one example.Posted Image

Edited by Digisoul

73 108 111 118 101 65 117 116 111 105 116

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