Leaderboard
Popular Content
Showing content with the highest reputation on 08/15/2024 in all areas
-
Script with _WinAPI_ReadDirectoryChanges
hudsonhock and one other reacted to Nine for a topic
As per help file : It means this is a blocking function, it will wait forever for a change. But there is a way to make it asynchronous. You would need to use the Overlapped version of it. Search the forum, I believe there is an example of it. ps. made one for the fun of it : #include <Constants.au3> #include <WinAPI.au3> Opt("MustDeclareVars", True) HotKeySet("{ESC}", Terminate) Global Const $WATCH_PATH = "C:\Apps\Temp" Global Const $BUFFER_LENGTH = 4096 Global Const $NOTIFY_FLAGS = BitOR($FILE_NOTIFY_CHANGE_FILE_NAME, $FILE_NOTIFY_CHANGE_DIR_NAME) Global $tOverlapped, $hDirectory, $hCompletion, $pCompletion, $pDataBuffer, $hEvent DirectoryChange() Func DirectoryChange() $tOverlapped = DllStructCreate($tagOVERLAPPED) $hEvent = _WinAPI_CreateEvent(0, False, False) $tOverlapped.hEvent = $hEvent $hDirectory = _WinAPI_CreateFileEx($WATCH_PATH, $OPEN_EXISTING, $FILE_LIST_DIRECTORY, _ BitOR($FILE_SHARE_READ, $FILE_SHARE_WRITE, $FILE_SHARE_DELETE), _ BitOR($FILE_FLAG_BACKUP_SEMANTICS, $FILE_FLAG_OVERLAPPED)) $hCompletion = DllCallbackRegister(CompletionRoutine, "none", "dword;dword;ptr") $pCompletion = DllCallbackGetPtr($hCompletion) $pDataBuffer = _WinAPI_CreateBuffer($BUFFER_LENGTH) _WinAPI_ReadDirectoryChangesW($hDirectory, $NOTIFY_FLAGS, $pDataBuffer, $BUFFER_LENGTH, True, $tOverlapped, $pCompletion) OnAutoItExitRegister(CleanUp) While True _WinAPI_WaitForSingleObjectEx($hEvent, 0, 1) Sleep(100) WEnd EndFunc ;==>DirectoryChange ;~ Make decisions on file changes in here Func FileNotifyUser($nFileAction, $sFileName) Switch $nFileAction Case $FILE_ACTION_ADDED ConsoleWrite("File added: " & $sFileName & @CRLF) Case $FILE_ACTION_REMOVED ConsoleWrite("File removed: " & $sFileName & @CRLF) Case $FILE_ACTION_RENAMED_OLD_NAME ConsoleWrite("File renamed from: " & $sFileName & @CRLF) Case $FILE_ACTION_RENAMED_NEW_NAME ConsoleWrite("File renamed to: " & $sFileName & @CRLF) EndSwitch EndFunc ;==>FileNotifyUser Func CleanUp() _WinAPI_CancelIoEx($hDirectory, $tOverlapped) _WinAPI_CloseHandle($hDirectory) _WinAPI_FreeMemory($pDataBuffer) _WinAPI_CloseHandle($hEvent) DllCallbackFree($hCompletion) EndFunc ;==>CleanUp Func CompletionRoutine($nError, $nTransfered, $pOverlapped) Local $pBuffer = $pDataBuffer, $tNotify, $nLength While True $nLength = DllStructGetData(DllStructCreate("dword", $pBuffer + 8), 1) $tNotify = DllStructCreate("dword NextOffset;dword Action;dword NameLength;wchar FileName[" & $nLength / 2 & "]", $pBuffer) FileNotifyUser($tNotify.Action, $tNotify.FileName) If Not $tNotify.NextOffset Then ExitLoop $pBuffer += $tNotify.NextOffset WEnd _WinAPI_ReadDirectoryChangesW($hDirectory, $NOTIFY_FLAGS, $pDataBuffer, $BUFFER_LENGTH, 0, $tOverlapped, $pCompletion) EndFunc ;==>CompletionRoutine Func _WinAPI_ReadDirectoryChangesW($hDirectory, $iFilter, $pBuffer, $iLength, $bSubtree = 0, $tOverlapped = 0, $pCompletion = 0) Local $aRet = DllCall('kernel32.dll', 'bool', 'ReadDirectoryChangesW', 'handle', $hDirectory, 'struct*', $pBuffer, _ 'dword', $iLength - Mod($iLength, 4), 'bool', $bSubtree, 'dword', $iFilter, 'dword*', 0, 'struct*', $tOverlapped, 'PTR', $pCompletion) If @error Or Not $aRet[0] Then Return SetError(@error + 10, @extended, 0) Return SetExtended(_WinAPI_GetLastError(), $aRet[0]) EndFunc ;==>_WinAPI_ReadDirectoryChangesW Func _WinAPI_CancelIoEx($hFile, $tOverlapped) Local $aRet = DllCall('kernel32.dll', 'bool', 'CancelIoEx', 'handle', $hFile, 'struct*', $tOverlapped) If @error Then Return SetError(@error, @extended, 0) Return $aRet[0] EndFunc ;==>_WinAPI_CancelIoEx Func _WinAPI_WaitForSingleObjectEx($hEvent, $nMilliseconds, $bAlertable = 0) Local $aRet = DllCall('kernel32.dll', 'dword', 'WaitForSingleObjectEx', 'handle', $hEvent, 'dword', $nMilliseconds, 'bool', $bAlertable) If @error Then Return SetError(@error, @extended, 0) Return $aRet[0] EndFunc ;==>_WinAPI_WaitForSingleObjectEx Func Terminate() Exit EndFunc ;==>Terminate2 points -
🙂 of course, but can't say we aren't biased.1 point
-
I agree, but here, since it's not specified, and what we're interested is, if Chrome is open, I think it's sufficient and the op can take a step further1 point
-
Request: ReadDirectoryChangesExW example
argumentum reacted to Nine for a topic
Definitely buggy on the rename, got same issue as you. I added a Case Else on action and there is no other, so the bug is not about a change in action. Didn't test much the new EX, but size works well. IDK about the other triggers. In any case, this API is not well written. We do not know what has been modified. Won't be using it for sure... Passing $tNofify as a parameter is a good idea.1 point -
OCR_ClickText Clicks on a text in a window using Optical Character Recognition (OCR) technology. This function uses the _UWPOCR_GetWordsRectTo2DArray() function from the UWPOCR UDF , to get the words rectangles in the window. (Thanks to Danyfirex) ; https://www.autoitscript.com/forum/topic/212167-ocr_clicktext/ ;---------------------------------------------------------------------------------------- ; Title...........: OCR_ClickText.au3 ; Description.....: Clicks on a text in a window using Optical Character Recognition (OCR) technology. ; AutoIt Version..: 3.3.16.1 Author: ioa747 Script Version: 1.0 ; Note............: Testet in Win10 22H2 ;---------------------------------------------------------------------------------------- #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <GUIConstantsEx.au3> #include <ScreenCapture.au3> #include <WindowsConstants.au3> #include <Array.au3> #include "UWPOCR.au3" ; * <-"https://www.autoitscript.com/forum/topic/207324-uwpocr-windows-platform-optical-character-recognition-api-implementation" Global $g_idMemo, $MyGui, $a_idBtn[7] ;Run Example3() in separate process as example Window ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If $CmdLine[0] > 0 Then If $CmdLine[1] = "Example3" Then Example3() Exit EndIf Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" Example3') ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Global $hWin = WinWait("Example3") ; with "*" _ArrayDisplay all found words (initialize mode) OCR_ClickText($hWin, "*") ; If it ends with a "*", it will be treated as a partial match (First match) OCR_ClickText($hWin, "Button*") OCR_ClickText($hWin, "Button2") OCR_ClickText($hWin, "Button5") ;to me Button1 sees it as ButtonL. I overcome this by using the offset parameter OCR_ClickText($hWin, "Button2", "primary", 1, 1, 0, -60) ;-------------------------------------------------------------------------------------------------------------------------------- Func Example3() $MyGui = GUICreate("Example3", 510, 400) GUISetFont($g_idMemo, 9) ConsoleWrite("$MyGui=" & $MyGui & @CRLF) $g_idMemo = GUICtrlCreateEdit("", 119, 10, 376, 374, $WS_VSCROLL) $a_idBtn[0] = 6 Local $y = 20, $iWidth = 90, $iHeight = 50 For $x = 1 To $a_idBtn[0] $a_idBtn[$x] = GUICtrlCreateButton("Button" & $x, 10, $y, $iWidth, $iHeight) $y += 60 Next GUISetState(@SW_SHOW) Local $iMsg While 1 $iMsg = GUIGetMsg() Switch $iMsg Case $GUI_EVENT_CLOSE ExitLoop Case $a_idBtn[1] To $a_idBtn[$a_idBtn[0]] MemoWrite("cklick-" & GUICtrlRead($a_idBtn[$iMsg - $a_idBtn[1] + 1])) EndSwitch WEnd EndFunc ;==>Example3 ;-------------------------------------------------------------------------------------------------------------------------------- Func MemoWrite($sMessage) ; Write a line to the memo control GUICtrlSetData($g_idMemo, $sMessage & @CRLF, 1) EndFunc ;==>MemoWrite ; #FUNCTION# -------------------------------------------------------------------------------------------------------------------- ; Name...........: OCR_ClickText ; Description....: Clicks on a text in a window using Optical Character Recognition (OCR) technology. ; Syntax.........: OCR_ClickText($hWnd, $sFindText = "*", $sButton = "primary", $iClicks = 1, $iSpeed = 1, $sLanguageTag = Default) ; Parameters.....: $hWnd Handle of the window to search in. ; $sFindText [optional] Text to find in the window, with "*" _ArrayDisplay all found words (initialize mode) (default = "*"). ; If it ends with a "*", it will be treated as a partial match (First match). ; $sButton [optional] Button to click on the found text (default = "primary"). ; $iClicks [optional] Number of clicks to perform on the button (default = 1). ; $iSpeed [optional] Speed at which to click the button, in milliseconds (default = 1). ; $iXOffset [optional] if it is positive it adds , if it is negative it subtracts to X coords (default = 0). ; $iYOffset [optional] if it is positive it adds , if it is negative it subtracts to Y coords (default = 0). ; $sLanguageTag [optional] Language tag to use for OCR. (default = first OS language). ; Return values..: Success - 1 if text was found and clicked on, 0 otherwise. ; With "*" as $sFindText parameter return _ArrayDisplay with all found words with coords (initialize mode) ; Failure - Returns an error code ; @error = 1 if the function fails to capture a screenshot of the window. ; @error = 2 if the function fails to get the words rectangles using OCR. ; Author ........: ioa747 ; Notes .........: This function uses the _UWPOCR_GetWordsRectTo2DArray() function from the UWPOCR UDF to get the words rectangles in the window. ; Link ..........: https://www.autoitscript.com/forum/topic/212167-ocr_clicktext/ ; Dependencies...: #include "UWPOCR.au3" ;https://www.autoitscript.com/forum/topic/207324-uwpocr-windows-platform-optical-character-recognition-api-implementation ;-------------------------------------------------------------------------------------------------------------------------------- Func OCR_ClickText($hWnd, $sFindText = "*", $sButton = "primary", $iClicks = 1, $iSpeed = 1, $iXOffset = 0, $iYOffset = 0, $sLanguageTag = Default) Local $hHBitmap, $hBitmap Local $aWPos = WinGetPos($hWnd) Local $RetVal = 0 _GDIPlus_Startup() $hHBitmap = _ScreenCapture_Capture("", $aWPos[0], $aWPos[1], $aWPos[0] + $aWPos[2], $aWPos[1] + $aWPos[3], False) If @error Then Return SetError(1, 0, 0) $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap) Local $aWords = _UWPOCR_GetWordsRectTo2DArray($hBitmap, $sLanguageTag) If @error Then Return SetError(2, 0, 0) Local $bPartial = StringRight($sFindText, 1) = "*" ? True : False If $sFindText = '*' Then $RetVal = _ArrayDisplay($aWords) Else For $i = 1 To UBound($aWords) - 1 If $bPartial = True Then If StringLeft($aWords[$i][0], StringLen($sFindText) - 1) = StringTrimRight($sFindText, 1) Then ExitLoop Else If $aWords[$i][0] = $sFindText Then ExitLoop EndIf Next If $i < UBound($aWords) Then ConsoleWrite($aWords[$i][0] & " -> " & $aWords[$i][1] & ";" & $aWords[$i][2] & ";" & $aWords[$i][3] & ";" & $aWords[$i][4] & @CRLF) $RetVal = MouseClick($sButton, $aWPos[0] + $aWords[$i][1] + $iXOffset + $aWords[$i][3] / 2, _ $aWPos[1] + $aWords[$i][2] + $iYOffset + $aWords[$i][4] / 2, $iClicks, $iSpeed) EndIf EndIf _WinAPI_DeleteObject($hHBitmap) _GDIPlus_BitmapDispose($hBitmap) _GDIPlus_Shutdown() Return $RetVal EndFunc ;==>OCR_ClickText ;-------------------------------------------------------------------------------------------------------------------------------- Please, every comment is appreciated! leave your comments and experiences here! Thank you very much1 point
-
Script with _WinAPI_ReadDirectoryChanges
hudsonhock reacted to argumentum for a topic
Beautiful @Nine. Did change the buffer size from 4k to 64k as otherwise creating and removing 200 files at once would not catch them all. Any reason to the set a small buffer on this version ? My counter: ... ... Func FileNotifyUser($nFileAction, $sFileName) Local Static $iCount = 0 Switch $nFileAction Case $FILE_ACTION_ADDED $iCount += 1 ConsoleWrite($iCount & @TAB & "File added: " & $sFileName & @CRLF) Case $FILE_ACTION_REMOVED $iCount -= 1 ... ...1 point -
Or a full interface version : #include <Constants.au3> Opt("MustDeclareVars", True) Interface() Func Interface() Local Const $sTagIDispatch = _ "GetTypeInfoCount hresult(dword*);" & _ "GetTypeInfo hresult(dword;dword;ptr*);" & _ "GetIDsOfNames hresult(struct*;struct*;dword;dword;struct*);" & _ "Invoke hresult(uint;struct*;dword;word;struct*;struct*;ptr;uint*);" Local Const $sCLSID_NetworkListManager = "{DCB00C01-570F-4A9B-8D69-199FDBA5723B}" Local Const $sIID_INetworkListManager = "{DCB00000-570F-4A9B-8D69-199FDBA5723B}" Local Const $sTag_INetworkListManager = $sTagIDispatch & _ "GetNetworks hresult(int;ptr*);" & _ "GetNetwork hresult(ptr;ptr*)" & _ "GetNetworkConnections hresult(ptr*);" & _ "GetNetworkConnection hresult(ptr;ptr*);" & _ "IsConnectedToInternet hresult(short*);" & _ "IsConnected hresult(short*);" & _ "GetConnectivity hresult(int_ptr*);" & _ "SetSimulatedProfileInfo hresult(struct*);" & _ "ClearSimulatedProfileInfo hresult();" Local Const $sIID_IEnumNetworkConnections = "{DCB00006-570F-4A9B-8D69-199FDBA5723B}" Local Const $sTag_IEnumNetworkConnections = $sTagIDispatch & _ "NewEnum hresult(ptr*);" & _ "Next hresult(int;ptr*;int*);" & _ "Skip hresult(int);" & _ "Reset hresult();" & _ "Clone hresult(ptr*);" Local Enum $NLM_CONNECTIVITY_DISCONNECTED, $NLM_CONNECTIVITY_IPV4_NOTRAFFIC, $NLM_CONNECTIVITY_IPV6_NOTRAFFIC, _ $NLM_CONNECTIVITY_IPV4_SUBNET = 0x10, $NLM_CONNECTIVITY_IPV4_LOCALNETWORK = 0x20, $NLM_CONNECTIVITY_IPV4_INTERNET = 0x40, _ $NLM_CONNECTIVITY_IPV6_SUBNET = 0x100, $NLM_CONNECTIVITY_IPV6_LOCALNETWORK = 0x200, $NLM_CONNECTIVITY_IPV6_INTERNET = 0x400 Local Const $sIID_INetworkConnection = "{DCB00005-570F-4A9B-8D69-199FDBA5723B}" Local Const $sTag_INetworkConnection = $sTagIDispatch & _ "GetNetwork hresult(ptr*)" & _ "IsConnectedToInternet hresult(boolean*);" & _ "IsConnected hresult(boolean*);" & _ "GetConnectivity hresult(int*);" & _ "GetConnectionId hresult(clsid*);" & _ "GetAdapterId hresult(clsid*);" & _ "GetDomainType hresult(int*);" Local Enum $NLM_NETWORK_CATEGORY_PUBLIC, $NLM_NETWORK_CATEGORY_PRIVATE, $NLM_NETWORK_CATEGORY_DOMAIN_AUTHENTICATED Local Const $sIID_INetwork = "{DCB00002-570F-4A9B-8D69-199FDBA5723B}" Local Const $sTag_INetwork = $sTagIDispatch & _ "GetName hresult(wstr*);" & _ "SetName hresult(wstr);" & _ "GetDescription hresult(wstr*);" & _ "SetDescription hresult(wstr);" & _ "GetNetworkId hresult(clsid*);" & _ "GetDomainType hresult(int*);" & _ "GetNetworkConnections hresult(ptr*);" & _ "GetTimeCreatedAndConnected hresult(int*;int*;int*;int*);" & _ "IsConnectedToInternet hresult(boolean*);" & _ "IsConnected hresult(boolean*);" & _ "GetConnectivity hresult(int*);" & _ "GetCategory hresult(int*);" & _ "SetCategory hresult(int);" Local $pNWCs, $iNum, $pConn, $oNWC, $sGUID, $pNet, $oNet, $sName Local $oNLM_INetworkListManager = ObjCreateInterface($sCLSID_NetworkListManager, $sIID_INetworkListManager, $sTag_INetworkListManager) If Not IsObj($oNLM_INetworkListManager) Then Exit MsgBox($MB_OK, "Error", "INetworkListManager") $oNLM_INetworkListManager.GetNetworkConnections($pNWCs) Local $oNWCs = ObjCreateInterface($pNWCs, $sIID_IEnumNetworkConnections, $sTag_IEnumNetworkConnections) If Not IsObj($oNWCs) Then Exit MsgBox($MB_OK, "Error", "IEnumNetworkConnections") While True $oNWCs.Next(1, $pConn, $iNum) If Not $iNum Then ExitLoop $oNWC = ObjCreateInterface($pConn, $sIID_INetworkConnection, $sTag_INetworkConnection) If Not IsObj($oNWC) Then Exit MsgBox($MB_OK, "Error", "INetworkConnection") $oNWC.GetAdapterId($sGUID) ConsoleWrite("GetAdapterId: " & $sGUID & @CRLF) $oNWC.GetNetwork($pNet) $oNet = ObjCreateInterface($pNet, $sIID_INetwork, $sTag_INetwork) If Not IsObj($oNet) Then Exit MsgBox($MB_OK, "Error", "INetwork") $oNet.GetName($sName) ConsoleWrite("GetName: " & $sName & @CRLF) WEnd EndFunc ;==>Interface1 point