Jump to content

COM Automation in AutoIt; no longer a myth.


SvenP
 Share

Recommended Posts

Outstanding. I was too cowardly to have a go at adding COM functionality.

That's just an 'cosmetic' issue.  Changing the name of a function call is no big deal in AutoIt,  providing that you know the 'alphabet'  (<- inside joke about only Jon can laugh :-).

BTW: Concerning my previous question in this topic; I think I've solved the problem with connecting to the WMI namespace.  However I'm stuck with the FOR EACH xx in xx loop. I don't seem to be able to 'enumerate' objects from a 'collection' without using such loop structures.

Have you thought about concatenating the results of a collection in a delimited string? I had thought about using that method specifically for multiple results from WMI, but you might need something more versatile for COM collections in general.

Another option might be an AutoIt array; either way you get a single returned object for the AutoIt function call...

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Link to comment
Share on other sites

Hello guys,

It's 01:55 AM at this moment, but I just want to tell you this:

Just a few minutes ago I successfully finished programming a FOR .. IN loop implementation in AutoIT.

It took me a lot of research, because information was very scarce about this specific subject. ( Finally the magic keyword was IENumVariant types..:-)

And...now I've got WMI-enumeration 90% working !!

The following example DOES ACTUALLY work in AutoIT:

$MyCompName = @ComputerName

$oWMIservice =  getObject("winmgmts:\\" & $MyCompName  & "\root\cimv2")

$ColProcessors = $oWMIService.ExecQuery("Select * from Win32_Processor")

FOR $Processor IN $ColProcessors

  Msgbox(0, "WMI Test","Processor Id: " & $Processor.processorid)
  Msgbox(0, "WMI Test","Maximum Clock Speed: " & $Processor.MaxClockSpeed)

NEXT

Ok, it's very preliminary..

I first thought the code above was faulty because it reported me twice a processor of 3150 MHz, but then it turned out: it's HyperTreading ofcourse :-).

BTW: For the Syntax-freaks among us: The FOR loop is backwards compatible to Pre-COM Autoit versions, so it won't break your older scripts.

Now I get some sleep....and get rid of all my VBS scripts...

Regards,

-Sven

Link to comment
Share on other sites

As many people here, and just as you Sven, I'll be glad to get rid of my VBS scripts just made to get informations using WMI...

With your work on COM (and WMI as a consequence), many people will definitively switch to AutoIt language as a real standalone scripting solution!

Be sure I'll use your inovations and will feedback with my experiences.

Thank you for what you do :idiot: (and I don't forget the original AutoIt developpers who made AutoIt the brilliant tool it already is)

A good program computing A into B is mostly one that won't crash in all the other cases...
Link to comment
Share on other sites

Great!

AutoIT-COM works now also with AutoItX3 !!

; 'stolen' from the VBS example in the AutoItX3 beta web page

$oShell = CreateObject("WScript.Shell")
$oAutoIt = CreateObject("AutoItX3.Control")

Msgbox (0,"","This script will run some test calculations")

$oShell.Run("calc.exe", 1, 0)

; Wait for the calc window to become active
$oAutoIt.WinWaitActive("Calculator", "")

; Send some keystokes to calc
$oAutoIt.Send( "2*2=")
$oAutoIt.Sleep( 500)
$oAutoIt.Send( "4*4=")
$oAutoIt.Sleep( 500)
$oAutoIt.Send( "8*8=")
$oAutoIt.Sleep( 500)
$oAutoIt.WinClose( "Calc", "")
$oAutoIt.WinWaitClose( "Calc", "")

Quite useful isn't it?? *grin*

Regards,

-Sven

PS: This post is not to be taken seriously, but the code does actually work.

Link to comment
Share on other sites

Outstanding. I was too cowardly to have a go at adding COM functionality.

Have you thought about concatenating the results of a collection in a delimited string? I had thought about using that method specifically for multiple results from WMI, but you might need something more versatile for COM collections in general.

Another option might be an AutoIt array; either way you get a single returned object for the AutoIt function call...

<{POST_SNAPBACK}>

Hi DaveF,

Thanks for your idea's. As you already might have read; I've solved the enumeration problem. Is wasn't that easy as just 'concatenating' strings. I'll PM it you.

Regards,

-Sven

Link to comment
Share on other sites

Sven I wrote this feedback someday ago to another thread.

I don't know if you have seen it.

In case of I repost here

Thanks for your contribution

----------------------------------

You can find below some questions about Object Management in AutoIt.

1) Is it possible to pass a parameter to a method ?

Example :
        object.Exec(command)

2) If a property returns a collection, is it possible to access

the right item through an index ?

Example :
        Set Sh = WScript.CreateObject ("WScript.Shell")
        Set WshSysEnv = Sh.Environment ("Process")
        Windir = WshSysEnv("windir")
        Systemdrive = WshSysEnv("Systemdrive")

3) Is it possible to get class instances ?

Example :
        For Each objOS in GetObject("winmgmts:").InstancesOf
("Win32_OperatingSystem")
                ........
            Next

4) Is it possible to get the error number and error message of

the windows run time to trap some specific errors

Example :

          objGroup.SetInfo ()

          If Err <> 0 Then
          ' Test error number 1379 which means that the group already exists

            If (CStr(Err.Number AND K_0xFFFF) = 1379) Then
              Err.Clear
            Else
             errnumber = Err.Number
             On Error GoTo 0
              Err.Raise errnumber
            End If
          End If
Link to comment
Share on other sites

Holy... what? Holy... AutoIT starting to do COM/OLE???

http://www.windowsitpro.com/Windows/Articl.../4461/4461.html (article that explains more about VBScript and COM/OLE... bit old though)

http://www4.dogus.edu.tr/bim/bil_kay/prog_...vex/axu02fi.htm OLE/COM/ACTIVEX Explained & very understandable

http://host16.webserver1010.com:5100/caspd...ct_function.htm (VBScript createobject command)

http://host16.webserver1010.com:5100/caspd...ct_function.htm (VBScript getobject command)

VBScript's death grip as a scripting language has been COM/OLE/ACTIVEX, though some scripting languages like Perl, Jscript, etc... have made major gains in this area. VBA (Visual Basic for Applications) takes it one step further with its major in OLE automation. One reason for the importance of OLE automation is interacting with MS Office applications. MS Office is often EVERYWHERE. OLE automation is kind of like the "macro language" that MS Office apps. like Excel, Word, Outlook, etc... speaks to allow you to manipulate its properties and methods.

The more AutoIT can do with COM/OLE, than the less you have to rely on VBScript. DLL calls and COM/OLE support means you can primarily use AutoIT, except for very low level functions or creating DLLs. ( for newbies and making DLLs check out www.lazarus.freepascal.org ). AutoIT becomes the PRIMARY interface in which many would use to serve their scripting and automation. Many newbies and part-time programmers would often have no need to go outside of AutoIT, unless they meant to do some heavyweight programming (like making dlls for example). The only thing close to offer this kind of range that I've seen is WinBatch.

Long live AutoIT...

Edited by autoitNOW
An ADVOCATE for AutoIT
Link to comment
Share on other sites

Sven I wrote this feedback someday ago to another thread.

I don't know if you have seen it.

In case of I repost here

Thanks for your contribution

Hello jpm,

I'm sorry, I didn't notice the thread. But here are the answers:

1) Is it possible to pass a parameter to a method ?

Answer: Yes, see my AutoitX example in this thread.

2) Yes, the code below produces:

C:\WINDOWS

C:

$Sh = CreateObject ("WScript.Shell")
$WshSysEnv = $Sh.Environment ("Process")

$string = ""

$Windir = $WshSysEnv("windir")

$String = $String & $Windir & @crlf

$Systemdrive = $WshSysEnv("Systemdrive")

$String = $String & $systemdrive & @crlf

msgbox (0,"test", $string)

3) Yes, but you have to split the GetObject() line

$oWMI = GetObject("winmgmts:")

  $string = ""

  FOR $objOS IN $oWMI.InstancesOf("Win32_OperatingSystem")


      Msgbox(0,"Result", "Name = " & @TAB & $objOS.Caption & @CRLF & _
        "Version = " & @TAB & $objOS.Version & @CRLF & _
        "Registered User = " & @TAB & $objOS.RegisteredUser @CRLF & _
        "Manufacturer = " & $objOS.Manufacturer )
  NEXT

HOWEVER, I should advise using ExecQuery() instead, because InstancesOf() can be a VERY slow WMI-call.

4) I tested the following code:

; Add User, Add User to group test

$objContainer=GetObject("WinNT://domain")
$strUser="CBrooke"
$clsUser=$objContainer.Create("User", $strUser)
$clsUser.SetInfo
$objGroup=GetObject("WinNT://domain/MyGroup, group")

$objGroup.Add($clsUser.ADsPath)
$objGroup.SetInfo

At the .SetInfo line it produced the test-message: "SetInfo Failed, Error(0x80020009):Exception occured"

Ofcourse, because "domain" does not exist on my computer.

I programmed a call to 'FatalError()' when any COM-error occures, so the script execution will currently stop at that line.

But I could change this by allowing the script to continue and pass the error code in a result code.

Regards,

-Sven

Link to comment
Share on other sites

Holy... what? Holy... AutoIT starting to do COM/OLE???

http://www.windowsitpro.com/Windows/Articl.../4461/4461.html (article that explains more about VBScript and COM/OLE... bit old though)

http://www4.dogus.edu.tr/bim/bil_kay/prog_...vex/axu02fi.htm (COM/OLE/ACTIVEX info.)

http://host16.webserver1010.com:5100/caspd...ct_function.htm

http://host16.webserver1010.com:5100/caspd...ct_function.htm

<{POST_SNAPBACK}>

I read this on your last URL:

VBScript GetObject Function

Returns a reference to an Automation object from a file. This is a >Windows NT only < feature.

Duh, in AutoIT-COM it works perfectly wel under Windows 98, 2000, XP and 2003. I tested it myself! I guess it would also work under Win95.

Regards,

-Sven

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...