WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: stevent on August 24, 2020, 06:02:02 AM

Title: AppExist - TerminateApp - into an Array
Post by: stevent on August 24, 2020, 06:02:02 AM
Hello,

This is more than a little embarrassing but, I am having one hell of a time figuring out how to create an array (and function) that will do the following;
I need to look to see if 10 applications might be open. If one of more processes exist, display a Message asking the user to save their work and close the application. If a user closes application (a) but not application (b) I need to reevaluate until all the processes in the array come back from the AppExist with @FALSE. I have never created an array in WinBatch (or anything else for that matter). I have some sample code from an AutoIT script someone wrote but, for the life of me I am getting stuck on how I can recreate it in WinBatch. I have been playing around with some things from the consolidated help file (arrays) etc but, can't seem to wrap my head around what to do and in what order.

AutoIT sample;
Func CloseAlert()

Local $aInternalArray_0[2] = ["OUTLOOK.EXE","Microsoft Outlook"]
Local $aInternalArray_1[2] = ["lync.exe","Skype / Lync"]
Local $aInternalArray_2[2] = ["iexplore.exe","Internet Explorer"]
Local $aInternalArray_3[2] = ["winword.exe","Microsoft Word"]
Local $aInternalArray_4[2] = ["excel.exe","Microsoft Excel"]
Local $aInternalArray_5[2] = ["POWERPNT.exe","Microsoft PowerPoint"]
Local $aInternalArray_6[2] = ["atmgr.exe","Webex"]
Local $aInternalArray_7[2] = ["firefox.exe","Mozilla Firefox"]
Local $aInternalArray_8[2] = ["WINPROJ.exe","Microsoft Project"]
Local $aInternalArray_9[2] = ["CiscoWebExStart.exe","Cisco Meeting Center"]

Local $ProcArray[10]
$ProcArray[0] = $aInternalArray_0
$ProcArray[1] = $aInternalArray_1
$ProcArray[2] = $aInternalArray_2
$ProcArray[3] = $aInternalArray_3
$ProcArray[4] = $aInternalArray_4
$ProcArray[5] = $aInternalArray_5
$ProcArray[6] = $aInternalArray_6
$ProcArray[7] = $aInternalArray_7
$ProcArray[8] = $aInternalArray_8
$ProcArray[9] = $aInternalArray_9
Local $procsmessage=""


For $proc In $ProcArray
If ProcessExists($proc[0]) Then
   ;MsgBox(0, "Programs Running", "The Following Programs are running:"&@CRLF&$proc&@CRLF& "To avoid loss or interruption, Please save all work and close them before you click 'OK' to proceed."&@CRLF)
   $procsmessage=$procsmessage&$proc[1]&@CRLF
EndIf
Next

If $procsmessage<>"" Then
MsgBox(4096, "Programs Running", "The Following Programs Are Running:"&@CRLF&@CRLF&$procsmessage&@CRLF& "To avoid loss of data or interruption, please save your work and close these programs before you click 'OK' to proceed."&@CRLF&"This installation will automatically proceed after 5 minutes.", 284)
;Else
;MsgBox(0, "You're Cool", "You're Cool"&@CRLF)
EndIf

EndFunc
Title: Re: AppExist - TerminateApp - into an Array
Post by: JTaylor on August 24, 2020, 07:01:27 AM
Assuming you have a fairly recent version of WinBatch you might consider using a "Map" instead of the standard array since you only have two fields and they are unique.   Lookup MapCreate() and see if those examples are easier to utilize.


Jim
Title: Re: AppExist - TerminateApp - into an Array
Post by: td on August 24, 2020, 07:21:27 AM
Or you could consider a rank 2 array (an array with two dimensions.)  You create multi-dimensional arrays using the ArrDimension function.  The documentation for the function can be found here:

https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/WILAK_A__020.htm

Although using a WIL Map is likely the simpler solution.
Title: Re: AppExist - TerminateApp - into an Array
Post by: stanl on August 25, 2020, 03:02:03 AM
Since the code you set out is in AutoIt, not WB you might consider designing your script around tlistproc(). Load 1 or more of the applications in your list and see how they would appear using
Code (WINBATCH) Select


AddExtender("WWPRC44I.DLL",0,"WWPRC64I.DLL")
list=tListProc()
process=AskItemlist("Process List",list,@TAB,@UNSORTED,@SINGLE)





How the app appears in the itemlist will help determine any map or array you might build.


[EDIT]: cheap shot w/out map or array


Code (WINBATCH) Select


AddExtender("WWPRC44I.DLL",0,"WWPRC64I.DLL")
srchapps = "EXCEL,SKYPE,WINBATCH"
list=strupper(tListProc())


n=ItemCount(srchapps,",")
results = ""
For i = 1 to n
   srch = ItemExtract(i,srchapps,",")
   If StrIndex(list,srch,1,@FWDSCAN)>0
      results = results:srch:" is active Process":@LF
   Else
      results = results:srch:" is Not active Process":@LF
   Endif


Next


Message("Searching Processes",results)
Exit
Title: Re: AppExist - TerminateApp - into an Array
Post by: ChuckC on August 25, 2020, 03:48:41 AM
Additional options are to use WMI and/or .NET, too, as both of them have classes for process enumeration, interrogation and command & control.  Or, if you want to go old school, WinBatch's DllCall() can be used to directly invoke the Win32 API functions that all other suggested method make use of to do their work.
Title: Re: AppExist - TerminateApp - into an Array
Post by: td on August 25, 2020, 08:10:01 AM
The WIL AppExist and TerminateApp functions are analogous to the functions used in the OPs example.

https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/WILAK_A__005.htm (https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/WILAK_A__005.htm)

https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/WILLZ_T__021.htm (https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/WILLZ_T__021.htm)