Bert Posted May 7, 2007 Share Posted May 7, 2007 I have a script that will detect a window and close it. Currently it is designed to look for notepad for proof of concept. What I like to do is have a white list of windows to allow, and if any window is opened that doesn't match the list, it gets closed. Items I want in the whitelist are Program manager and Firefox. Any help would be welcomed. CODEexpandcollapse popup#include <Array.au3> dim $var, $i, $array HotKeySet("asd", "_exit") Opt("WinTitleMatchMode", 2) while 1 sleep(100) _getwinlist() For $x = 0 to UBound($array) -1 $result = StringInStr($array[$x], "Notepad") if $result > 0 then WinClose("Notepad") _Talk("Access Denied!") MsgBox(0, "Access Denied!", "Please contact Volly if you need access to this application.") endif next WEnd Func _Talk($sText) $talk = ObjCreate("SAPI.SpVoice") $talk.Speak($sText) EndFunc func _exit() Exit EndFunc func _getwinlist() $array = _ArrayCreate("") $var = WinList() For $i = 1 to $var[0][0] If $var[$i][0] <> "" AND IsVisible($var[$i][1]) Then $adj = _ArrayAdd($array, $var[$i][0]) EndIf Next _ArrayDelete($array,0) EndFunc Func IsVisible($handle) If BitAnd( WinGetState($handle), 2 ) Then Return 1 Else Return 0 EndIf EndFunc The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
Paulie Posted May 7, 2007 Share Posted May 7, 2007 (edited) You could try using a .txt file to do it. CODEexpandcollapse popup#include <Array.au3> #include <File.au3> dim $var, $i, $array, $location="C:\Documents and Settings\"&@UserName&"\Desktop\Whitelist.txt" Dim $lines = _FileCountLines($Location) Dim $whitelist[$lines] _FileReadToArray($Location,$Whitelist) HotKeySet("asd", "_exit") Opt("WinTitleMatchMode", 2) while 1 sleep(100) _getwinlist() For $x = 0 to UBound($array) -1 For $z = 1 to $whitelist[0] $result = StringInStr($array[$x], $Whitelist[$z]) if $result > 0 then WinClose($array[$x]) _Talk("Access Denied!") MsgBox(0, "Access Denied!", "Access to '"&$array[$x]&"' is denied!!"&@CRLF&"Please contact Volly if you need access to this application.") endif next Next WEnd Func _Talk($sText) $talk = ObjCreate("SAPI.SpVoice") $talk.Speak($sText) EndFunc func _exit() Exit EndFunc func _getwinlist() $array = _ArrayCreate("") $var = WinList() For $i = 1 to $var[0][0] If $var[$i][0] <> "" AND IsVisible($var[$i][1]) Then $adj = _ArrayAdd($array, $var[$i][0]) EndIf Next _ArrayDelete($array,0) EndFunc Func IsVisible($handle) If BitAnd( WinGetState($handle), 2 ) Then Return 1 Else Return 0 EndIf EndFunc Edited May 7, 2007 by Paulie Link to comment Share on other sites More sharing options...
Bert Posted May 7, 2007 Author Share Posted May 7, 2007 (edited) I came up with this, and it works for the most part. I'm having trouble with the hotkeyset. My script keeps wanting to restart. CODEexpandcollapse popup;asdfghjkl; #include <Array.au3> #include <Misc.au3> dim $var, $i, $array HotKeySet("^d", "_exit") Opt("WinTitleMatchMode", 2) if _Singleton ("asdfghjkl",1) = 0 then exit while 1 sleep(10) _getwinlist() For $x = 0 to UBound($array) -1 $result1 = StringInStr($array[$x], "SciTE") $result2 = StringInStr($array[$x], "Firefox") $result3 = StringInStr($array[$x], "Program Manager") $result4 = StringInStr($array[$x], "Help") if $result1 = 0 and $result2 = 0 and $result3 = 0 and $result4 = 0 then WinClose("") MsgBox(16 + 262144, "Access Denied!", "Please contact Volly if you need access to this application.",6) endif next WEnd Func _Talk($sText) $talk = ObjCreate("SAPI.SpVoice") $talk.Speak($sText) EndFunc func _exit() Exit sleep(1000) EndFunc func _getwinlist() $array = _ArrayCreate("") $var = WinList() For $i = 1 to $var[0][0] If $var[$i][0] <> "" AND IsVisible($var[$i][1]) Then $adj = _ArrayAdd($array, $var[$i][0]) EndIf Next _ArrayDelete($array,0) EndFunc Func IsVisible($handle) If BitAnd( WinGetState($handle), 2 ) Then Return 1 Else Return 0 EndIf EndFunc Edited May 7, 2007 by Volly The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
xcal Posted May 7, 2007 Share Posted May 7, 2007 (edited) I was messing with this for fun and came up with... expandcollapse popupHotKeySet('{esc}', 'quit') Opt('WinTitleMatchMode', 2) Global $filter[4] = [3, 'Notepad', 'cmd.exe', 'SciTE'] While 1 Sleep(1000) _enforcefilter() WEnd Func _enforcefilter() Local $winarray = _getwinlist() For $i = 1 To UBound($winarray) - 1 For $x = 1 To UBound($filter) - 1 If StringInStr($winarray[$i], $filter[$x]) Then $winarray[$i] = 'Allowed' Next Next For $i = 1 To UBound($winarray) - 1 If $winarray[$i] = 'Allowed' Then ContinueLoop WinClose($winarray[$i]) MsgBox(16, 'Error', 'You may not use: ' & $winarray[$i]) Next EndFunc Func _getwinlist() Local $wins = WinList(), $ret[1], $num = 0 For $i = 1 To $wins[0][0] If $wins[$i][0] <> '' And BitAND(WinGetState($wins[$i][1]), 2) Then ReDim $ret[$num + 1] $ret[$num] = $wins[$i][0] $num += 1 EndIf Next $ret[0] = UBound($ret) - 1 Return $ret EndFunc Func quit() Exit EndFunc edit - left in debugging stuff Edited May 7, 2007 by xcal How To Ask Questions The Smart Way Link to comment Share on other sites More sharing options...
Bert Posted May 8, 2007 Author Share Posted May 8, 2007 tried it. doesn't work. It keeps popping up the program manager as if I'm trying to shutdown. I add it to the list of allowed by changing the following line: Global $filter[6] = [3, 'Notepad', 'cmd.exe', 'SciTE', 'Firefox', 'Program manager'] Now it won't stop anything. Not sure why. The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
Bert Posted May 8, 2007 Author Share Posted May 8, 2007 Paulie - tries yours, and yours will close things on the whitelist. not keep them open. Need to give this some more thought. The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
xcal Posted May 8, 2007 Share Posted May 8, 2007 That's strange, mine worked for me. I'll see if I can figure why it might not work for others when I get a chance. How To Ask Questions The Smart Way Link to comment Share on other sites More sharing options...
Danny35d Posted May 8, 2007 Share Posted May 8, 2007 Give it a try.... #include <Misc.au3> #include <Array.au3> HotKeySet("^d", "_exit") If _Singleton("asdfghjkl", 1) = 0 Then Exit While 1 Sleep(10) _CloseThemUp("Firefox|Help|SciTe|White|Internet Explorer") WEnd Func _CloseThemUp($SLEAVEOPEN = "Program Manager") $EXCLUDE = StringSplit("Program Manager|Start|Start Menu", "|") If Not IsArray($SLEAVEOPEN) Then $SLEAVEOPEN = StringSplit($SLEAVEOPEN, '|') $SWINDOWS = WinList() For $I = 1 To $SWINDOWS[0][0] $CLOSE = True If $SWINDOWS[$I][0] <> "" And BitAND(WinGetState($SWINDOWS[$I][1]), 2) And _ArraySearch($EXCLUDE, $SWINDOWS[$I][0], 1) = -1 Then For $X = 1 To $SLEAVEOPEN[0] If StringInStr($SWINDOWS[$I][0], $SLEAVEOPEN[$X]) <> 0 Then $CLOSE = False Next If $CLOSE Then WinClose($SWINDOWS[$I][0]) MsgBox(16 + 262144, "Access Denied!", "Please contact Volly if you need access to " & $SWINDOWS[$I][0], 6) EndIf EndIf Next EndFunc ;==>_CloseThemUp Func _exit() Exit Sleep(1000) EndFunc ;==>_exit AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line Link to comment Share on other sites More sharing options...
xcal Posted May 8, 2007 Share Posted May 8, 2007 Hmm I can't see what's wrong with my function. I'm sure you know the logic of it, but here it is spelled out for the heck of it... Func _enforcefilter() ; gets a 1d array of window titles Local $winarray = _getwinlist () ; run through all titles For $i = 1 To UBound($winarray) - 1 ; compare against the filter For $x = 1 To UBound($filter) - 1 ; if a filter item is found in the array of windows, change the array entry to 'Allowed' If StringInStr($winarray[$i], $filter[$x]) Then $winarray[$i] = 'Allowed' Next Next ; run through the array of windows again, this time allowed entries are renamed to 'Allowed' For $i = 1 To UBound($winarray) - 1 ; if an entry is 'Allowed' skip it If $winarray[$i] = 'Allowed' Then ContinueLoop ; if an entry isn't named 'Allowed', we get to here, and it gets closed WinClose($winarray[$i]) MsgBox(16, 'Error', 'You may not use: ' & $winarray[$i]) Next EndFunc I did actually remove Opt('WinTitleMatchMode', 2) from my script, as I couldn't see the point of it. I tested it all again, and it worked perfectly on my setup. How To Ask Questions The Smart Way Link to comment Share on other sites More sharing options...
Bert Posted May 8, 2007 Author Share Posted May 8, 2007 (edited) I tested Danny's flavor of the script. His works well, but I'm going to make a small change where it looks at a ini file so I can update it more easily. Thanks everyone! Edit: Danny, the CPU hunger problem still exist. I've been thinking of how to address that, but the only way I know how to is to put in a sleep. That would impair performance as far as response time to close the window. Edited May 8, 2007 by Volly The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
Paulie Posted May 8, 2007 Share Posted May 8, 2007 I tested Danny's flavor of the script. His works well, but I'm going to make a small change where it looks at a ini file so I can update it more easily. Thanks everyone! Edit: Danny, the CPU hunger problem still exist. I've been thinking of how to address that, but the only way I know how to is to put in a sleep. That would impair performance as far as response time to close the window. Personally, I don't think it really matters.What could someone do on a computer with 1000 miliseconds or something. if the window is only open for a few seconds, i don't think it would reduce the effectiveness of the protection. Link to comment Share on other sites More sharing options...
Bert Posted May 8, 2007 Author Share Posted May 8, 2007 well, I did this, and it got the CPU problem down to a manageable level (around 6), though I like to have it down to 0 Func _CloseThemUp($SLEAVEOPEN = "Program Manager") $EXCLUDE = StringSplit("Program Manager|Start|Start Menu|Windows Task Manager", "|") If Not IsArray($SLEAVEOPEN) Then $SLEAVEOPEN = StringSplit($SLEAVEOPEN, '|') $SWINDOWS = WinList() For $I = 1 To $SWINDOWS[0][0] $CLOSE = True If $SWINDOWS[$I][0] <> "" And BitAND(WinGetState($SWINDOWS[$I][1]), 2) And _ArraySearch($EXCLUDE, $SWINDOWS[$I][0], 1) = -1 Then For $X = 1 To $SLEAVEOPEN[0] sleep(1) If StringInStr($SWINDOWS[$I][0], $SLEAVEOPEN[$X]) <> 0 Then $CLOSE = False endif Next If $CLOSE Then WinClose($SWINDOWS[$I][0]) MsgBox(16 + 262144, "Access Denied!", "Please contact Your system administrator if you need access to " & $SWINDOWS[$I][0], 6) EndIf EndIf Next EndFunc ;==>_CloseThemUp The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
Danny35d Posted May 8, 2007 Share Posted May 8, 2007 The CPU hunger problem still exist.My computer it is using 1% of CPU, but I have 1GB of RAM and 2.3 Ghz CPU. The only thing I can suggest is to use ReduceMemory() UDF by wOuter. ; Reduce memory usage ; Author wOuter ( mostly ) Func _ReduceMemory($i_PID = -1) If $i_PID <> -1 Then Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID) Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0]) DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0]) Else Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1) EndIf Return $ai_Return[0] EndFunc;==> _ReduceMemory() AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line Link to comment Share on other sites More sharing options...
Bert Posted May 9, 2007 Author Share Posted May 9, 2007 Mine has 2 gig and about the same processor. When I don't have the sleep in there, it is tacking around 94. I'll try ReduceMemory() to see what happens. It may fix another script I'm having trouble with. The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
erebus Posted May 9, 2007 Share Posted May 9, 2007 That's because you are polling the CPU too often. Try the Sleep() function with low values (i.e. 100 - 250) when facing such problems. Link to comment Share on other sites More sharing options...
Bert Posted May 10, 2007 Author Share Posted May 10, 2007 If you look in my previous post, I already did that. I'm looking for a way to solve the problem without doing that. When you add the sleep, you degrade performance, so that isn't the best solution. The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now