Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 07/10/2015 in all areas

  1. Jon

    AutoIt v3.3.14.2 Released

    AutoIt v3.3.14.0 has been released. Thanks to everyone involved, both visible and behind the scenes. Download it here. Complete list of changes: History Note The Map functionality was disabled in this release as it was not ready. I will release another beta version later with it re-enabled for continued dev and test.
    11 points
  2. Danyfirex

    libZPlay UDF

    Hi all. some time without share something. today I made this UDF because I really like this library so I wanted to share with all you. libZPlay (Win32) Version 2.02 06. May, 2010. This is multimedia library for playing mp3, mp2, mp1, ogg, flac, ac3, aac, oga, wav and pcm files and streams. New: version 2.02 can also record sound from soundcard and encode into disk file with mp3, ogg, flac, aac or wav encoder. For more information look into the LibzPlay Page.txt file into the zip file. About the libZPlay UDF supports playing mp3, ogg, AAC, AC-3, flac, wav, pcmUNICODE support for functions using strings ( filename, error messages, ID3 info, ... ) REVERSE PLAYING (you can play song backward in real time, "real" backward playing without predecoding in PCM) built in echo and reverb sound processor internal volume control (without affecting wave out device volume) pulling VU meter data (you can simply create VU meter) built in FFT spectrum graph, library will draw complete spectrum graph on your window ibzPlayExample.au3 Capture: LibzPlayAU3.zip File contents libZPlay.au3libzPlayExample.au3libZPlayform.kxf LibzPlay Page.txt License.txt- From the libZPlay SDKReadme.txt - From the libZPlay SDKChangeLog.txt - From the libZPlay SDKlibzplay.dll - From the libZPlay SDKlibzplay.chm - From the libZPlay SDKlibzplay.chw - From the libZPlay SDK Download: LibzPlayAU3.zip Saludos
    2 points
  3. I have created a UDF that will allow you to easily create a customizable GUI with either a standard progress bar or a marquee style progress bar. You can change the size and back ground color of the GUI, as well as the font, font size, and color of the text. The function doesn't require any parameters to operate, but the default text is very generic so you probably would want to set that at the very least. The function returns the handle/control id of the created GUI in the return value and the @extended macro holds the value of the control id of the progress bar so that you can update it. See update notes below for changes to the most recent version There is no functionality to close the GUI, so once you're done using it you need to GUIDelete the GUI using the return value. The default size is relatively small, only 290 x 100, which is sufficient for it's usage. The other default values are 14 for the font size, Arial for the font, the GUI background is Blue, and the label text is yellow. I created this function when I discovered that I needed a way to display something on the screen while my program was performing a lengthy operation that didn't require any screen output. I was using it to read ID3 tag information for about 4000 songs and it took some time to do that. Having a small gui with a configurable label and parameters allowed me to hide the program while informing the user that something was still going on. The only include needed to use this is the Send.au3 file that comes with AutoIt, so that the marquee style progress bar would work. I have changed the function for the marquee style progress bar to use GUICtrlSendMsg instead of _SendMessage so now it's completely stand-alone with no need for any external includes. UPDATED: 03/30/2012 I've gotten rid of most of the "magic numbers" in the script so that it's using constants for everything, and you'll have a better idea of what the code is doing. I've added the ability to set the background and label colors using the Default keyword, and -1. If you wish this GUI to use the color scheme of your Windows theme, or your skin theme, use "" as this will not set a color for the label text or the GUI background. I've limited the Y dimension of the GUI to no less than 100 pixels tall, as this would cause several issues with displaying things properly. I've updated the UDF header information so that things are a bit clearer as to how to use this. None of these changes should be script breaking, just added functionality and usage. UPDATED: 5/6/2011 Removed the need for any external includes, doesn't change the functionality in any way though so it will work as it did previously UPDATED: 5/6/2011 Updated the return values, it now returns an array on success. The array contains the handles to the GUI(array[0]), Progress Bar (array[1]), and the label (array[2]). So now you will be able to update the controls on the fly now. This version is incompatible with the previous versions because of the change. I have updated the example code below to match the newest version. Here is a short demo program showing how the 2 different progress bars would show up, and showing some of the custom features of the function. #include "_ProgressGUI.au3" $Return = _ProgressGUI("This is a test message xxxxxxxxxxxxxxx xxxxxxxxxxxxx", 1, 30, "", 400, 300);, 4, 6) For $I = 1 To 100 GUICtrlSetData($Return[1], $I) Sleep(100) If Mod($I, 2) Then GUISetBkColor(Random(0, 32767, 1), $Return[0]) Next Sleep(2000) GUIDelete($Return[0]) $Return = _ProgressGUI() Sleep(4000) And here's a link to the actual UDF Please let me know if you find it useful or have suggestions. Thanks for reading.
    1 point
  4. I posted this the other day, but thought I would post in a separate topic instead. #include <MsgBoxConstants.au3> ; ---- Start of Person Class ; Stored in the 'object' to verify it's our 'object' and not some random array Global Const $PERSON_GUID = '4197B285-6AB1-489B-8585-08C852E33F3D' ; Friendly names for 0, 1, 2 and 3 Global Enum $PERSON_AGE, $PERSON_NAME, $PERSON_ID, $PERSON_MAX ; Constructor Func Person($sName, $iAge) Local $hPerson[$PERSON_MAX] ; Set the GUID, so as the verification will work $hPerson[$PERSON_ID] = $PERSON_GUID Person_SetAge($hPerson, $iAge) Person_SetName($hPerson, $sName) ; Return the Person 'object' Return $hPerson EndFunc ;==>Person ; Getter for the age property Func Person_GetAge(ByRef $hPerson) Return _Person_IsObject($hPerson) ? $hPerson[$PERSON_AGE] : Null EndFunc ;==>Person_GetAge ; Setter for the age property Func Person_SetAge(ByRef $hPerson, $iAge) ; If not a valid 'object' or integer then return If Not _Person_IsObject($hPerson) Or Not IsInt($iAge) Then Return ; Set the age $hPerson[$PERSON_AGE] = $iAge EndFunc ;==>Person_SetAge ; Getter for the name property Func Person_GetName(ByRef $hPerson) Return _Person_IsObject($hPerson) ? $hPerson[$PERSON_NAME] : Null EndFunc ;==>Person_GetName ; Setter for the name property Func Person_SetName(ByRef $hPerson, $sName) ; If not a valid 'object' then return If Not _Person_IsObject($hPerson) Then Return ; Set the name $hPerson[$PERSON_NAME] = $sName EndFunc ;==>Person_SetName ; ToString() for the 'object' Func Person_ToString(ByRef $hPerson) Return _Person_IsObject($hPerson) ? StringFormat('Name: %s, Age: %i', $hPerson[$PERSON_NAME], $hPerson[$PERSON_AGE]) : Null EndFunc ;==>Person_ToString ; Check if it's a valid 'object' and not some random array. "NTERNAL ONLY! Func _Person_IsObject(ByRef $hPerson) Return UBound($hPerson) = $PERSON_MAX And $hPerson[$PERSON_ID] == $PERSON_GUID EndFunc ;==>_Person_IsObject ; ---- End of Person Class Example() Func Example() ; Store the Person 'object', which is just a glorified array Local $hP1 = Person('John', 30) ; Display the 'object' MsgBox($MB_SYSTEMMODAL, 'Person 1', Person_ToString($hP1)) ; Create a new person ; Store the Person 'object', which is just a glorified array Local $hP2 = Person('James', 36) ; Display the 'object' MsgBox($MB_SYSTEMMODAL, 'Person 2', Person_ToString($hP2)) ; Set the age for Person 2 Person_SetAge($hP2, 45) ; Display the 'object' MsgBox($MB_SYSTEMMODAL, 'Person 2 - Revised', Person_ToString($hP2)) EndFunc ;==>Example
    1 point
  5. Jefrey

    JSONgen: JSON generator

    Hey folks! I bring this JSON generator I made, which uses a syntax that reminds OOP a little. Here's an example: #include "JSONgen.au3" $oJson = New_Json() ; Let's add some stand-alone elements Json_AddElement($oJson, "test") ; A string Local $aArray[2] = ['hai', 'halo'] Json_AddElement($oJson, $aArray) ; An array ; Let's add some associative elements Json_AddElement($oJson, "hey", 2.55) Json_AddElement($oJson, "delete", "me") ; We will delete this one Json_AddElement($oJson, "hoo", True) Json_AddElement($oJson, "edit", "this") ; And edit this one ; Let's do some editing Json_DeleteElement($oJson, "delete") ; Deleting that one Json_EditElement($oJson, "edit", "that") ; Editing that one ; Let's now add an associated (non-associative) array :) Local $aArray[2] = ['hey', 'bye'] Json_AddElement($oJson, 'array', $aArray) ; Now we get the JSON $sTheJsonCode = Json_GetJson($oJson) MsgBox(0, "Json code", $sTheJsonCode)This will show: Includes help. License: CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/) Download: https://www.autoitscript.com/forum/files/file/345-jsongen-json-generator/
    1 point
  6. Lets assume that his example can only spawn one SciTE. In that case if I wanted a second process of SciTE it wouldn't be possible. My approach you can have that. I use the GUID as a means to ensure that if a user passes an array with the exact dimensions, that it shouldn't fail as I have a check of whether one element contains the GUID. It's not full-proof, but better than nothing.
    1 point
  7. One of FreeBasic's paradigms is object orientation.
    1 point
  8. Oh just realized it was an <a>, not a <button> lol In this case, it gets a lot easier. You can use _IELinkClickByText from IE.au3. See: #include <IE.au3> $oIE = _IECreate("http://yourpagehere.com") _IELinkClickByText($oIE, "Upload")See more (the 2nd example is quite interesting if the above refuses to work): https://www.autoitscript.com/autoit3/docs/libfunctions/_IELinkClickByText.htm
    1 point
  9. Something I am excited about with this release, is many native functions now utilise the use of constants for improved readability (you can still use magic values of course). Take for example FileGetVersion(). Doesn't that just bring tears to your eyes? I came up with the list and jpm implemented. Though don't blame jpm, blame me! Now I will grab a cup of tea, sit back and watch the comments ensue. True, my bad
    1 point
  10. Look at FileRead, or FileReadLine in the help file. Look in the help file under GUICtrlCreateButton for an example on setting up a small GUI with your two buttons. If you get stuck, post what you have (even if it is not working the way you would like it to) and we will do our best to assist.
    1 point
  11. There are already a few examples of sharing variables via memory on this forum but you may like this one. There are 2 scripts: 1. Example_Sharing_Memory_Var.au3 is the calling application (I call it the mother application) 2. Sharing_Memory_Client.au3 is the client application The mother Application is creating the structures for the different variables to be shared across applications (could be more than 2 of course) Once the structures are defined and assigned, the child application is launched bythe mother with command line parameters that contains the PID of the mother and the addresses of the pointers to the shared variables. In this example the dialogue is bi-directional: - The mother application changes background colour as requested by the child application. - The Child application GUI will move position on request of the mother. - Both GUIs will force shutdown of the other application by pressing the Stop button (I quit, you quit…) - The mother application will send any text that is entered in the input field (press enter to confirm) - The mother application can send two commands to the child application, ‘color’ and ‘quit’. Color will force a color change for both GUIs, Quit will be suicidal for the mother application. Pre-requisite: - Don’t change the name of the child application unless you change the name in the calling application The script uses Nomad memory UDF, I included it in the script, so you don't have to search for it. GreenCan Example_Sharing_Memory_Var.au3 #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.0.0 Author: GreenCan Script Function: Example_Sharing_Memory_Var.au3 Example of sharing variables between applications This script will start another application 'Sharing_Memory_Client.au3' It will pass it's PID and 3 pointers to the variables to be shared It will communicate with that application by changing the content of the variables: The child application can also change the content (background color) and can force the shutdown of this application #ce ---------------------------------------------------------------------------- #include <GUIConstantsEx.au3> ; initial values for backround color and corner position Global $BackgroundColor = 0x01398E ; initial value Global $CornerPosition = 1, $add = True ; corner variables ; set GUI $Gui = GUICreate("Calling Application", 300, 200, 100, 100) GuiCtrlCreateLabel("Example of sharing variables across applications", 5, 10,290,20) GUICtrlSetFont (-1, 10) GUICtrlSetColor ( -1, 0xFFFFFF) $LabelColor = GuiCtrlCreateLabel("Color: " & $BackgroundColor, 5, 35,290,40) GUICtrlSetColor ( $LabelColor, 0xFFFFFF) GUICtrlSetFont (-1, 10) $LastMessage = GuiCtrlCreateLabel("Type something and press enter", 5, 80,290,60) GUICtrlSetColor ( -1, 0xFFFFFF) GUICtrlSetFont (-1, 10) $Input = GUICtrlCreateInput("", 5, 140, 290, 21) GUICtrlSetTip ( -1 , "You can type anything but only 2 commands will be understood by the client:" & @CR & " Type 'Color' to force a color change" & @CR & " Type 'Quit' to force the client to quit" ) Local $ok = GUICtrlCreateButton("Stop", 5, 170, 70,22 ) Local $Corner = GUICtrlCreateButton("Change Corner", 195, 170, 100,22 ) ; Create the structure $struct = "int color;int position;char var[128]" $Structure=DllStructCreate($struct) if @error Then MsgBox(0,"","Error in DllStructCreate " & @error); exit endif DllStructSetData($Structure,"color",$BackgroundColor) ; sets the value of color at $BackgroundColor DllStructSetData($Structure,"position",$CornerPosition) ; sets the value of corner position ; get the pointers $Pointer1 = DllStructGetPtr($Structure,"color") ; Returns the pointer to the color $Pointer2 = DllStructGetPtr($Structure,"position") ; Returns the pointer to the position $Pointer3 = DllStructGetPtr($Structure,"var") ; Returns the pointer to the position ConsoleWrite(@AutoItPID & @CR & $Pointer1 & @CR & $Pointer2 & @CR & $Pointer3 & @CR ) ; Start the client application with 4 parameters: ; the PID of the calling program (this example) ; the 3 pointers of the shared variables If FileExists(@ScriptDir & "\" & "Sharing_Memory_Client.exe") = 1 Then $return = Run("Sharing_Memory_Client.exe " & @AutoItPID & " " & $Pointer1 & " " & $Pointer2 & " " & $Pointer3, @ScriptDir) Else If FileExists(@ScriptDir & "\" & "Sharing_Memory_Client.au3") = 1 Then $AutoItexePath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir");betaInstallDir for beta $ToRun = '"' & $AutoItexePath & '\AutoIt3.exe "' & ' "' & @ScriptDir & '\Sharing_Memory_Client.au3"' & ' "' & @AutoItPID &'" "' & $Pointer1 & '" "' & $Pointer2 & '" "' & $Pointer3 & '"' Run($ToRun) Else MsgBox(48, "Error" , "'Sharing_Memory_Client.au3' not found" & @CR & "Both applications should be in the same folder.") Exit EndIf EndIf $OldValue = 0 GUISetState() While 1 $msg = GUIGetMsg() Select Case $msg = $Input GUICtrlSetData ($LastMessage, "Sent: " & GUICtrlRead($Input)) ; set the string into the memory DllStructSetData($Structure,"var", GUICtrlRead($Input)) GUICtrlSetData ( $Input, "") Case $msg = $GUI_EVENT_CLOSE DllStructSetData($Structure,"color",0) ; exit value for child application ;Exit Case $msg = $ok DllStructSetData($Structure,"color",0) ; exit value for child application ;Exit Case $msg = $Corner ; change position 1, 2 and 3 If $CornerPosition = 3 Then $add = False ElseIf $CornerPosition = 1 Then $add = True EndIf If $add = True Then $CornerPosition += 1 Else $CornerPosition -= 1 EndIf DllStructSetData($Structure,"position",$CornerPosition) ; exit value for child application EndSelect $NewValue = DllStructGetData($Structure,"color") ; Pick up the value from the shared memory $NewPosition = DllStructGetData($Structure,"position") ; Pick up the value from the shared memory ;ConsoleWrite(Hex($NewValue) & " " & $NewPosition & @CR) If $NewValue = -1 Then ; Shutdown request from child GUICtrlSetData ( $LabelColor,"Exit value: " & $NewValue & @CR & "Child application requests Shutdown") Beep(3000,150) sleep(3000) Exit EndIf If $OldValue <> $NewValue Then ; change color $BackgroundColor = $NewValue GUISetBkColor( $BackgroundColor, $Gui) GUICtrlSetData ( $LabelColor, "Color: 0x" & Hex($BackgroundColor) ) $OldValue = $NewValue EndIf WEnd Sharing_Memory_Client.au3 #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.0.0 Author: GreenCan Script Function: Sharing_Memory_Client.au3 Child application started by Example_Sharing_Memory_Var.au3 Pre-requisite: 1. Do NOT change the name of the script as it is started by Example_Sharing_Memory_Var.au3 This is the child application that is started by the mother application The child receives the PID and the pointers so it can access the variables Credits: Thanks to following contributors - This source includes Memory UDF library by Nomad #ce ---------------------------------------------------------------------------- ; #include "memory2.au3" #include <GUIConstantsEx.au3> If $CmdLine[0] <> 4 Then Exit ; require PID, pointer1 and pointer2 ; set GUI $GuiWidth = 300 $GuiHeight = 300 $Gui = GUICreate("Child Application", $GuiWidth, $GuiHeight, @DesktopWidth - $GuiWidth - 100, @DesktopHeight - $GuiHeight - 100) Global $PID = $CmdLine[1] Global $Pointer1 = $CmdLine[2] Global $Pointer2 = $CmdLine[3] Global $Pointer3 = $CmdLine[4] ; $Pointer1 = initial value of color passed by the mother application ; the child application (this script) will also use $Pointer1 to communicate that it's stops (by setting $Pointer1 to -1 ) $Color = Read_Memory($PID, $Pointer1) ; $Pointer2 = initial value of corner position passed by the mother application $Position = Read_Memory($PID, $Pointer2) ; initial value of passed char String $Char = "" GuiCtrlCreateLabel("Child application" , 5, 5,$GuiWidth - 10,20) GUICtrlSetColor ( -1, 0xFFFFFF) GUICtrlSetFont (-1, 10) Local $LabelColor = GuiCtrlCreateLabel("The Application will stop when the mother application Quits", 5, 30,$GuiWidth - 10,50) GUICtrlSetColor ( $LabelColor, 0xFFFFFF) GUICtrlSetFont (-1, 10) Local $GUIEdit=GUICtrlCreateEdit ( "" , 5, $GuiHeight - 235, $GuiWidth - 10, 195) Local $Stop = GUICtrlCreateButton("Stop", ($GuiWidth/2) - 35, $GuiHeight -30, 70,22 ) GUISetState() ; show the pointers GUICtrlSetData ( $GUIEdit, "Pointer address 1: " & $Pointer1 & @CRLF & " address 2: " & $Pointer2 & @CRLF & " address 3: " & $Pointer3 & @CRLF , "append") Color() While 1 $msg = GUIGetMsg() If $msg = $Stop Then Write_Memory($PID, $Pointer1, -1) ; communicate the exit value to the mother application Exit EndIf ; if the mother application is closed, the the value will be set to -1 and the child will close too $exitValue = Read_Memory($PID, $Pointer1, "int") If $exitValue = 0 Then Quit() Else ; the mother application requests the child application to change corner if the value of $Pointer2 changes $newPosition = Read_Memory($PID, $Pointer2) If $Position <> $newPosition Then GUICtrlSetData ( $GUIEdit, "Request from mother to change corner to: " & $newPosition & @CRLF , "append" ) $Position = $newPosition If $Position = 1 Then ; Right Lower corner WinMove($Gui, "", @DesktopWidth - $GuiWidth - 100, @DesktopHeight - $GuiHeight - 100) ElseIf $Position = 2 Then ; Left Lower corner WinMove($Gui, "", 100, @DesktopHeight - $GuiHeight - 100) ElseIf $Position = 3 Then ; Right Upper corner WinMove($Gui, "", @DesktopWidth - $GuiWidth - 100, 100) EndIf EndIf ; the mother application sents a character string $Char = Read_Memory($PID, $Pointer3, "char var4[128]") If $Char <> "" Then Write_Memory($PID, $Pointer3, "", "char var4[128]") ; empty value in memory If StringUpper(StringStripWS ($Char,3)) = "QUIT" Then GUICtrlSetData ( $GUIEdit, "Received command" & ": " & $Char & @CRLF , "append" ) Quit() ElseIf StringUpper(StringStripWS ($Char,3)) = "COLOR" Then GUICtrlSetData ( $GUIEdit, "Received command" & ": " & $Char & @CRLF , "append" ) Color() Else GUICtrlSetData ( $GUIEdit, "Received" & ": " & $Char & @CRLF , "append" ) ;TrayTip("text", $Char, 5) EndIf EndIf ; change background color and sent to the mother application $randomizer = Random(1,600,1) If $randomizer = 1 Then Color() EndIf WEnd #FUNCTION# ============================================================== Func Color() $Color = Random(1,30000000,1) ; new background color GUISetBkColor( $Color, $Gui) Write_Memory($PID, $Pointer1, $Color, "int") ; set the new value to be returned to the mother application GUICtrlSetData ( $GUIEdit, "Sent Color Change: 0x" & Hex($Color) & @CRLF , "append" ) EndFunc ;==>Color #FUNCTION# ============================================================== Func Quit() GUICtrlSetData ( $GUIEdit, "Mother application requests Shutdown" & @CRLF & @CRLF & "Shutdown within 3 seconds", "append") Beep(4000,150) sleep(3000) ; wait 3 secs before closing ; You ask me to quit but you Quit too... Write_Memory($PID, $Pointer1, -1) ; communicate the exit value to the mother application Exit EndFunc ;==>Quit #FUNCTION# ============================================================== Func Write_Memory($_PID, $_Pointer, $_Value, $_VarType = "int") ; This function will replace a value in memory, allocated to the calling application Local $DllHandle = _MemoryOpen($_PID) ; Open the memory allocated by the PID from the calling application Local $Data = _MemoryWrite($_Pointer, $DllHandle, $_Value, $_VarType) ; Write the new value at the Pointer address $error = @Error ; just check if any error writing the memory If $error > 1 Then beep(1000,1000) ; just ring the bell if unable to write _MemoryClose($DllHandle) ; Close the Handle EndFunc ;==>Write_Memory #FUNCTION# ============================================================== Func Read_Memory($_PID, $_Pointer, $_VarType = "int") ; This function will read a value in memory, allocated by the calling application ToolbarAG.exe Local $DllHandle = _MemoryOpen($_PID) ; Open the memory allocated by the PID from the calling application Local $Data = _MemoryRead($_Pointer, $DllHandle , $_VarType) ; read value passed by the mother application $error = @Error ; just check if any error reading the memory _MemoryClose($DllHandle) ; Close the Handle If $error > 1 Then Return "" Else Return $Data EndIf EndFunc ;==>Read_Memory #FUNCTION# ============================================================== #include-once #region _Memory ;================================================================================================= ; AutoIt Version: 3.1.127 (beta) ; Language: English ; Platform: All Windows ; Author: Nomad ; Requirements: These functions will only work with beta. ;================================================================================================= ; Credits: wOuter - These functions are based on his original _Mem() functions. But they are ; easier to comprehend and more reliable. These functions are in no way a direct copy ; of his functions. His functions only provided a foundation from which these evolved. ;================================================================================================= ; ; Functions: ; ;================================================================================================= ; Function: _MemoryOpen($iv_Pid[, $iv_DesiredAccess[, $iv_InheritHandle]]) ; Description: Opens a process and enables all possible access rights to the process. The ; Process ID of the process is used to specify which process to open. You must ; call this function before calling _MemoryClose(), _MemoryRead(), or _MemoryWrite(). ; Parameter(s): $iv_Pid - The Process ID of the program you want to open. ; $iv_DesiredAccess - (optional) Set to 0x1F0FFF by default, which enables all ; possible access rights to the process specified by the ; Process ID. ; $if_InheritHandle - (optional) If this value is TRUE, all processes created by ; this process will inherit the access handle. Set to TRUE ; (1) by default. Set to 0 if you want it to be FALSE. ; Requirement(s): A valid process ID. ; Return Value(s): On Success - Returns an array containing the Dll handle and an open handle to ; the specified process. ; On Failure - Returns 0 ; @Error - 0 = No error. ; 1 = Invalid $iv_Pid. ; 2 = Failed to open Kernel32.dll. ; 3 = Failed to open the specified process. ; Author(s): Nomad ; Note(s): ;================================================================================================= Func _MemoryOpen($iv_Pid, $iv_DesiredAccess = 0x1F0FFF, $if_InheritHandle = 1) If Not ProcessExists($iv_Pid) Then SetError(1) Return 0 EndIf Local $ah_Handle[2] = [DllOpen('kernel32.dll')] If @Error Then SetError(2) Return 0 EndIf Local $av_OpenProcess = DllCall($ah_Handle[0], 'int', 'OpenProcess', 'int', $iv_DesiredAccess, 'int', $if_InheritHandle, 'int', $iv_Pid) If @Error Then DllClose($ah_Handle[0]) SetError(3) Return 0 EndIf $ah_Handle[1] = $av_OpenProcess[0] Return $ah_Handle EndFunc ;================================================================================================= ; Function: _MemoryRead($iv_Address, $ah_Handle[, $sv_Type]) ; Description: Reads the value located in the memory address specified. ; Parameter(s): $iv_Address - The memory address you want to read from. It must be in hex ; format (0x00000000). ; $ah_Handle - An array containing the Dll handle and the handle of the open ; process as returned by _MemoryOpen(). ; $sv_Type - (optional) The "Type" of value you intend to read. This is set to ; 'dword'(32bit(4byte) signed integer) by default. See the help file ; for DllStructCreate for all types. ; An example: If you want to read a word that is 15 characters in ; length, you would use 'char[16]'. ; Requirement(s): The $ah_Handle returned from _MemoryOpen. ; Return Value(s): On Success - Returns the value located at the specified address. ; On Failure - Returns 0 ; @Error - 0 = No error. ; 1 = Invalid $ah_Handle. ; 2 = $sv_Type was not a string. ; 3 = $sv_Type is an unknown data type. ; 4 = Failed to allocate the memory needed for the DllStructure. ; 5 = Error allocating memory for $sv_Type. ; 6 = Failed to read from the specified process. ; Author(s): Nomad ; Note(s): Values returned are in Decimal format, unless specified as a 'char' type, then ; they are returned in ASCII format. Also note that size ('char[size]') for all ; 'char' types should be 1 greater than the actual size. ;================================================================================================= Func _MemoryRead($iv_Address, $ah_Handle, $sv_Type = 'dword') If Not IsArray($ah_Handle) Then SetError(1) Return 0 EndIf Local $v_Buffer = DllStructCreate($sv_Type) If @Error Then SetError(@Error + 1) Return 0 EndIf DllCall($ah_Handle[0], 'int', 'ReadProcessMemory', 'int', $ah_Handle[1], 'int', $iv_Address, 'ptr', DllStructGetPtr($v_Buffer), 'int', DllStructGetSize($v_Buffer), 'int', '') If Not @Error Then Local $v_Value = DllStructGetData($v_Buffer, 1) Return $v_Value Else SetError(6) Return 0 EndIf EndFunc ;================================================================================================= ; Function: _MemoryWrite($iv_Address, $ah_Handle, $v_Data[, $sv_Type]) ; Description: Writes data to the specified memory address. ; Parameter(s): $iv_Address - The memory address you want to write to. It must be in hex ; format (0x00000000). ; $ah_Handle - An array containing the Dll handle and the handle of the open ; process as returned by _MemoryOpen(). ; $v_Data - The data to be written. ; $sv_Type - (optional) The "Type" of value you intend to write. This is set to ; 'dword'(32bit(4byte) signed integer) by default. See the help file ; for DllStructCreate for all types. ; An example: If you want to write a word that is 15 characters in ; length, you would use 'char[16]'. ; Requirement(s): The $ah_Handle returned from _MemoryOpen. ; Return Value(s): On Success - Returns 1 ; On Failure - Returns 0 ; @Error - 0 = No error. ; 1 = Invalid $ah_Handle. ; 2 = $sv_Type was not a string. ; 3 = $sv_Type is an unknown data type. ; 4 = Failed to allocate the memory needed for the DllStructure. ; 5 = Error allocating memory for $sv_Type. ; 6 = $v_Data is not in the proper format to be used with the "Type" ; selected for $sv_Type, or it is out of range. ; 7 = Failed to write to the specified process. ; Author(s): Nomad ; Note(s): Values sent must be in Decimal format, unless specified as a 'char' type, then ; they must be in ASCII format. Also note that size ('char[size]') for all ; 'char' types should be 1 greater than the actual size. ;================================================================================================= Func _MemoryWrite($iv_Address, $ah_Handle, $v_Data, $sv_Type = 'dword') If Not IsArray($ah_Handle) Then SetError(1) Return 0 EndIf Local $v_Buffer = DllStructCreate($sv_Type) If @Error Then SetError(@Error + 1) Return 0 Else DllStructSetData($v_Buffer, 1, $v_Data) If @Error Then SetError(6) Return 0 EndIf EndIf DllCall($ah_Handle[0], 'int', 'WriteProcessMemory', 'int', $ah_Handle[1], 'int', $iv_Address, 'ptr', DllStructGetPtr($v_Buffer), 'int', DllStructGetSize($v_Buffer), 'int', '') If Not @Error Then Return 1 Else SetError(7) Return 0 EndIf EndFunc ;================================================================================================= ; Function: _MemoryClose($ah_Handle) ; Description: Closes the process handle opened by using _MemoryOpen(). ; Parameter(s): $ah_Handle - An array containing the Dll handle and the handle of the open ; process as returned by _MemoryOpen(). ; Requirement(s): The $ah_Handle returned from _MemoryOpen. ; Return Value(s): On Success - Returns 1 ; On Failure - Returns 0 ; @Error - 0 = No error. ; 1 = Invalid $ah_Handle. ; 2 = Unable to close the process handle. ; Author(s): Nomad ; Note(s): ;================================================================================================= Func _MemoryClose($ah_Handle) If Not IsArray($ah_Handle) Then SetError(1) Return 0 EndIf DllCall($ah_Handle[0], 'int', 'CloseHandle', 'int', $ah_Handle[1]) If Not @Error Then DllClose($ah_Handle[0]) Return 1 Else DllClose($ah_Handle[0]) SetError(2) Return 0 EndIf EndFunc ;================================================================================================= ; Function: _MemoryPointerRead ($iv_Address, $ah_Handle, $av_Offset[, $sv_Type]) ; Description: Reads a chain of pointers and returns an array containing the destination ; address and the data at the address. ; Parameter(s): $iv_Address - The static memory address you want to start at. It must be in ; hex format (0x00000000). ; $ah_Handle - An array containing the Dll handle and the handle of the open ; process as returned by _MemoryOpen(). ; $av_Offset - An array of offsets for the pointers. Each pointer must have an ; offset. If there is no offset for a pointer, enter 0 for that ; array dimension. ; $sv_Type - (optional) The "Type" of data you intend to read at the destination ; address. This is set to 'dword'(32bit(4byte) signed integer) by ; default. See the help file for DllStructCreate for all types. ; Requirement(s): The $ah_Handle returned from _MemoryOpen. ; Return Value(s): On Success - Returns an array containing the destination address and the value ; located at the address. ; On Failure - Returns 0 ; @Error - 0 = No error. ; 1 = $av_Offset is not an array. ; 2 = Invalid $ah_Handle. ; 3 = $sv_Type is not a string. ; 4 = $sv_Type is an unknown data type. ; 5 = Failed to allocate the memory needed for the DllStructure. ; 6 = Error allocating memory for $sv_Type. ; 7 = Failed to read from the specified process. ; Author(s): Nomad ; Note(s): Values returned are in Decimal format, unless a 'char' type is selected. ; Set $av_Offset like this: ; $av_Offset[0] = NULL (not used) ; $av_Offset[1] = Offset for pointer 1 (all offsets must be in Decimal) ; $av_Offset[2] = Offset for pointer 2 ; etc... ; (The number of array dimensions determines the number of pointers) ;================================================================================================= Func _MemoryPointerRead ($iv_Address, $ah_Handle, $av_Offset, $sv_Type = 'dword') If IsArray($av_Offset) Then If IsArray($ah_Handle) Then Local $iv_PointerCount = UBound($av_Offset) - 1 Else SetError(2) Return 0 EndIf Else SetError(1) Return 0 EndIf Local $iv_Data[2], $i Local $v_Buffer = DllStructCreate('dword') For $i = 0 to $iv_PointerCount If $i = $iv_PointerCount Then $v_Buffer = DllStructCreate($sv_Type) If @Error Then SetError(@Error + 2) Return 0 EndIf $iv_Address = '0x' & hex($iv_Data[1] + $av_Offset[$i]) DllCall($ah_Handle[0], 'int', 'ReadProcessMemory', 'int', $ah_Handle[1], 'int', $iv_Address, 'ptr', DllStructGetPtr($v_Buffer), 'int', DllStructGetSize($v_Buffer), 'int', '') If @Error Then SetError(7) Return 0 EndIf $iv_Data[1] = DllStructGetData($v_Buffer, 1) ElseIf $i = 0 Then DllCall($ah_Handle[0], 'int', 'ReadProcessMemory', 'int', $ah_Handle[1], 'int', $iv_Address, 'ptr', DllStructGetPtr($v_Buffer), 'int', DllStructGetSize($v_Buffer), 'int', '') If @Error Then SetError(7) Return 0 EndIf $iv_Data[1] = DllStructGetData($v_Buffer, 1) Else $iv_Address = '0x' & hex($iv_Data[1] + $av_Offset[$i]) DllCall($ah_Handle[0], 'int', 'ReadProcessMemory', 'int', $ah_Handle[1], 'int', $iv_Address, 'ptr', DllStructGetPtr($v_Buffer), 'int', DllStructGetSize($v_Buffer), 'int', '') If @Error Then SetError(7) Return 0 EndIf $iv_Data[1] = DllStructGetData($v_Buffer, 1) EndIf Next $iv_Data[0] = $iv_Address Return $iv_Data EndFunc ;================================================================================================= ; Function: _MemoryPointerWrite ($iv_Address, $ah_Handle, $av_Offset, $v_Data[, $sv_Type]) ; Description: Reads a chain of pointers and writes the data to the destination address. ; Parameter(s): $iv_Address - The static memory address you want to start at. It must be in ; hex format (0x00000000). ; $ah_Handle - An array containing the Dll handle and the handle of the open ; process as returned by _MemoryOpen(). ; $av_Offset - An array of offsets for the pointers. Each pointer must have an ; offset. If there is no offset for a pointer, enter 0 for that ; array dimension. ; $v_Data - The data to be written. ; $sv_Type - (optional) The "Type" of data you intend to write at the destination ; address. This is set to 'dword'(32bit(4byte) signed integer) by ; default. See the help file for DllStructCreate for all types. ; Requirement(s): The $ah_Handle returned from _MemoryOpen. ; Return Value(s): On Success - Returns the destination address. ; On Failure - Returns 0. ; @Error - 0 = No error. ; 1 = $av_Offset is not an array. ; 2 = Invalid $ah_Handle. ; 3 = Failed to read from the specified process. ; 4 = $sv_Type is not a string. ; 5 = $sv_Type is an unknown data type. ; 6 = Failed to allocate the memory needed for the DllStructure. ; 7 = Error allocating memory for $sv_Type. ; 8 = $v_Data is not in the proper format to be used with the ; "Type" selected for $sv_Type, or it is out of range. ; 9 = Failed to write to the specified process. ; Author(s): Nomad ; Note(s): Data written is in Decimal format, unless a 'char' type is selected. ; Set $av_Offset like this: ; $av_Offset[0] = NULL (not used, doesn't matter what's entered) ; $av_Offset[1] = Offset for pointer 1 (all offsets must be in Decimal) ; $av_Offset[2] = Offset for pointer 2 ; etc... ; (The number of array dimensions determines the number of pointers) ;================================================================================================= Func _MemoryPointerWrite ($iv_Address, $ah_Handle, $av_Offset, $v_Data, $sv_Type = 'dword') If IsArray($av_Offset) Then If IsArray($ah_Handle) Then Local $iv_PointerCount = UBound($av_Offset) - 1 Else SetError(2) Return 0 EndIf Else SetError(1) Return 0 EndIf Local $iv_StructData, $i Local $v_Buffer = DllStructCreate('dword') For $i = 0 to $iv_PointerCount If $i = $iv_PointerCount Then $v_Buffer = DllStructCreate($sv_Type) If @Error Then SetError(@Error + 3) Return 0 EndIf DllStructSetData($v_Buffer, 1, $v_Data) If @Error Then SetError(8) Return 0 EndIf $iv_Address = '0x' & hex($iv_StructData + $av_Offset[$i]) DllCall($ah_Handle[0], 'int', 'WriteProcessMemory', 'int', $ah_Handle[1], 'int', $iv_Address, 'ptr', DllStructGetPtr($v_Buffer), 'int', DllStructGetSize($v_Buffer), 'int', '') If @Error Then SetError(9) Return 0 Else Return $iv_Address EndIf ElseIf $i = 0 Then DllCall($ah_Handle[0], 'int', 'ReadProcessMemory', 'int', $ah_Handle[1], 'int', $iv_Address, 'ptr', DllStructGetPtr($v_Buffer), 'int', DllStructGetSize($v_Buffer), 'int', '') If @Error Then SetError(3) Return 0 EndIf $iv_StructData = DllStructGetData($v_Buffer, 1) Else $iv_Address = '0x' & hex($iv_StructData + $av_Offset[$i]) DllCall($ah_Handle[0], 'int', 'ReadProcessMemory', 'int', $ah_Handle[1], 'int', $iv_Address, 'ptr', DllStructGetPtr($v_Buffer), 'int', DllStructGetSize($v_Buffer), 'int', '') If @Error Then SetError(3) Return 0 EndIf $iv_StructData = DllStructGetData($v_Buffer, 1) EndIf Next EndFunc #endregion
    1 point
  12. Valik

    Global Vars

    That's really not an API. An API would be more like this: #include <GUIConstants.au3> Opt("GUIOnEventMode", 1) Enum $GUI_HWND, $GUI_CLOSEBUTTON, $GUI_MAX Global $g_aGUI[$GUI_MAX] CreateGUI() GUISetState(@SW_SHOW, GUI_GetHWND()) WaitForClose() Func CreateGUI() GUI_SetHWnd(GUICreate("GUI")) GUI_SetCloseButton(GUICtrlCreateButton("Close", 5, 5, 50, 25)) GUICtrlSetOnEvent(GUI_GetCloseButton(), "OnClose") GUISetOnEvent($GUI_EVENT_CLOSE, "OnClose") EndFunc ; CreateGUI() Func WaitForClose() While WinExists(GUI_GetHWND()) Sleep(50) WEnd EndFunc ; WaitForClose() Func OnClose() GUIDelete($g_aGUI[$GUI_HWND]) EndFunc ; OnClose() #Region API Func GUI_SetHWND($hWnd) $g_aGUI[$GUI_HWND] = $hWnd EndFunc ; GUI_SetHWnd() Func GUI_GetHWND() Return $g_aGUI[$GUI_HWND] EndFunc ; GUI_GetHWND() Func GUI_SetCloseButton($id) $g_aGUI[$GUI_CLOSEBUTTON] = $id EndFunc ; GUI_SetCloseButton() Func GUI_GetCloseButton() Return $g_aGUI[$GUI_CLOSEBUTTON] EndFunc ; GUI_GetCloseButton() #EndRegion API The idea is to provide an interface to call into. The interface is all the user should interact with. The fact that the implementation uses a global variable is irrelevant. Here's a better example of an interface and implementation that are separate that doesn't use global variables: ; =================================================================== ; Project: DocLib Library ; Description: Functions for building the AutoIt documentation. ; Author: Jason Boggs <vampire DOT valik AT gmail DOT com> ; =================================================================== #include-once #Region Members Exported #cs Exported Functions SciTE_Create() - Creates a SciTE object that can be used with SciTE_* functions. The handle returned must be destroyed with SciTE_Destroy(). SciTE_Destroy(ByRef $vHandle) - Destroys a SciTE handle created by SciTE_Create(). SciTE_OpenFile($vHandle, $sFile) - Opens the specified file in the SciTE instance. SciTE_ExportAsHtml($vHandle, $sFile) - Exports the active file to the specified destination. SciTE_CloseFile($vHandle) - Closes the active file. SciTE_SendCommand($vHandle, $sCmd, $hResponse = 0) - Sends the specified command to the SciTE instance. _FileListToArraySorted($sPath, $sFilter = "*", $iFlag = 0) - Returns a file list in a sorted array. #ce Exported Functions #EndRegion Members Exported #Region Includes #include <Array.au3> #include <File.au3> #include <WinAPI.au3> #include <WindowsConstants.au3> #EndRegion Includes #Region Global Variables Global Enum $SCITE_MAINHWND, $SCITE_DIRECTORHWND, $SCITE_MAX #EndRegion Global Variables #Region Library Initialization #EndRegion Library Initialization #Region Public Members #Region SciTE Functions #Region SciTE_Create() ; =================================================================== ; SciTE_Create() ; ; Creates a SciTE object that can be used with SciTE_* functions. The handle returned must ; be destroyed with SciTE_Destroy(). ; Parameters: ; None. ; Returns: ; Success: A handle for use in other SciTE_* functions. ; Failure: 0, sets @error to non-zero as follows: ; 1 - SciTE could not be found. ; 2 - The required properties file could not be found. ; =================================================================== Func SciTE_Create() ; Get the ptah to the required files. Local $sSciTE = __SciTE_FindFile("SciTE.exe") Local $sPropertiesFile = __SciTE_FindFile("au3.keywords.properties") If Not $sSciTE Then Return SetError(1, 0, 0) If Not $sPropertiesFile Then Return SetError(2, 0, 0) ; The -import command requires the .properties extenstion to be stripped. $sPropertiesFile = StringTrimRight($sPropertiesFile, 11) ; Set SciTE_HOME to a directory that doesn't contain global/user properties ; files. This ensures the import statement works correctly. EnvSet("SciTE_HOME", @TempDir) ; Build the full command to start SciTE. $sSciTE = StringFormat('"%s" "-import %s" -check.if.already.open=0', $sSciTE, $sPropertiesFile) ; Run SciTE hidden and store the PID. Local $pid = Run($sSciTE) ; Build the handle to return. Local $vHandle[$SCITE_MAX] $vHandle[$SCITE_MAINHWND] = __SciTE_MainHWNDFromPID($pid) $vHandle[$SCITE_DIRECTORHWND] = __SciTE_DirectorHWNDFromPID($pid) ; Hide the SciTE window because it ignores @SW_HIDE. WinSetState($vHandle[$SCITE_MAINHWND], "", @SW_HIDE) ; Return the handle. Return $vHandle EndFunc ; SciTE_Create() #EndRegion SciTE_Create() #Region SciTE_Destroy() ; =================================================================== ; SciTE_Destroy(ByRef $vHandle) ; ; Destroys a SciTE handle created by SciTE_Create(). ; Parameters: ; $vHandle - IN/OUT - The handle to SciTE. ; Returns: ; Sets @error to non-zero if the handle was invalid. ; =================================================================== Func SciTE_Destroy(ByRef $vHandle) If Not __SciTE_IsValid($vHandle) Then Return SetError(-1, 0, 0) SciTE_SendCommand($vHandle, "quit:") $vHandle = 0 EndFunc ; SciTE_Destroy() #EndRegion SciTE_Destroy() #Region SciTE_OpenFile() ; =================================================================== ; SciTE_OpenFile($vHandle, $sFile) ; ; Opens the specified file in the SciTE instance. ; Parameters: ; $vHandle - IN - The handle to SciTE. ; $sFile - IN - The file to open. ; Returns: ; Sets @error to non-zero if the handle was invalid. ; =================================================================== Func SciTE_OpenFile($vHandle, $sFile) If Not __SciTE_IsValid($vHandle) Then Return SetError(-1, 0, 0) Return SciTE_SendCommand($vHandle, "open:" & StringReplace($sFile, "\", "\\")) EndFunc ; SciTE_OpenFile() #EndRegion SciTE_OpenFile() #Region SciTE_ExportAsHtml() ; =================================================================== ; SciTE_ExportAsHtml($vHandle, $sFile) ; ; Exports the active file to the specified destination. ; Parameters: ; $vHandle - IN - The handle to SciTE. ; $sFile - IN - The destination file to save to. ; Returns: ; Sets @error to non-zero if the handle was invalid. ; =================================================================== Func SciTE_ExportAsHtml($vHandle, $sFile) If Not __SciTE_IsValid($vHandle) Then Return SetError(-1, 0, 0) Return SciTE_SendCommand($vHandle, "exportashtml:" & StringReplace($sFile, "\", "\\")) EndFunc ; SciTE_ExportAsHtml() #EndRegion SciTE_ExportAsHtml() #Region SciTE_CloseFile() ; =================================================================== ; SciTE_CloseFile($vHandle) ; ; Closes the active file. ; Parameters: ; $vHandle - IN - The handle to SciTE. ; Returns: ; Sets @error to non-zero if the handle was invalid. ; =================================================================== Func SciTE_CloseFile($vHandle) If Not __SciTE_IsValid($vHandle) Then Return SetError(-1, 0, 0) Return SciTE_SendCommand($vHandle, "close:") EndFunc ; SciTE_CloseFile() #EndRegion SciTE_CloseFile() #Region SciTE_SendCommand() ; =================================================================== ; SciTE_SendCommand($vHandle, $sCmd, $hResponse = 0) ; ; Sends the specified command to the SciTE instance. ; Parameters: ; $vHandle - IN - The handle to SciTE. ; $sCmd - IN - The command to send. ; $hResponse - IN/OPTIONAL - The optional HWND to a window that will receive a response. ; Returns: ; Sets @error to non-zero if the handle was invalid. ; =================================================================== Func SciTE_SendCommand($vHandle, $sCmd, $hResponse = 0) If Not __SciTE_IsValid($vHandle) Then Return SetError(-1, 0, 0) ; Create a string to hold the command. Local $tCmd = DllStructCreate(StringFormat("char[%d]", StringLen($sCmd) + 1)) DllStructSetData($tCmd, 1, $sCmd) ; Create the COPYDATA structure. Local $tCopyDataStruct = DllStructCreate("ULONG_PTR dwData; DWORD cbData; ptr lpData;") DllStructSetData($tCopyDataStruct, "dwData", 1) DllStructSetData($tCopyDataStruct, "cbData", StringLen($sCmd) + 1) DllStructSetData($tCopyDataStruct, "lpData", DllStructGetPtr($tCmd)) ; Send the command. Return _SendMessageA(__SciTE_GetDirectorHWND($vHandle), $WM_COPYDATA, $hResponse, DllStructGetPtr($tCopyDataStruct), 0, "hwnd", "ptr") EndFunc ; SciTE_SendCommand() #EndRegion SciTE_SendCommand() #EndRegion SciTE Functions #Region _FileListToArraySorted() ; =================================================================== ; _FileListToArraySorted($sPath, $sFilter = "*", $iFlag = 0) ; ; Returns a file list in a sorted array. ; Parameters: ; $sPath - IN - The root path to search. ; $sFilter - IN/OPTIONAL - The file filter to search for. ; $iFlag - IN/OPTIONAL - Specifies whether to return files folders or both: ; 0 - Return both files and folders (Default). ; 1 - Return files only. ; 2 - Return Folders only ; Returns: ; Sucess: A sorted array. ; Failure: 0, sets @error to non-zero. ; =================================================================== Func _FileListToArraySorted($sPath, $sFilter = "*", $iFlag = 0) Local $aFiles = _FileListToArray($sPath, $sFilter, $iFlag) Local Const $nError = @error, $nExtended = @extended _ArraySort($aFiles, 0, 1) Return SetError($nError, $nExtended, $aFiles) EndFunc ; _FileListToArraySorted() #EndRegion _FileListToArraySorted() #EndRegion Public Members #Region Private Members #Region SciTE Private Functions #Region __SciTE_FindFile() ; =================================================================== ; __SciTE_FindFile($sFile) ; ; Searches for the specified file in the standard SciTE locations. ; Parameters: ; $sFile - IN - The filename to search for. ; Returns: ; Success - The path to the specified file. ; Failure - An empty string. ; =================================================================== Func __SciTE_FindFile($sFile) ; Look for the file relative to docs\build. Local $sLocation = "..\..\install\SciTE\" & $sFile If Not FileExists($sLocation) Then ; Look for the file in the AutoIt SciTE directory. $sLocation = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir") & "\SciTE\" & $sFile ; Return an empty string if the file was not found. If Not FileExists($sLocation) Then Return "" EndIf ; Return the location if the file was found. Return $sLocation EndFunc ; __SciTE_FindFile() #EndRegion __SciTE_FindFile() #Region __SciTE_GetMainHWND() ; =================================================================== ; __SciTE_GetMainHWND($vHandle) ; ; Retrieves the SciTE window handle. ; Parameters: ; $vHandle - IN - The handle to SciTE. ; Returns: ; Success - The SciTE window handle. ; Failure - 0, sets @error to non-zero. ; =================================================================== Func __SciTE_GetMainHWND($vHandle) If Not __SciTE_IsValid($vHandle) Then Return SetError(-1, 0, 0) Return $vHandle[$SCITE_MAINHWND] EndFunc ; __SciTE_GetMainHWND() #EndRegion __SciTE_GetMainHWND() #Region __SciTE_GetDirectorHWND() ; =================================================================== ; __SciTE_GetDirectorHWND($vHandle) ; ; Retrieves the SciTE Director window handle. ; Parameters: ; $vHandle - IN - The handle to SciTE. ; Returns: ; Success - The SciTE Director window handle. ; Failure - 0, sets @error to non-zero. ; =================================================================== Func __SciTE_GetDirectorHWND($vHandle) If Not __SciTE_IsValid($vHandle) Then Return SetError(-1, 0, 0) Return $vHandle[$SCITE_DIRECTORHWND] EndFunc ; __SciTE_GetDirectorHWND() #EndRegion __SciTE_GetDirectorHWND() #Region __SciTE_MainHWNDFromPID() ; =================================================================== ; __SciTE_MainHWNDFromPID($pid) ; ; Retrieves the SciTE window from the specified PID. ; Parameters: ; $pid - IN - The PID to get the SciTE window from. ; Returns: ; Success - The handle to the SciTE window. ; Failure - 0. ; =================================================================== Func __SciTE_MainHWNDFromPID($pid) Return __SciTE_HWNDFromPID($pid, "[CLASS:SciTEWindow]") EndFunc ; __SciTE_MainHWNDFromPID() #EndRegion __SciTE_MainHWNDFromPID() #Region __SciTE_DirectorHWNDFromPID() ; =================================================================== ; __SciTE_DirectorHWNDFromPID($pid) ; ; Retrieves the SciTE Director window from the specified PID. ; Parameters: ; $pid - IN - The PID to get the SciTE Director window from. ; Returns: ; Success - The handle to the SciTE Director window. ; Failure - 0. ; =================================================================== Func __SciTE_DirectorHWNDFromPID($pid) Return __SciTE_HWNDFromPID($pid, "[CLASS:DirectorExtension]") EndFunc ; __SciTE_DirectorHWNDFromPID() #EndRegion __SciTE_DirectorHWNDFromPID() #Region __SciTE_HWNDFromPID() ; =================================================================== ; __SciTE_HWNDFromPID($pid, $sWindowDescription, $nTimeout = 5000) ; ; Description. ; Parameters: ; $pid - IN - The PID to get the window from. ; $sWindowDescription - IN - An advanced window description of the window. ; $nTimeout - IN/OPTIONAL - How long to wait for the window before giving up. ; Returns: ; Success - The handle to the specified window. ; Failure - 0. ; =================================================================== Func __SciTE_HWNDFromPID($pid, $sWindowDescription, $nTimeout = 5000) Local $nStart = TimerInit() While TimerDiff($nStart) < $nTimeout Local $aList = WinList($sWindowDescription) For $i = 1 To UBound($aList) - 1 If WinGetProcess($aList[$i][1]) = $pid Then Return $aList[$i][1] Next Sleep(250) WEnd Return 0 EndFunc ; __SciTE_HWNDFromPID() #EndRegion __SciTE_HWNDFromPID() #Region __SciTE_IsValid() ; =================================================================== ; __SciTE_IsValid($vHandle) ; ; Tests if the specified handle is valid. ; Parameters: ; $vHandle - IN - The handle to SciTE. ; Returns: ; True - The handle is valid. ; False - The handle is not valid. ; =================================================================== Func __SciTE_IsValid($vHandle) Return UBound($vHandle) = $SCITE_MAX EndFunc ; __SciTE_IsValid() #EndRegion __SciTE_IsValid() #EndRegion SciTE Private Functions #EndRegion Private Members SciTE_Create() returns an opaque handle. The caller doesn't need to worry about what the handle really is, they just pass it to other SciTE_* functions. Even the SciTE_* functions aren't bound to an implementation. If SciTE had a COM interface I could easily change the code to use the COM interface and wouldn't have to touch a single line in any of the SciTE_* functions nor would the caller of the SciTE_* functions need to be changed. Only the __SciTE functions would change because they are the only parts of the code that deal with the implementation. A design like this is far more flexible and future-proof than a design using global variables. I have pushed all the nasty details far away from where things are used. Things are abstracted away into concepts. The caller only wants to automate SciTE, the caller doesn't care if it uses messages, window automation, a COM interface or command line arguments. The interface doesn't care about those details, either. It only exists to provide an access point between the caller and the raw implementation of the behavior.
    1 point
×
×
  • Create New...