Jump to content

AutoIt Snippets


Chimaera
 Share

Recommended Posts

2 hours ago, InternetMonkeyBoy said:

Autoit Thread

If you want a function to be a true thread with it's own PID and all then check out this super simple example that does it well...

 

 

test.exe

test.au3

This is not real thread.. it is just more processes. you may be interested in my udf to add the communication functionality.

Link to comment
Share on other sites

; This works very very well.  Low CPU.  All you have is the exe in memory for each thread.

; Send messages anyway you need.  Try the iniwrite()/iniread(), regwrite()/regread, or fancy  _NamedPipes.

#include <WinAPI.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <WinAPIShPath.au3>
#include <Misc.au3>

If $CmdLine[0] > 0 Then
    If $CmdLine[1] = "MyFuncThread1" Then MyFuncThread1()
    If $CmdLine[1] = "MyFuncThread2" Then MyFuncThread2()
    If $CmdLine[1] = "MyFuncThread3" Then MyFuncThread3()
EndIf

If @Compiled Then
    ShellExecute(@AutoItExe, "MyFuncThread1")
    ShellExecute(@AutoItExe, "MyFuncThread2")
    ShellExecute(@AutoItExe, "MyFuncThread3")
Else
    ShellExecute(@ScriptName, "MyFuncThread1")
    ShellExecute(@ScriptName, "MyFuncThread2")
    ShellExecute(@ScriptName, "MyFuncThread3")
EndIf

;*** WARNING, if your thread returns without exiting - it will respawn.  (Maybe you need that?)

Func MyFuncThread1()
    _Singleton("MyFuncThread1", 0)
    MsgBox(0, "", "Thread 1", 0)
    Exit
EndFunc   ;==>MyFuncThread1

Func MyFuncThread2()
    _Singleton("MyFuncThread2", 0)
    MsgBox(0, "", "Thread 2", 0)
    Exit
EndFunc   ;==>MyFuncThread2

Func MyFuncThread3()
    _Singleton("MyFuncThread3", 0)
    MsgBox(0, "", "Thread 3", 0)
    Exit
EndFunc   ;==>MyFuncThread3

Oh, I forgot to post the code so here it is...

Link to comment
Share on other sites

Base64 Snippets:

; #FUNCTION# ====================================================================================================================
; Name ..........: _Base64_Encode
; Description ...: Encode the $vData in Base64
; Syntax ........: _Base64_Encode($vData)
; Parameters ....: $vData               - $vData to Encode.
; Return values .: Success: Base64 encoded $vData in the form of a string.
;                  Failure: False and @error set to:
;                           1 - If "error calculating the length of the buffer needed"
;                           2 - If "error encoding"
; Author ........: trancexx
; Modified ......: Damon Harris (TheDcoder)
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Base64_Encode($vData)
    $vData = Binary($vData)
    Local $tByteStruct = DllStructCreate("byte[" & BinaryLen($vData) & "]")
    DllStructSetData($tByteStruct, 1, $vData)
    Local $tIntStruct = DllStructCreate("int")
    Local $aDllCall = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _
            "ptr", DllStructGetPtr($tByteStruct), _
            "int", DllStructGetSize($tByteStruct), _
            "int", 1, _
            "ptr", 0, _
            "ptr", DllStructGetPtr($tIntStruct))
    If @error Or Not $aDllCall[0] Then
        Return SetError(1, 0, False) ; error calculating the length of the buffer needed
    EndIf
    Local $tCharStruct = DllStructCreate("char[" & DllStructGetData($tIntStruct, 1) & "]")
    $aDllCall = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _
            "ptr", DllStructGetPtr($tByteStruct), _
            "int", DllStructGetSize($tByteStruct), _
            "int", 1, _
            "ptr", DllStructGetPtr($tCharStruct), _
            "ptr", DllStructGetPtr($tIntStruct))
    If @error Or Not $aDllCall[0] Then
        Return SetError(2, 0, False) ; error encoding
    EndIf
    Return DllStructGetData($tCharStruct, 1)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _Base64_Decode
; Description ...: Decode a base64 string
; Syntax ........: _Base64_Decode($sBase64)
; Parameters ....: $sBase64             - $sBase64 to decode.
; Return values .: Success: Decode data in the form of binary
;                  Failure: False & @error set to:
;                           1 - If "error calculating the length of the buffer needed"
;                           2 - If "error decoding"
; Author ........: trancexx
; Modified ......: Damon Harris (TheDcoder)
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Base64_Decode($sBase64)
    Local $tIntStruct = DllStructCreate("int")
    $aDllCall = DllCall("Crypt32.dll", "int", "CryptStringToBinary", _
            "str", $sBase64, _
            "int", 0, _
            "int", 1, _
            "ptr", 0, _
            "ptr", DllStructGetPtr($tIntStruct, 1), _
            "ptr", 0, _
            "ptr", 0)
    If @error Or Not $aDllCall[0] Then
        Return SetError(1, 0, False) ; error calculating the length of the buffer needed
    EndIf
    Local $tByteStruct = DllStructCreate("byte[" & DllStructGetData($tIntStruct, 1) & "]")
    $aDllCall = DllCall("Crypt32.dll", "int", "CryptStringToBinary", _
            "str", $sBase64, _
            "int", 0, _
            "int", 1, _
            "ptr", DllStructGetPtr($tByteStruct), _
            "ptr", DllStructGetPtr($tIntStruct, 1), _
            "ptr", 0, _
            "ptr", 0)
    If @error Or Not $aDllCall[0] Then
        Return SetError(2, 0, False); error decoding
    EndIf
    Return DllStructGetData($tByteStruct, 1)
EndFunc

I don't take credit, @trancexx made it, I just polished the functions, enjoy!

 

TD :D

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

; Hours, Minutes, Seconds To Milliseconds
Func _HMS_To_MS($H = 0, $M = 0, $S = 0)
    Local $Rtn = 0
    If $S > 0 Then
        $Rtn += $S * 1000
    EndIf

    If $M > 0 Then
        $Rtn += $M * (1000 * 60)
    EndIf

    If $H > 0 Then
        $Rtn += $H * ((1000 * 60) * 60)
    EndIf
    Return $Rtn
EndFunc

 

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

Monkey's are, like, natures humans.

Link to comment
Share on other sites

8 hours ago, JohnOne said:
; Hours, Minutes, Seconds To Milliseconds
Func _HMS_To_MS($H = 0, $M = 0, $S = 0)
    Local $Rtn = 0
    If $S > 0 Then
        $Rtn += $S * 1000
    EndIf

    If $M > 0 Then
        $Rtn += $M * (1000 * 60)
    EndIf

    If $H > 0 Then
        $Rtn += $H * ((1000 * 60) * 60)
    EndIf
    Return $Rtn
EndFunc

 

Or in a simpler form...

; Hours, Minutes, Seconds To Milliseconds
Func _HMS_To_MS($H = 0, $M = 0, $S = 0)
    Return $H * 3600000 + $M * 60000 + $S * 1000
EndFunc   ;==>_HMS_To_MS

... & the return trip...

; Milliseconds To Hours:Minutes:Seconds
Func _MS_TO_HHMMSS($iMs)
    $iMs /= 1000
    Return StringFormat("%02d:%02d:%02d", Floor($iMs / 3600), Mod(Floor($iMs / 60), 60), Mod($iMs, 60))
EndFunc   ;==>_MS_TO_HHMMSS

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Subclass labels to make them link with their corresponding checkbox. Ran into an issue creating a picture background with some checkboxes on it and the checkbox label wasn't transparent, but labels are. The checkbox doesn't get the highlight until you actually left click on it but I think it's a cleaner way of doing it since I can make windows send the handle of the checkbox this label goes to.

#include <GUIConstants.au3>
#include <StaticConstants.au3>
#include <WinAPIShellEx.au3>
#include <WinAPI.au3>

Global $pNewWindowProc = DllCallbackGetPtr(DllCallbackRegister("NewWindowProc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr"))
Global $hMain = GUICreate("Example", 200, 450)
Global $aCheckHandles[22]
Global $aLabelHandles[22]
Global $tRectMain = _WinAPI_GetClientRect($hMain)

For $i = 0 to 21
    $aCheckHandles[$i] = GUICtrlGetHandle(GUICtrlCreateCheckbox("", DllStructGetData($tRectMain, "right") - 20, 10 + $i * 20, 13, 13))
    $aLabelHandles[$i] = GUICtrlGetHandle(GUICtrlCreateLabel("Label " & $i, DllStructGetData($tRectMain, "right") - ($i < 10 ? 75 : 85), 10 + $i * 20, ($i < 10 ? 50 : 60), 20))
    GUICtrlSetFont(-1, 10, 600, "", "Arial")
    GUICtrlSetColor(-1, 0X1F1F1F)
Next

GUISetState(@SW_SHOW, $hMain)

For $i = 0 to 21
    _WinAPI_SetWindowSubclass($aLabelHandles[$i], $pNewWindowProc, 9999, $aCheckHandles[$i])
Next


While (True)
    Switch (GUIGetMsg())
        Case $GUI_EVENT_CLOSE
            For $i = 0 to 21
                _WinAPI_RemoveWindowSubclass($aLabelHandles[$i], $pNewWindowProc, 9999)
            Next
            GUIDelete($hMain)
            Exit 0
    EndSwitch
WEnd

Func NewWindowProc($hWnd, $iMsg, $wParam, $lParam, $uIdSubclass, $dwRefData)
    If ($iMsg = $WM_LBUTTONUP Or $iMsg = $WM_LBUTTONDOWN Or $iMsg = $WM_MOUSEMOVE) Then Return _SendMessage($dwRefData, $iMsg, $wParam, _WinAPI_MakeLong(3, 3))
    Return _WinAPI_DefSubclassProc($hWnd, $iMsg, $wParam, $lParam)
EndFunc

 

Link to comment
Share on other sites

  • 2 weeks later...

Day of the week

MsgBox(0, '', "Today is " & _DayOfTheWeek())
MsgBox(0, '', "Johann Sebastian Bach was born on " & _DayOfTheWeek(1685, 3, 31))

;  Passing a date in the form (yyyy, mm, dd) it returns the day of the week
Func _DayOfTheWeek($y = @YEAR, $m = @MON, $d = @MDAY)
    Local Static $aDaysOfWeek[7] = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    Local $fJulianDay = 367 * $y - Int(7 * ($y + Int(($m + 9) / 12)) / 4) - Int(3 * (Int(($y + Int(($m - 9) / 7)) / 100) + 1) / 4) + Int(275 * $m / 9) + $d + 1721028.5
    Local $iDOW = Mod($fJulianDay + .5, 7) ; 0 = Monday . . . 6 = Sunday
    Return $aDaysOfWeek[$iDOW]
EndFunc   ;==>_DayOfTheWeek

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Is that significantly different from _DateDayOfWeek() from Date.au3?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

6 hours ago, jchd said:

Is that significantly different from _DateDayOfWeek() from Date.au3?

Yes it is,
this one I posted here is worse and even worst it's not multilingual..... :D

jokes apart, this is just another way to have all in one line without the need to include the whole date.au3
When I came across this formula,  I liked it and I could not resist to post here this snippet, that's all.

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

  • 2 weeks later...

The most efficient and fast  way to store number in Array and then  check if the number is exists in the array.

#include <Array.au3>


Global $g_aNumbers[101]

AddNumber(0)
AddNumber(5)
AddNumber(80)
AddNumber(21)
AddNumber(100)

ConsoleWrite('Number 0: '&NumberExists(0)&@CRLF)
ConsoleWrite('Number 5: '&NumberExists(5)&@CRLF)
ConsoleWrite('Number 80: '&NumberExists(80)&@CRLF)
ConsoleWrite('Number 21: '&NumberExists(21)&@CRLF)
ConsoleWrite('Number 100: '&NumberExists(100)&@CRLF)


ConsoleWrite('Number 1: '&NumberExists(1)&@CRLF)
ConsoleWrite('Number 6: '&NumberExists(6)&@CRLF)
ConsoleWrite('Number 81: '&NumberExists(81)&@CRLF)
ConsoleWrite('Number 22: '&NumberExists(22)&@CRLF)
ConsoleWrite('Number 99: '&NumberExists(99)&@CRLF)



Func AddNumber($iNumber)
    $g_aNumbers[$iNumber] = True
EndFunc

Func RemoveNumber($iNumber)
    $g_aNumbers[$iNumber] = False
EndFunc

Func NumberExists($iNumber)
    Return $g_aNumbers[$iNumber]
EndFunc

 

Note that this technique requires that the array size will be the size of the biggest number + 1 that you may store.
It is ultra fast compared to Array search

Edited by Guest
Link to comment
Share on other sites

If you know how many there are because you have to set the ubound, and you know where they are because you can totally count.  Why would you need an array?  You have developed a rube goldberg machine for setting flags.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

I do not understand. What is the alternative? use 101 variables, each representing a number?
It does not seem right to me ..

In my case my max count is 255. (not 100) I need to know if a x number (that is <= 255 and >= 0) is exists. and I want it to be fast as possible.
The only way I thought about how to do it without loop is in this way. do you have a better idea? If so, I'd love to know.

 

Edit:
Oh, flags. I understand .. it does not appear in my head. This may also good..
But in my case, the machine flags I developing is more complex then the example. it is 3d array (it is std::vector<std::vector<std::vector<byte>>> vecRgbColorsA;) of bytes.. and it is in c++ ..

Screenshot_6.png.c65848ecd232c1976b92556

 

This is the  concept and it works.

Now if I want to check if the color 10,8,9 is added, I go to index 10 (the R value) inside the array of R.
If it's .size() is bigger then 0 then I continue to check and go into the sub array to look for 8,9 (GB)
It works
 

 

Edited by Guest
Link to comment
Share on other sites

  • 2 months later...
On Dienstag, 24. Juli 2012 at 4:11 PM, guinness said:

[translate the language code] using _GetOSLanguage (see signature) but didn't bother.

I noticed that "_Iif" has been removed since v3.3.10, so here is an updated version of above script:

#include <Misc.au3>

; Version: 1.10. AutoIt: V3.3.14.2 - replaced _Iif
; Retrieve the recommended information of the current system when posting a support question.
Local $sSystemInfo = 'For my AutoIt support question, here the details of my system:' & @CRLF & @CRLF & _
        'AutoIt  Version: V' & @AutoItVersion & _ ; ' [' & _ Iif(@AutoItX64, 'X64', 'X32') & ']' & @CRLF & _
        ' [' &( (@AutoItX64<>0) ? ('X64'):('X32') )& ']' & @CRLF & _
        'Windows Version: ' & @OSVersion & ' [' & _GetOSLanguage() & ']' & @CRLF & _
        'Language: ' & @OSLang & @CRLF & @CRLF
MsgBox(4096, 'Get this info from the clipboard with Ctrl + V.', $sSystemInfo)
ClipPut($sSystemInfo)

; #FUNCTION# ====================================================================================================================
; Name ..........: _GetOSLanguage
; Description ...: Retrieves the language of the OS, this supports 19 of the most popular languages.
; Syntax ........: _GetOSLanguage()
; Parameters ....: None
; Return values .: None
; Author ........: guinness
; Link ..........: http://www.autoitscript.com/forum/topic/131832-getoslanguage-retrieve-the-language-of-the-os/
; Example .......: No
; ===============================================================================================================================
Func _GetOSLanguage()
    Local $aString[20] = [19, "0409 0809 0c09 1009 1409 1809 1c09 2009 2409 2809 2c09 3009 3409", "0404 0804 0c04 1004 0406", "0406", "0413 0813", "0425", _
            "040b", "040c 080c 0c0c 100c 140c 180c", "0407 0807 0c07 1007 1407", "040e", "0410 0810", _
            "0411", "0414 0814", "0415", "0416 0816", "0418", _
            "0419", "081a 0c1a", "040a 080a 0c0a 100a 140a 180a 1c0a 200a 240a 280a 2c0a 300a 340a 380a 3c0a 400a 440a 480a 4c0a 500a", "041d 081d"]

    Local $aLanguage[20] = [19, "English", "Chinese", "Danish", "Dutch", "Estonian", "Finnish", "French", "German", "Hungarian", "Italian", _
            "Japanese", "Norwegian", "Polish", "Portuguese", "Romanian", "Russian", "Serbian", "Spanish", "Swedish"]
    For $i = 1 To $aString[0]
        If StringInStr($aString[$i], @OSLang) Then
            Return $aLanguage[$i]
        EndIf
    Next
    Return $aLanguage[1]
EndFunc   ;==>_GetOSLanguage

I also shortened the message to fit into the top of the MsgBox().

-HaJo

Link to comment
Share on other sites

  • 1 month later...
#include <Array.au3>

local $aSearch = FileReadToArray("urllist.txt")
local $aMatch= ["http:", ".php"]

;---urllist.txt---
;https://hello.low.com/moo.php
;http://www.hi.edu/e.php
;http://www.foo.hu
;https://www.foo.hu/o.php
;http://www.test.hu/goto.php?low.com/
;-----------------

$aResult=ArrayFlagItems($aSearch, $aMatch)
;_ArrayDisplay($aResult)
local $ff
for $x=1 to UBound($aMatch)
   $ff&="1"
Next
for $x=0 to UBound($aResult)-1
   if $aResult[$x] = $ff Then
      consolewrite($aSearch[$x] & @crlf)
   endif
Next

;---results---
;http://hi.edu/e.php
;http://test.hu/goto.php?low.com/
;-------------

; =======================================================================================
; Function 'ArrayFlagItems' (by Kattekop)
;
;  Originally made to check if a given URL list contains a given set of occurences.
;  Returns an array of each occurence over 'the whole list scanned linewise'.
;
;   e.g. your searching items array, $aSearch=
;   ["My girls hungry hordes","Hung my phone","Void mysteries"]
;   and your matching items array, $aMatch:
;   ["My","Hun"]
;
;   resulting output array $aResult: ["11","11","10"]
;   because the first item 'My' was found in 'My', 'my', and 'mysteries'.   [1x, 1x, 1x)
;   because second item 'Hun' only was found in 'hungry', and 'hung'.       (x1, x1, xx)
;   Each 'matching item' keeps its place in the resulting set.              =(11, 11, 1x)
;==============================================(grtz to my dearest: dad & x7a)===========
;
Func ArrayFlagItems(ByRef $aSearch, ByRef $aMatch)
   $e=-1
   local $aResult
    ;traverse all 'search array'
   For $x=0 To Ubound($aSearch)-1
    ;for all 'matching array items'
      For $y=0 To UBound($aMatch)-1
    ;appropriate flag (0 or 1) set on: match of a 'search array item' against 'matching array item'
         $f=StringInStr($aSearch[$x], $aMatch[$y])
         $e+=1
         If $e=UBound($aMatch) Then
            $e=0
            $aResult&=" "
         EndIf
         If $f Then $f="1"
         $aResult&=$f
      Next
   Next
   $aResult=StringSplit($aResult," ")
   _ArrayDelete($aResult,0)
Return $aResult

 

Link to comment
Share on other sites

Just now, Kattekop said:

Correction in comments of my snippet above. I spent a week and a half of coding. Comments must be:

---results---
;http://hi.edu/e.php
;http://test.hu/goto.php?low.com/
;-------------

I mean the last line must be: 'www.test.hu'. But you get my way of working with flags. I don't seem to find a way to edit my posts, Sorry.

Above is my function and application for it. It's adaptable in all circumstances.

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   1 member

×
×
  • Create New...