david1337

Set application in idle state

2 posts in this topic

Hi guys

I have a script that runs for about 20 seconds.

If Outlook.exe is not already open, and I open it while the script is running, I want Outlook to wait until the script is done, and then open.
Is it possible to set an application in some sort of idle state until the script is done? I want to avoid it from just closing Outlook, and open it again.
 

Thanks

David

Share this post


Link to post
Share on other sites



; Johnmcloud - 2016
; _GetAllProcessThreads by monoceres, a little edited

Local $iPID = Run("notepad")
Sleep(1000)

_ProcessSuspend($iPID)
Sleep(2000)
_ProcessResume($iPID)

Func _ProcessSuspend($iPID)
    Local $THREAD_SUSPEND_RESUME = 0x0002
    Local $hWnd, $aProcessThreads = _GetAllProcessThreads($iPID)
    For $i = 1 To $aProcessThreads[0][0]
        $hWnd = DllCall('Kernel32.dll', 'hwnd', "OpenThread", "int", $THREAD_SUSPEND_RESUME, "int", 0, 'int', $aProcessThreads[$i][0])
        If Not @error Then DllCall('Kernel32.dll', 'uint', "SuspendThread", 'hwnd', $hWnd[0])
    Next
    Return 1
EndFunc   ;==>_ProcessSuspend

Func _ProcessResume($iPID)
    Local $THREAD_SUSPEND_RESUME = 0x0002
    Local $hWnd, $aProcessThreads = _GetAllProcessThreads($iPID)
    For $i = 1 To $aProcessThreads[0][0]
        $hWnd = DllCall('Kernel32.dll', 'hwnd', "OpenThread", "int", $THREAD_SUSPEND_RESUME, "int", 0, 'int', $aProcessThreads[$i][0])
        If Not @error Then DllCall('Kernel32.dll', 'uint', "ResumeThread", 'hwnd', $hWnd[0])
    Next
    Return 1
EndFunc   ;==>_ProcessResume

Func _GetAllProcessThreads($iPID)
    Local Const $TH32CS_SNAPTHREAD = 0x00000004
    Local Const $THREADENTRY32 = "dword dwSize;dword cntUsage;dword th32ThreadId;dword th32OwnerProcessID;long tpBasePri;long tpDeltaPri;dword dwFlags;"
    Local $aCall = DllCall("Kernel32.dll", "ptr", "CreateToolhelp32Snapshot", "dword", $TH32CS_SNAPTHREAD, "dword", 0)
    If @error Then Return SetError(1, 0, 0)
    Local $hThread = $aCall[0]
    Local $aReturn[1][3]
    Local $Thread32 = DllStructCreate($THREADENTRY32)
    DllStructSetData($Thread32, "dwSize", DllStructGetSize($Thread32))
    $aCall = DllCall("Kernel32.dll", "int", "Thread32First", "ptr", $hThread, "ptr", DllStructGetPtr($Thread32))
    If @error Then Return SetError(2, 0, 0)
    If DllStructGetData($Thread32, "th32OwnerProcessID") = $iPID Then _GetAllThreads_ArrHelper($aReturn, $Thread32)
    Do
        $aCall = DllCall("Kernel32.dll", "int", "Thread32Next", "ptr", $hThread, "ptr", DllStructGetPtr($Thread32))
        If Not $aCall[0] Then ExitLoop
        If DllStructGetData($Thread32, "th32OwnerProcessID") = $iPID Then _GetAllThreads_ArrHelper($aReturn, $Thread32)
    Until True And False
    $aReturn[0][0] = UBound($aReturn) - 1 ; number of items in the array
    DllCall("Kernel32.dll", "bool", "CloseHandle", "handle", $hThread)
    If @error Then Return SetError(3, 0, 0)
    Return $aReturn
EndFunc   ;==>_GetAllProcessThreads

Func _GetAllThreads_ArrHelper(ByRef $aArray, $Thread32_Struct)
    Local $iUBound = UBound($aArray)
    ReDim $aArray[$iUBound + 1][3]
    $aArray[$iUBound][0] = DllStructGetData($Thread32_Struct, "th32ThreadId")
    $aArray[$iUBound][1] = DllStructGetData($Thread32_Struct, "th32OwnerProcessID")
    $aArray[$iUBound][2] = DllStructGetData($Thread32_Struct, "tpBasePri")
EndFunc   ;==>_GetAllThreads_ArrHelper

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • Nareshm
      By Nareshm
      If Process exits then end process and ;Some code here {1}
       
      If Process does not exits then ; My {1} Code
    • ur
      By ur
      How to retrieve the target executable path from a process.
      My system is effected with IMG001.exe virus and I remove the folders created by it daily but still it is creating the folders everytime I login to my PC.
      My Antivirus is not detecting it.

       
      So I thought to create a process in AutoIT to check for the process name IMG001.exe and retrieve the process target exe to a log file, so that I can track where it is putting these files.
      With  ProcessExists ( "process" ) , i can get the process ID.
      But how to get the target location of the executable of the process.??
    • salah kai
      By salah kai
      Hey everyone
      i wanna close a process by  path like
      C:\Users\salah\AppData\Local\Temp\a.exe
      processclose(C:\Users\salah\AppData\Local\Temp\a.exe)
      i tried to split the path but i don't know how to know last loop
      and thanks 
       
    • iXX
      By iXX
      Hi!
      Looking for working code to  get full path of process  - both 32 & 64 bit.
      I tryed this bellow, but it works only for 32-bit processes, even if compiled for x64...
      Thanx for suggestions!
       
      Func _ProcessGetPath($vProcess) ;get the program path done by MrCreatoR Local $iPID = ProcessExists($vProcess) If NOT $iPID Then Return SetError(1, 0, -1) Local $aProc = DllCall('kernel32.dll', 'hwnd', 'OpenProcess', 'int', BitOR(0x0400, 0x0010), 'int', 0, 'int', $iPID) If NOT IsArray($aProc) OR NOT $aProc[0] Then Return SetError(2, 0, -1) Local $vStruct = DllStructCreate('int[1024]') Local $hPsapi_Dll = DllOpen('Psapi.dll') If $hPsapi_Dll = -1 Then $hPsapi_Dll = DllOpen(@SystemDir & '\Psapi.dll') If $hPsapi_Dll = -1 Then $hPsapi_Dll = DllOpen(@WindowsDir & '\Psapi.dll') If $hPsapi_Dll = -1 Then Return SetError(3, 0, '') DllCall($hPsapi_Dll, 'int', 'EnumProcessModules', _ 'hwnd', $aProc[0], _ 'ptr', DllStructGetPtr($vStruct), _ 'int', DllStructGetSize($vStruct), _ 'int_ptr', 0) Local $aRet = DllCall($hPsapi_Dll, 'int', 'GetModuleFileNameEx', _ 'hwnd', $aProc[0], _ 'int', DllStructGetData($vStruct, 1), _ 'str', '', _ 'int', 2048) DllClose($hPsapi_Dll) If NOT IsArray($aRet) OR StringLen($aRet[3]) = 0 Then Return SetError(4, 0, '') Return $aRet[3] EndFunc  
    • Phoenixx177
      By Phoenixx177
      I have a script that is calling 2 browser windows in my GUI and loading up the appropriate URLs.  Tested alone it works fine.  I also have another script that launches a 3rd party application in the GUI.  When I try to modify my 2nd script so that it includes the two browser windows along with the GUI, the GUI loads but not the browser windows.  Can someone get a second set of eyes on the script below and tell me where my brain jumped track please?  Many Thanks.
      #include <GUIConstants.au3> #include <Constants.au3> #include <windowsconstants.au3> #include <IE.au3> Global $oIE_google = _IECreateEmbedded() Global $oIE_autoit = _IECreateEmbedded() Opt("GUIOnEventMode", 1) ; Change to OnEvent mode $mainWindow = GUICreate("Embed Cmd", 1280, 780, 10, 10) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") GUISetState (@SW_SHOW) GUIRegisterMsg(0xF, "WM_PAINT") ; create a borderless window that is a child to the main window $embedWindow = GUICREATE("", 700, 400, 15, 15, $WS_POPUP, -1, $mainWindow) Global $google = GUICtrlCreateObj($oIE_google, 10, 10, 1000, 300) Global $autoit = GUICtrlCreateObj($oIE_autoit, 800, 10, 500, 300) _IENavigate($oIE_google, "http://www.google.com") _IENavigate($oIE_autoit, "http://www.bing.com") DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $embedWindow, "hwnd", $mainWindow) ; launch the command prompt (black on white, without the operating system message) $pid = run("C:\Program Files (x86)\Cisco\Router Manager\Router Administration.exe") ProcessWait ($pid) ; get the handle of the cmd window as i cannot be certain that there will be only one instance of the cmd running with the same window title or class $cmdHandle = _ProcessGetHWnd($pid, 2) $hWndChild = $cmdHandle[1][1] ; make the command prompt window a child to the earlier created borderless child window DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $hWndChild, "hwnd", $embedWindow) ; resize the command prompt window so that its bolder and title bar are outside the borderless child window ; giving the appearance of a borderless command prompt WinMove($hWndChild, '', 10, 500, 485, 206) ;WinMove($hWndChild, '', 10, 500) WinSetState($hWndChild, '', @SW_SHOW) WinSetState($embedWindow, '', @SW_SHOW) WinSetState($oIE_google, '', @SW_SHOW) WinSetState($oIE_autoit, '', @SW_SHOW) ; inifinite event loop While 1 ; sleep for 100 milliseconds (to not hog the cpu) sleep(100) ; end of event loop WEnd Func CLOSEClicked() ; take care of things to do when exiting Winkill($hWndChild) Exit EndFunc Func WM_PAINT($hWnd, $Msg, $wParam, $lParam) Sleep(100) DllCall("user32.dll", "int", "InvalidateRect", "hwnd", $hWnd, "ptr", 0, "int", 0) EndFunc ;==>WM_PAINT ;=============================================================================== ; ; Function Name: _ProcessGetHWnd ; Description: Returns the HWND(s) owned by the specified process (PID only !). ; ; Parameter(s): $iPid - the owner-PID. ; $iOption - Optional : return/search methods : ; 0 - returns the HWND for the first non-titleless window. ; 1 - returns the HWND for the first found window (default). ; 2 - returns all HWNDs for all matches. ; ; $sTitle - Optional : the title to match (see notes). ; $iTimeout - Optional : timeout in msec (see notes) ; ; Return Value(s): On Success - returns the HWND (see below for method 2). ; $array[0][0] - number of HWNDs ; $array[x][0] - title ; $array[x][1] - HWND ; ; On Failure - returns 0 and sets @error to 1. ; ; Note(s): When a title is specified it will then only return the HWND to the titles ; matching that specific string. If no title is specified it will return as ; described by the option used. ; ; When using a timeout it's possible to use WinWaitDelay (Opt) to specify how ; often it should wait before attempting another time to get the HWND. ; ; ; Author(s): Helge ; ;=============================================================================== Func _ProcessGetHWnd($iPid, $iOption = 1, $sTitle = "", $iTimeout = 2000) Local $aReturn[1][1] = [[0]], $aWin, $hTimer = TimerInit() While 1 ; Get list of windows $aWin = WinList($sTitle) ; Searches thru all windows For $i = 1 To $aWin[0][0] ; Found a window owned by the given PID If $iPid = WinGetProcess($aWin[$i][1]) Then ; Option 0 or 1 used If $iOption = 1 OR ($iOption = 0 And $aWin[$i][0] <> "") Then Return $aWin[$i][1] ; Option 2 is used ElseIf $iOption = 2 Then ReDim $aReturn[UBound($aReturn) + 1][2] $aReturn[0][0] += 1 $aReturn[$aReturn[0][0]][0] = $aWin[$i][0] $aReturn[$aReturn[0][0]][1] = $aWin[$i][1] EndIf EndIf Next ; If option 2 is used and there was matches then the list is returned If $iOption = 2 And $aReturn[0][0] > 0 Then Return $aReturn ; If timed out then give up If TimerDiff($hTimer) > $iTimeout Then ExitLoop ; Waits before new attempt Sleep(Opt("WinWaitDelay")) WEnd ; No matches SetError(1) Return 0 EndFunc ;==>_ProcessGetHWnd $StaticTxt = ControlGetText("Embed Cmd","",1007) MsgBox(1,"Static Text", $StaticTxt) GUISetState() While GUIGetMsg() <> -3 WEnd