#!perl use strict; use Win32::API; use Win32::GuiTest qw(:ALL); use File::HomeDir; my $maxLimit = 30; #second my $cmdName = "notepad.exe"; my $counterFile = File::HomeDir->my_home."/counterFile.tmp"; if(! -e "$counterFile"){ system "ECHO 0 > \"$counterFile\""; } system "start ".$cmdName; #run command for testing #sleep 1; while(1){ my $counter = `CAT \"$counterFile\"`; #read counter from counterFile.tmp chomp $counter; print "Counter is $counter\n"; #To get foreground window handle my $hlWindow = GetForegroundWindow(); #========================================================================= #To get PID via windows API Win32::API::->Import("user32","DWORD GetWindowThreadProcessId( HWND hWnd, LPDWORD lpdwProcessId)") or die $^E; my $pidLPDWORDStruct = pack( "L", 0 ); GetWindowThreadProcessId($hlWindow, $pidLPDWORDStruct); my ($fwPid) = unpack ("L", $pidLPDWORDStruct); print "Foreground window PID is $fwPid\n"; #========================================================================= my @taskList = `tasklist.exe`; my $targetPid=0; my $i=0; foreach my $line (@taskList){ chomp $line; if($i > 4){ my ($imageName, $pid, $sessionName, $session, $memUsage) = split /\ +/, $line; if($imageName =~ $cmdName){ $targetPid = $pid; #save target PID if($pid == $fwPid){ print $imageName."[".$pid."] --- foreground\n"; system "ECHO 0 > \"$counterFile\""; #clear counter last; #break loop }else{ print $imageName."[".$pid."] --- background\n"; $counter++; system "ECHO $counter > \"$counterFile\""; #write to counterFile.tmp last; #break loop } } } $i++; } sleep(1); if($targetPid != 0 && $counter >= $maxLimit){ system "taskkill.exe /pid $targetPid"; #kill process system "ECHO 0 > \"$counterFile\""; } } exit 0;本人PO的原文
2013年8月14日 星期三
關閉ERP的程式
有時候ERP授權是有限制的,通常那些用ERP的人為了怕等待,所以就把視窗一直開著,這就是所謂的「佔著毛坑不拉屎」的行為,導致後來要用的人根本連不上去,底下就是本人為了解決這個問題而試著寫的程式,當然路不只有一條。
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言