Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 09/23/2021 in all areas

  1. In this thread it was mentioned how a software could manage single/multiple monitors. I was curious to see how it could be possible to make it work without the tool. So I made a script that change brightness (VCP 10) of my primary monitor for a few seconds. To learn about the VCP commands refer to : https://www.ddcutil.com/vcpinfo_output/ Among other commands, you can turn on/off (VCP D6) secondary monitor, change contrast (VCP 12), etc. In the example you can see (and parse) all commands allowed by your monitor(s). #include <String.au3> #include <Array.au3> #include <WinAPIGdi.au3> #include <WinAPISysWin.au3> Local $aPos, $aData = _WinAPI_EnumDisplayMonitors() If IsArray($aData) Then ReDim $aData[$aData[0][0] + 1][5] For $i = 1 To $aData[0][0] $aPos = _WinAPI_GetPosFromRect($aData[$i][1]) For $j = 0 To 3 $aData[$i][$j + 1] = $aPos[$j] Next Next EndIf _ArrayDisplay($aData, '_WinAPI_EnumDisplayMonitors') Local $hWnd = _WinAPI_GetDesktopWindow() ConsoleWrite($hWnd & @CRLF) $hMonitor = _WinAPI_MonitorFromWindow($hWnd, $MONITOR_DEFAULTTONEAREST) ConsoleWrite($hMonitor & @CRLF) Local $aRet = DllCall("Dxva2.dll", "int", "GetNumberOfPhysicalMonitorsFromHMONITOR", "handle", $hMonitor, "int*", 0) ;_ArrayDisplay($aRet) Local $iNumberMonitor = $aRet[2] Const $tag_PHYSICAL_MONITOR = "handle hPhysicalMonitor; wchar strPhysicalMonitorDescription[128];" Local $tPhysicalMonitor = DllStructCreate(_StringRepeat($tag_PHYSICAL_MONITOR, $iNumberMonitor)) $aRet = DllCall("Dxva2.dll", "int", "GetPhysicalMonitorsFromHMONITOR", "handle", $hMonitor, "int", $iNumberMonitor, "struct*", $tPhysicalMonitor) ;_ArrayDisplay($aRet) MsgBox($MB_SYSTEMMODAL, "", $tPhysicalMonitor.strPhysicalMonitorDescription) $aRet = DllCall("Dxva2.dll", "int", "GetCapabilitiesStringLength", "handle", $tPhysicalMonitor.hPhysicalMonitor, "int*", 0) ;_ArrayDisplay($aRet) Local $iLength = $aRet[2] + 1 Local $tCapabilities = DllStructCreate("char sCapabilities[" & $iLength & "];") $aRet = DllCall("Dxva2.dll", "int", "CapabilitiesRequestAndCapabilitiesReply", "handle", $tPhysicalMonitor.hPhysicalMonitor, _ "ptr", DllStructGetPtr($tCapabilities), "int", $iLength) ;_ArrayDisplay($aRet) ConsoleWrite($tCapabilities.sCapabilities & @CRLF) Local Enum $MC_MOMENTARY, $MC_SET_PARAMETER ; _MC_VCP_CODE_TYPE $aRet = DllCall("Dxva2.dll", "int", "GetVCPFeatureAndVCPFeatureReply", "handle", $tPhysicalMonitor.hPhysicalMonitor, _ "byte", 0x10, "int*", 0, "int*", 0, "int*", 0) ;_ArrayDisplay($aRet) ConsoleWrite($aRet[3] = $MC_MOMENTARY ? "Momentary" : ($aRet[3] = $MC_SET_PARAMETER ? "Set" : "Unknown") & @CRLF) ConsoleWrite("Current value = " & $aRet[4] & "/" & "Maximum = " & $aRet[5] & @CRLF) Local $iCurrent = $aRet[4] $aRet = DllCall("Dxva2.dll", "int", "SetVCPFeature", "handle", $tPhysicalMonitor.hPhysicalMonitor, _ "byte", 0x10, "int", 20) Sleep(5000) $aRet = DllCall("Dxva2.dll", "int", "SetVCPFeature", "handle", $tPhysicalMonitor.hPhysicalMonitor, _ "byte", 0x10, "int", $iCurrent) $aRet = DllCall("Dxva2.dll", "int", "DestroyPhysicalMonitors", "int", $iNumberMonitor, "ptr", DllStructGetPtr($tPhysicalMonitor)) ;_ArrayDisplay($aRet)
    2 points
  2. @stiv : Since you are a new member, you may be unfamiliar with what is meant by PM. PM (abbreviation for Personal or Private Message) is a feature of the forum to have a private conversation with one (or more) members. Such a conversation, as the name already indicates, does not appear as a public post, and there you can use whatever language you prefer. To start a private conversation, use the "Envelope Button" from the website header : However, you should consider the following excerpt from the forum rules : In this case it is fine, because @Danyfirex was so kind to offer you additional help. EDIT : A good translation tool for your future contributions can be found e.g. here : https://www.deepl.com/translate
    2 points
  3. The Running Object Table (ROT) is a system global table where objects can register themselves. This makes the objects accessible in processes other than the process where the object was created. IRunningObjectTable and related interfaces provides access to register and maintain objects in the ROT. ThreadsOther threads related to ROT objects: Non-blocking data display functions IPC Techniques through ROT Objects AutoIt and Perl integration through ROT objects Access AutoIt by trancexx is about executing AutoIt commands in other programming languages. The AutoIt command is executed by calling a Call method of the ROT object. The example is based on the AutoItObject UDF. Posts (2021-01-03)This is a list of the most interesting posts below: Usage section with typical examples of the use of ROT objects with the following subsections: Passing array between AutoIt programs Passing array between AutoIt and VBScript Passing data through ROT Objects Executing Object Methods through VBScript VBScript (2021-01-03)VBScript is used as a consistent programming language to demonstrate the ROT technique for communicating between AutoIt and other languages. This is a list of the VBScript posts: Passing array between AutoIt and VBScript (in middle of post) Executing Object Methods through VBScript BasicRegister an objectExamples\1) Basic\0) ROT_RegisterObject.au3 #include <Array.au3> #include "..\..\Includes\IRunningObjectTable.au3" Example() Func Example() Local $sNameId = "DataTransferObject" & ROT_CreateGUID() Local $iHandle = ROT_RegisterObject( Default, $sNameId ) ; Default => Object = Dictionary object If Not $iHandle Then Return ConsoleWrite( "ROT_RegisterObject ERR" & @CRLF ) ConsoleWrite( "ROT_RegisterObject OK" & @CRLF ) Local $aROT_List = ROT_Enumerate() _ArrayDisplay( $aROT_List, "ROT_RegisterObject" ) EndFunc Output in _ArrayDisplay looks like this: DataTransferObject-{05DCB964-FD4B-40EC-A95A-999491347D20} $sNameId (similar to ProgID) is the identification of the ROT object and is used in the ObjGet() function to access the object in this way: Local $oDataTransferObject = ObjGet( "DataTransferObject-{05DCB964-FD4B-40EC-A95A-999491347D20}" ) ROT_CreateGUID() is used to make $sNameId unique. See below. ROT_RegisterObject() is the function that registers the object in the ROT. The Dictionary object is the actual object that is registered in the ROT. The Add method allows you to add a new key/item pair to the object. The key is often a string. The item property can be any COM compatible data type which includes most AutoIt data types. An exception is the DllStruct type which must be converted to a compatible type. Because many different data types can be used in the item property and the item can be identified through a key, the Dictionary object is generally applicable for many different purposes. More information about registering objects in the ROT can be found in this Microsoft documentation. ROT_Enumerate() enumerate the objects, that are registered in the ROT. The program that registers an object in the ROT plays the role of server. One or more programs that access the ROT object with the ObjGet() function play the role of clients. The ROT object is only available as long as the server program is running or until the server program deletes the object with the ROT_Revoke() function. You can find examples of using the functions in Examples\1) Basic\. ROT objects (2020-07-11)The Dictionary object is the default object in the ROT_RegisterObject() function. The object that's passed to this function is the actual object that's registered in the ROT. The Dictionary object works fine when passing data between AutoIt scripts, between AutoIt and VBScript, and between AutoIt and Perl. However, situations may easily arise where it can be an advantage to pass data with a very simple object with just a single property. There are several options for creating such a simple object. This can be done through C# and VB.NET code, as demonstrated in Using C# and VB Code in AutoIt through .NET Framework. And it can be done through the AutoItObject UDF. Examples\4) ROT Objects\ contains examples for creating the ROT object in these three ways. Client.au3 is the same script in all examples: #include <Array.au3> Example() Func Example() Local $sNameId = "DataTransfer" Local $oDataTransfer = ObjGet( $sNameId ) Local $aArray = $oDataTransfer.Item _ArrayDisplay( $aArray ) EndFunc Create object with C# code CSharp\Server.au3: #include "..\..\..\Includes\IRunningObjectTable.au3" #include "..\DotNetAll.au3" Example() Func Example() Local $aArray[1000][10] For $i = 0 To 1000 - 1 For $j = 0 To 10 - 1 $aArray[$i][$j] = $i & "/" & $j Next Next Local $oNetCode = DotNet_LoadCScode( FileRead( "Object.cs" ), "System.dll" ) Local $oCSObject = DotNet_CreateObject( $oNetCode, "CSObjClass" ) $oCSObject.Item = $aArray Local $sNameId = "DataTransfer" ROT_RegisterObject( Ptr( $oCSObject ), $sNameId ) RunWait( @AutoItExe & " /AutoIt3ExecuteScript " & "Client.au3" ) EndFunc CSharp\Object.cs: using System; public class CSObjClass { public object Item { get; set; } } Create object with VB.NET code VB.NET\Server.au3: #include "..\..\..\Includes\IRunningObjectTable.au3" #include "..\DotNetAll.au3" Example() Func Example() Local $aArray[1000][10] For $i = 0 To 1000 - 1 For $j = 0 To 10 - 1 $aArray[$i][$j] = $i & "/" & $j Next Next Local $oNetCode = DotNet_LoadVBcode( FileRead( "Object.vb" ), "System.dll" ) Local $oVBObject = DotNet_CreateObject( $oNetCode, "VBObjClass" ) $oVBObject.Item = $aArray Local $sNameId = "DataTransfer" ROT_RegisterObject( Ptr( $oVBObject ), $sNameId ) RunWait( @AutoItExe & " /AutoIt3ExecuteScript " & "Client.au3" ) EndFunc VB.NET\Object.vb: Imports System Public Class VBObjClass Public Property Item As Object End Class In AutoIt/Perl integration, the very first attempts were performed with the VB.NET object. Check for recognition of the ROT object with this Client.pl script (copied from the AutoIt/Perl example) : use strict; use Win32::OLE; my $oObj = Win32::OLE->GetObject( "DataTransfer" ); Win32::OLE->EnumAllObjects( sub { my $Object = shift; my $Class = Win32::OLE->QueryObjectType( $Object ); printf "Object = %s, Class = %s\n", $Object, $Class; } ); Gave the following output: Object = Win32::OLE=HASH(0x72a51c), Class = _VBObjClass ; VB.NET object Object = Win32::OLE=HASH(0x71b5a8), Class = IDictionary ; Dictionary object Create object with AutoItObject AutoItObject\Server.au3: #include "..\..\..\Includes\IRunningObjectTable.au3" #include "AutoitObject.au3" Example() Func Example() Local $aArray[1000][10] For $i = 0 To 1000 - 1 For $j = 0 To 10 - 1 $aArray[$i][$j] = $i & "/" & $j Next Next _AutoItObject_StartUp() Local $oObject = NewObject(), $sNameId = "DataTransfer" ROT_RegisterObject( Ptr( $oObject ), $sNameId ) Local $oDataTransfer = ObjGet( $sNameId ) $oDataTransfer.Item = $aArray RunWait( @AutoItExe & " /AutoIt3ExecuteScript " & "Client.au3" ) _AutoItObject_Shutdown() EndFunc Func NewObject() Local $oClassObject = _AutoItObject_Class() $oClassObject.AddProperty( "Item" ) Return $oClassObject.Object EndFunc Unique objectsWhen using ROT objects in real code, there is a particular question that needs to be addressed. The question is how to make sure that $sNameId, which clients use to access the object with the ObjGet() function, actually identifies a unique object. Running a server program twice in succession can easily create two ROT objects with the same $sNameId. But how do you actually access the ROT object that was created first and the ROT object that was created last? The answer to the question is to make sure that $sNameId also becomes unique so that you can distinguish between the two objects. To that end, the ROT_CreateGUID() function is created (copy of _WinAPI_CreateGUID()). The function can ensure a unique $sNameId as shown in the example above. The problem and solution is demonstrated in Examples\1) Basic\2) Multiple ROT objects.au3. Time stampsUsing the NoteChangeTime() and GetTimeOfLastChange() methods of the IRunningObjectTable interface, you can set and get timestamps on a ROT object. But these timestamps do not seem particularly useful. Therefore, the methods are not implemented. The UDFThe IRunningObjectTable UDF, Includes\IRunningObjectTable.au3, is a very small UDF with less than 150 lines of code: #include-once ; --- Interface definitions --- Global Const $sIID_IRunningObjectTable = "{00000010-0000-0000-C000-000000000046}" Global Const $dtag_IRunningObjectTable = _ "Register hresult(dword;ptr;ptr;dword*);" & _ "Revoke hresult(dword);" & _ "IsRunning hresult(ptr);" & _ "GetObject hresult(ptr;ptr*);" & _ "NoteChangeTime hresult(dword;struct*);" & _ "GetTimeOfLastChange hresult(ptr;ptr*);" & _ "EnumRunning hresult(ptr*);" Global Const $ROTFLAGS_REGISTRATIONKEEPSALIVE = 0x1 Global Const $ROTFLAGS_ALLOWANYCLIENT = 0x2 Global Const $sIID_IEnumMoniker = "{00000102-0000-0000-C000-000000000046}" Global Const $dtag_IEnumMoniker = _ "Next hresult(ulong;ptr*;ulong*);" & _ "Skip hresult(ulong);" & _ "Reset hresult();" & _ "Clone hresult(ptr*);" Global Const $sIID_IPersist = "{0000010C-0000-0000-C000-000000000046}" Global Const $dtag_IPersist = _ "GetClassID hresult(ptr*);" Global Const $sIID_IPersistStream = "{00000109-0000-0000-C000-000000000046}" Global Const $dtag_IPersistStream = $dtag_IPersist & _ "IsDirty hresult();" & _ "Load hresult(ptr);" & _ "Save hresult(ptr;bool);" & _ "GetSizeMax hresult(uint*);" Global Const $sIID_IMoniker = "{0000000F-0000-0000-C000-000000000046}" Global Const $dtag_IMoniker = $dtag_IPersistStream & _ "BindToObject hresult();" & _ "BindToStorage hresult();" & _ "Reduce hresult();" & _ "ComposeWith hresult();" & _ "Enum hresult();" & _ "IsEqual hresult();" & _ "Hash hresult();" & _ "IsRunning hresult(ptr;ptr;ptr);" & _ "GetTimeOfLastChange hresult(ptr;ptr;ptr);" & _ "Inverse hresult();" & _ "CommonPrefixWith hresult();" & _ "RelativePathTo hresult();" & _ "GetDisplayName hresult(ptr;ptr;wstr*);" & _ "ParseDisplayName hresult();" & _ "IsSystemMoniker hresult(ptr*);" ; --- Windows API functions --- Func ROT_GetRunningObjectTable() Return DllCall( "Ole32.dll", "long", "GetRunningObjectTable", "dword", 0, "ptr*", 0 )[2] EndFunc Func ROT_CreateFileMoniker( $sNameId ) Return DllCall( "Ole32.dll", "long", "CreateFileMoniker", "wstr", $sNameId, "ptr*", 0 )[2] EndFunc Func ROT_CreateBindCtx() Return DllCall( "Ole32.dll", "long", "CreateBindCtx", "dword", 0, "ptr*", 0 )[2] EndFunc ; --- UDF functions --- ; Failure: Returns 0 ; Success: Returns $iHandle ; Registers an object and its identifying moniker in the ROT Func ROT_RegisterObject( $pObject, $sNameId, $iFlags = $ROTFLAGS_REGISTRATIONKEEPSALIVE ) If $pObject = Default Then Local $oDict = ObjCreate( "Scripting.Dictionary" ) $pObject = Ptr( $oDict ) EndIf If Not $pObject Or Not $sNameId Then Return 0 Local $oRunningObjectTable = ObjCreateInterface( ROT_GetRunningObjectTable(), $sIID_IRunningObjectTable, $dtag_IRunningObjectTable ) If Not IsObj( $oRunningObjectTable ) Then Return 0 Local $pMoniker = ROT_CreateFileMoniker( $sNameId ) If Not $pMoniker Then Return 0 Local $iHandle $oRunningObjectTable.Register( $iFlags, $pObject, $pMoniker, $iHandle ) Return $iHandle ? $iHandle : 0 EndFunc ; Returns unique GUID as string ; Copied from _WinAPI_CreateGUID Func ROT_CreateGUID() Local Static $tGUID = DllStructCreate( "struct;ulong Data1;ushort Data2;ushort Data3;byte Data4[8];endstruct" ) DllCall( "Ole32.dll", "long", "CoCreateGuid", "struct*", $tGUID ) Return "-" & DllCall( "Ole32.dll", "int", "StringFromGUID2", "struct*", $tGUID, "wstr", "", "int", 65536 )[2] EndFunc ; Failure: Returns 0 ; Success: Returns $aROT_List ; Enumerates objects and identifying monikers in the ROT Func ROT_Enumerate() Local $oRunningObjectTable = ObjCreateInterface( ROT_GetRunningObjectTable(), $sIID_IRunningObjectTable, $dtag_IRunningObjectTable ) If Not IsObj( $oRunningObjectTable ) Then Return 0 Local $pEnumMoniker $oRunningObjectTable.EnumRunning( $pEnumMoniker ) Local $oEnumMoniker = ObjCreateInterface( $pEnumMoniker, $sIID_IEnumMoniker, $dtag_IEnumMoniker ) If Not IsObj( $oEnumMoniker ) Then Return 0 Local $pMoniker, $oMoniker, $pBindCtx, $sMonikerName, $i = 0 Local $oDict = ObjCreate( "Scripting.Dictionary" ) While( $oEnumMoniker.Next( 1, $pMoniker, NULL ) = 0 ) $pBindCtx = ROT_CreateBindCtx() $oMoniker = ObjCreateInterface( $pMoniker, $sIID_IMoniker, $dtag_IMoniker ) $oMoniker.GetDisplayName( $pBindCtx, NULL, $sMonikerName ) $oDict.Add( "Key" & $i, $sMonikerName ) $i += 1 WEnd Local $aROT_List = $oDict.Items() Return $aROT_List EndFunc ; Failure: Returns 0 ; Success: Returns 1 ; Removes an object and its identifying moniker from the ROT Func ROT_Revoke( $iHandle ) If Not $iHandle Then Return 0 Local $oRunningObjectTable = ObjCreateInterface( ROT_GetRunningObjectTable(), $sIID_IRunningObjectTable, $dtag_IRunningObjectTable ) If Not IsObj( $oRunningObjectTable ) Then Return 0 Return Not $oRunningObjectTable.Revoke( $iHandle ) * 1 EndFunc 7z-fileThe 7z-file contains source code for the UDF and examples. You need AutoIt 3.3.12 or later. Tested on Windows 7 and Windows 10. Comments are welcome. Let me know if there are any issues. IRunningObjectTable.7z
    1 point
  4. Melba23

    StringInStr - (Moved)

    stiv, I told you above that this is an English speaking forum - please stop posting in Spanish or you may find you cannot post at all. M23
    1 point
  5. Thanks for your answers. I solved it using @Leendert-Jan's advice. Disabling `"launchMode": "maximized",` from the WT settings, then using WinMove and WinSetState(...@SW_MAXIMIZE) does it. @Danp2 I couldn't get a real difference neither in window coordinates nor in other useful parameters with Au3Info in both scenarios (i.e. window returning to the main monitor after minimizing vs. window moved manually)
    1 point
  6. TheDcoder

    AutoIt Snippets

    @CarlD _PathSplit
    1 point
  7. CarlD

    AutoIt Snippets

    Func _DateDiffWords uses _DateDiff and _DateAdd to report difference between two dates in words (also uses my Func _OneMany) #include <Date.au3> #include <MsgBoxConstants.au3> #AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d ; CarlD rev. 2021-09-25 Global $sDate1 = "2199/12/31 23:59:59"; later date Global $sDate2 = "1900/01/01 00:00:01"; earlier date (default = now) MsgBox( $MB_SYSTEMMODAL, @ScriptName, "Difference is:" & @LF & _DateDiffWords($sDate1) ); $sDate2 Func _DateDiffWords($date1, $date2 = @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC) ; Reports difference between two dates in words ; #include <Date.au3> ; $date1 = YYYY/MM/DD[ hh:mm[:ss]] Local $sDiffOut = "" Local $sYear = "year", $sMon = "month", $sWeek = "week", $sDay = "day" Local $sHour = "hour", $sMin = "minute", $sSec = "second" Local $sAnd = "and" Local $Y, $M, $W, $D, $H, $N, $S; _DateDiff types $Y = _DateDiff("Y", $date2, $date1) If $Y Then $sDiffOut = $Y & " " & _OneMany($sYear, $Y) $date1 = _DateAdd("Y", -1 * $Y, $date1) EndIf $M = _DateDiff("M", $date2, $date1) If $M Then If $sDiffOut Then $sDiffOut &= ", " $sDiffOut &= $M & " " & _OneMany($sMon, $M) $date1 = _DateAdd("M", -1 * $M, $date1) EndIf $W = _DateDiff("W", $date2, $date1) If $W Then If $sDiffOut Then $sDiffOut &= ", " $sDiffOut &= $W & " " & _OneMany($sWeek, $W) $date1 = _DateAdd("W", -1 * $W, $date1) EndIf $D = _DateDiff("D", $date2, $date1) If $D Then If $sDiffOut Then $sDiffOut &= ", " $sDiffOut &= $D & " " & _OneMany($sDay, $D) $date1 = _DateAdd("D", -1 * $D, $date1) EndIf $H = _DateDiff("H", $date2, $date1) If $H Then If $sDiffOut Then $sDiffOut &= ", " $sDiffOut &= $H & " " & _OneMany($sHour, $H) $date1 = _DateAdd("H", -1 * $H, $date1) EndIf $N = _DateDiff("N", $date2, $date1) If $N Then If $sDiffOut Then $sDiffOut &= ", " $sDiffOut &= $N & " " & _OneMany($sMin, $N) $date1 = _DateAdd("N", -1 * $N, $date1) EndIf $S = _DateDiff("S", $date2, $date1) If $S Then If $sDiffOut Then $sDiffOut &= " " & $sAnd & " " $sDiffOut &= $S & " " & _OneMany($sSec, $S) EndIf Return $sDiffOut EndFunc ;==>_DateDiffWords Func _OneMany($sSingular, $iCount, $sPlural = "") ; Returns singular or plural depending on count ; Plural appends S to singular (ES if it ends in S) ; unless $sPlural is supplied Local $sEss = "s" If StringRight($sSingular, 1) = "s" Then $sEss = "es" If StringUpper($sSingular) == $sSingular Then $sEss = StringUpper($sEss) If Not $sPlural Then $sPlural = $sSingular & $sEss If Abs($iCount) = 1 Then Return $sSingular Else Return $sPlural EndIf EndFunc ;==>_OneMany
    1 point
  8. Check first within chrome (versions, 32/64 bits) Check chrome://accessibility/ and see the trees as you then should see the same in UIA spy Check with inspect.exe as thats the original MS tool for spying uia
    1 point
  9. why not: Func GetRef($sFunc) Return Execute($sFunc) EndFunc
    1 point
  10. i kept looking at forums and found simple solution. not 100% effective but works 90% ild say and the answer is .. for anyone else that will need in future ... while 1 ;main gui while loop.... if $on = 1 then ;you change the variable value with a button or whatever..... while $on = 1 ;your whatever thing loop here... ;do things here... ;if you would want to exit loop while processing it through with loads of things just put this.. exitloop ;if you have more than one or two loops then for example... while $set = 1 ;do something here then ... while $go = 1 ;do something here and exit if $on = 0 then exitloop 3 ;this will exit all loops and go into the first gui loop to continue showing main gui.... wend wend wend endif ;exitloop [3] will go here automatically... :P wend those who will really need such thing will understand...
    1 point
  11. Here, let me then: #include <WinApi.au3> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; AddHookApi("user32.dll", "MessageBoxW", "Intercept_MessageBoxW", "int", "hwnd;wstr;wstr;uint") Func Intercept_MessageBoxW($hWnd, $sText, $sTitle, $iType) Local $aCall = DllCall("user32.dll", "int", "MessageBoxW", _ "hwnd", $hWnd, _ "wstr", $sText, _ "wstr", StringReplace($sTitle, "AutoIt", @ScriptName), _ "uint", $iType) If @error Or Not $aCall[0] Then Return 0 Return $aCall[0] EndFunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Let's try it ; Usual message box MsgBox(0, 'Test', 'Some text') ; Cause error that would say some AutoIt shit happened, but now it wouldn't say "AutoIt" DllStructCreate("byte[123456789097]") ; The End ; The magic is down below Func AddHookApi($sModuleName, $vFunctionName, $vNewFunction, $sRet = "", $sParams = "") Local Static $pImportDirectory, $hInstance Local Const $IMAGE_DIRECTORY_ENTRY_IMPORT = 1 If Not $pImportDirectory Then $hInstance = _WinAPI_GetModuleHandle(0) $pImportDirectory = ImageDirectoryEntryToData($hInstance, $IMAGE_DIRECTORY_ENTRY_IMPORT) If @error Then Return SetError(1, 0, 0) EndIf Local $iIsInt = IsInt($vFunctionName) Local $iRestore = Not IsString($vNewFunction) Local $tIMAGE_IMPORT_MODULE_DIRECTORY Local $pDirectoryOffset = $pImportDirectory Local $tModuleName Local $iInitialOffset, $iInitialOffset2 Local $iOffset2 Local $tBufferOffset2, $iBufferOffset2 Local $tBuffer, $tFunctionOffset, $pOld, $fMatch, $pModuleName, $pFuncName Local Const $PAGE_READWRITE = 0x04 While 1 $tIMAGE_IMPORT_MODULE_DIRECTORY = DllStructCreate("dword RVAOriginalFirstThunk;" & _ "dword TimeDateStamp;" & _ "dword ForwarderChain;" & _ "dword RVAModuleName;" & _ "dword RVAFirstThunk", _ $pDirectoryOffset) If Not DllStructGetData($tIMAGE_IMPORT_MODULE_DIRECTORY, "RVAFirstThunk") Then ExitLoop $pModuleName = $hInstance + DllStructGetData($tIMAGE_IMPORT_MODULE_DIRECTORY, "RVAModuleName") $tModuleName = DllStructCreate("char Name[" & _WinAPI_StringLenA($pModuleName) & "]", $pModuleName) If DllStructGetData($tModuleName, "Name") = $sModuleName Then ; function from this module $iInitialOffset = $hInstance + DllStructGetData($tIMAGE_IMPORT_MODULE_DIRECTORY, "RVAFirstThunk") $iInitialOffset2 = $hInstance + DllStructGetData($tIMAGE_IMPORT_MODULE_DIRECTORY, "RVAOriginalFirstThunk") If $iInitialOffset2 = $hInstance Then $iInitialOffset2 = $iInitialOffset $iOffset2 = 0 While 1 $tBufferOffset2 = DllStructCreate("dword_ptr", $iInitialOffset2 + $iOffset2) $iBufferOffset2 = DllStructGetData($tBufferOffset2, 1) If Not $iBufferOffset2 Then ExitLoop If $iIsInt Then If BitAND($iBufferOffset2, 0xFFFFFF) = $vFunctionName Then $fMatch = True; wanted function Else $pFuncName = $hInstance + $iBufferOffset2 + 2 ; 2 is size od "word", see line below... $tBuffer = DllStructCreate("word Ordinal; char Name[" & _WinAPI_StringLenA($pFuncName) & "]", $hInstance + $iBufferOffset2) If DllStructGetData($tBuffer, "Name") == $vFunctionName Then $fMatch = True; wanted function EndIf If $fMatch Then $tFunctionOffset = DllStructCreate("ptr", $iInitialOffset + $iOffset2) VirtualProtect(DllStructGetPtr($tFunctionOffset), DllStructGetSize($tFunctionOffset), $PAGE_READWRITE) If @error Then Return SetError(3, 0, 0) $pOld = DllStructGetData($tFunctionOffset, 1) If $iRestore Then DllStructSetData($tFunctionOffset, 1, $vNewFunction) Else DllStructSetData($tFunctionOffset, 1, DllCallbackGetPtr(DllCallbackRegister($vNewFunction, $sRet, $sParams))) EndIf Return $pOld EndIf $iOffset2 += DllStructGetSize($tBufferOffset2) WEnd ExitLoop EndIf $pDirectoryOffset += 20 ; size of $tIMAGE_IMPORT_MODULE_DIRECTORY WEnd Return SetError(4, 0, 0) EndFunc Func VirtualProtect($pAddress, $iSize, $iProtection) Local $aCall = DllCall("kernel32.dll", "bool", "VirtualProtect", "ptr", $pAddress, "dword_ptr", $iSize, "dword", $iProtection, "dword*", 0) If @error Or Not $aCall[0] Then Return SetError(1, 0, 0) Return 1 EndFunc Func ImageDirectoryEntryToData($hInstance, $iDirectoryEntry) ; Get pointer to data Local $pPointer = $hInstance ; Start processing passed binary data. 'Reading' PE format follows. Local $tIMAGE_DOS_HEADER = DllStructCreate("char Magic[2];" & _ "word BytesOnLastPage;" & _ "word Pages;" & _ "word Relocations;" & _ "word SizeofHeader;" & _ "word MinimumExtra;" & _ "word MaximumExtra;" & _ "word SS;" & _ "word SP;" & _ "word Checksum;" & _ "word IP;" & _ "word CS;" & _ "word Relocation;" & _ "word Overlay;" & _ "char Reserved[8];" & _ "word OEMIdentifier;" & _ "word OEMInformation;" & _ "char Reserved2[20];" & _ "dword AddressOfNewExeHeader", _ $pPointer) Local $sMagic = DllStructGetData($tIMAGE_DOS_HEADER, "Magic") ; Check if it's valid format If Not ($sMagic == "MZ") Then Return SetError(1, 0, 0) ; MS-DOS header missing. Btw 'MZ' are the initials of Mark Zbikowski in case you didn't know. ; Move pointer $pPointer += DllStructGetData($tIMAGE_DOS_HEADER, "AddressOfNewExeHeader") ; move to PE file header ; In place of IMAGE_NT_SIGNATURE structure Local $tIMAGE_NT_SIGNATURE = DllStructCreate("dword Signature", $pPointer) ; Check signature If DllStructGetData($tIMAGE_NT_SIGNATURE, "Signature") <> 17744 Then ; IMAGE_NT_SIGNATURE Return SetError(2, 0, 0) ; wrong signature. For PE image should be "PE\0\0" or 17744 dword. EndIf ; Move pointer $pPointer += 4 ; size of $tIMAGE_NT_SIGNATURE structure ; In place of IMAGE_FILE_HEADER structure ; Move pointer $pPointer += 20 ; size of $tIMAGE_FILE_HEADER structure ; Determine the type Local $tMagic = DllStructCreate("word Magic;", $pPointer) Local $iMagic = DllStructGetData($tMagic, 1) Local $tIMAGE_OPTIONAL_HEADER If $iMagic = 267 Then ; x86 version ; Move pointer $pPointer += 96 ; size of $tIMAGE_OPTIONAL_HEADER ElseIf $iMagic = 523 Then ; x64 version ; Move pointer $pPointer += 112 ; size of $tIMAGE_OPTIONAL_HEADER Else Return SetError(3, 0, 0) ; unsupported module type EndIf ; Validate input by checking available number of structures that are in the module Local Const $IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16 ; predefined value that PE modules always use (AutoIt certainly) If $iDirectoryEntry > $IMAGE_NUMBEROF_DIRECTORY_ENTRIES - 1 Then Return SetError(4, 0, 0) ; invalid input ; Calculate the offset to wanted entry (every entry is 8 bytes) $pPointer += 8 * $iDirectoryEntry ; At place of correst directory entry Local $tIMAGE_DIRECTORY_ENTRY = DllStructCreate("dword VirtualAddress; dword Size", $pPointer) ; Collect data Local $pAddress = DllStructGetData($tIMAGE_DIRECTORY_ENTRY, "VirtualAddress") If $pAddress = 0 Then Return SetError(5, 0, 0) ; invalid input ; $pAddress is RVA, add it to base address Return $hInstance + $pAddress EndFuncedit: because this is better than before.
    1 point
×
×
  • Create New...