AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Close all programs running from...
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
selioneru



Joined: 19 Feb 2006
Posts: 8

PostPosted: Sun Feb 19, 2006 7:34 am    Post subject: Close all programs running from... Reply with quote

Hey guys i was wondering if there was any way to get a script to close all the programs running from a drive with a specific drive label. Any help would be appreciated!
Back to top
View user's profile Send private message
evl



Joined: 24 Aug 2005
Posts: 1239

PostPosted: Sun Feb 19, 2006 12:17 pm    Post subject: Reply with quote

Not sure about the drive label, but these may be helpful:

The full path (including drive letter) can be found using shimanov's code posted here:
http://www.autohotkey.com/forum/viewtopic.php?t=4182&start=15

You can use "WinGet,..., List..." to get a list of all windows (I don't think there's a command to retrieve all running processes though, so it would have to have a window (might have a hidden window sometimes).

You can see this thread for some different ways of closing processes/windows:
http://www.autohotkey.com/forum/viewtopic.php?t=8086
Back to top
View user's profile Send private message
Peter



Joined: 30 Dec 2005
Posts: 279

PostPosted: Sun Feb 19, 2006 12:49 pm    Post subject: Reply with quote

You can follow something like this:
1. DriveGet, OutputVar, list ;list with all drives
2. DriveGet, OutputVar, label, Drive ;check which drive has your wanted label
3. Get all processes. Have a look here. Or use the by evl suggested WinGet, ... List and PID(The Winget will be enough in many cases)
4. Get drive of processes with Shimanov script as suggested by evl
5. Close all processes that match with 2.
Back to top
View user's profile Send private message
selioneru



Joined: 19 Feb 2006
Posts: 8

PostPosted: Tue Feb 21, 2006 3:46 am    Post subject: i Really haven't done this before :( Reply with quote

i'd like to learn but i haven't ever done anything like this before so i guess you'll need to spell it out to me a little more. Would i have to set the Drive With the correct label to a variable and then close all applications like that?



Here is my code so far. I have never coded before so i am pretty much hopeless, but i'd like to learn Smile

Code:
DriveGet, OutputVar, list
DriveGet, OutputVar, label, "Koju's Ipod"
Process, Exist
pid := ErrorLevel

MsgBox, % "pid = " pid "`n`nname = " GetModuleFileNameEx( pid )
return

GetModuleFileNameEx( p_pid )
{
   if A_OSVersion in WIN_95,WIN_98,WIN_ME
   {
      MsgBox, This Windows version (%A_OSVersion%) is not supported.
      return
   }

   /*
      #define PROCESS_VM_READ           (0x0010)
      #define PROCESS_QUERY_INFORMATION (0x0400)
   */
   h_process := DllCall( "OpenProcess", "uint", 0x10|0x400, "int", false, "uint", p_pid )
   if ( ErrorLevel or h_process = 0 )
   {
      MsgBox, [OpenProcess] failed
      return
   }
   
   name_size = 255
   VarSetCapacity( name, name_size )
   
   result := DllCall( "psapi.dll\GetModuleFileNameExA", "uint", h_process, "uint", 0, "str", name, "uint", name_size )
   if ( ErrorLevel or result = 0 )
      MsgBox, [GetModuleFileNameExA] failed
   
   DllCall( "CloseHandle", h_process )
   
   return, name
}


I'd like it to close all of the applications running from that drive in a safe manner (inorder for settings and whatnot to be saved) .

Thanks for the help!
Back to top
View user's profile Send private message
Peter



Joined: 30 Dec 2005
Posts: 279

PostPosted: Tue Feb 21, 2006 12:46 pm    Post subject: Reply with quote

selioneru wrote:
I have never coded before so i am pretty much hopeless
No offence, but I would start with something much more easy then.
Back to top
View user's profile Send private message
evl



Joined: 24 Aug 2005
Posts: 1239

PostPosted: Tue Feb 21, 2006 12:59 pm    Post subject: Reply with quote

Try figuring out how to close one specific program first and then building up from there.
Back to top
View user's profile Send private message
Serenity



Joined: 08 Nov 2004
Posts: 1277

PostPosted: Tue Feb 21, 2006 1:16 pm    Post subject: Reply with quote

*untested* ;)

Code:
; first get the drive letter of "MyDrive":

driveget, list, list, removable ; usb devices

stringtrimleft, list, list, 1 ; remove drive A
stringlen, len, list

loop, %len%
{
  stringleft, drive, list, 1
 
  driveget, label, label, %drive%:

  if label = MyDrive ; specify the label of your drive here
    mydrive = %drive%:
     
  stringtrimleft, list, list, 1
}

; now get list of running programs:

WS_EX_APPWINDOW = 0x40000
WS_EX_TOOLWINDOW = 0x80
GW_OWNER = 4

WinGet, list, List

loop, %list%
{
   wid := list%A_Index%
   
   WinGet, es, ExStyle, ahk_id %wid%
   
   if ( ( ! DllCall( "GetWindow", "uint", wid, "uint", GW_OWNER ) and ! ( es & WS_EX_TOOLWINDOW ) )
         or ( es & WS_EX_APPWINDOW ) )
   {
      WinGet, pid, PID, ahk_id %wid%
     
      name := GetModuleFileNameEx( pid )
                 
      ; use splitpath to obtain drive letter of each app:     
      SplitPath, name, , , , , Drive
     
      ; close the program if drive matches:
      if Drive = mydrive
        WinClose, ahk_pid %pid%
     
   }
}
return


GetModuleFileNameEx( p_pid )
{
   if A_OSVersion in WIN_95,WIN_98,WIN_ME
   {
      MsgBox, This Windows version (%A_OSVersion%) is not supported.
      return
   }

   /*
      #define PROCESS_VM_READ           (0x0010)
      #define PROCESS_QUERY_INFORMATION (0x0400)
   */
   h_process := DllCall( "OpenProcess", "uint", 0x10|0x400, "int", false, "uint", p_pid )
   if ( ErrorLevel or h_process = 0 )
   {
      MsgBox, [OpenProcess] failed
      return
   }
   
   name_size = 255
   VarSetCapacity( name, name_size )
   
   result := DllCall( "psapi.dll\GetModuleFileNameExA", "uint", h_process, "uint", 0, "str", name, "uint", name_size )
   if ( ErrorLevel or result = 0 )
      MsgBox, [GetModuleFileNameExA] failed
   
   DllCall( "CloseHandle", h_process )
   
   return, name
}

_________________
"Anything worth doing is worth doing slowly." - Mae West
Back to top
View user's profile Send private message Visit poster's website
Guest






PostPosted: Wed Feb 22, 2006 2:38 am    Post subject: Reply with quote

Thanks for the help Serenity! but... it doesn't seem to work i have changed all instances of "mydrive" to the label i have but it doesn't seem to work. the script just pops up on the task bar and then disappears. :/
Back to top
Guest






PostPosted: Wed Feb 22, 2006 2:43 am    Post subject: Reply with quote

Thanks for the help Serenity! but... it doesn't seem to work i have changed all instances of "mydrive" to the label i have but it doesn't seem to work. the script just pops up on the task bar and then disappears. :/
Back to top
evl



Joined: 24 Aug 2005
Posts: 1239

PostPosted: Wed Feb 22, 2006 2:51 am    Post subject: Reply with quote

You only needed to change one instance of MyDrive since the others are a variable:
Code:

if label = MyDrive ; specify the label of your drive here


Also:
Code:

driveget, list, list, removable ; usb devices

Only gets removable drives - not sure if that's what you needed.
Back to top
View user's profile Send private message
Serenity



Joined: 08 Nov 2004
Posts: 1277

PostPosted: Wed Feb 22, 2006 2:51 am    Post subject: Reply with quote

Perhaps you are not using a removable drive? See the first line:
driveget, list, list, removable ; usb devices

The following code should ascertain which letter your drive is assigned, it searches for all drives not just removable:

Code:
driveget, list, list ; get all drives on filesystem
msgbox, %list%

; now parse the list of drives, checking each one for its label
; if label matches, assign its letter to the "MyDrive" variable

stringlen, len, list
loop, %len%
{
  stringleft, drive, list, 1
  driveget, label, label, %drive%:
  if label = MyDrive ; specify the label of your drive here
    mydrive = %drive%:
   
  stringtrimleft, list, list, 1
}
msgbox, %MyDrive% is assigned to drive %mydrive%

_________________
"Anything worth doing is worth doing slowly." - Mae West


Last edited by Serenity on Wed Feb 22, 2006 3:32 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
Guest






PostPosted: Wed Feb 22, 2006 3:24 am    Post subject: Reply with quote

It's an iPod that i am running from i changed the code to this and it still has no effect Sad


Quote:

; first get the drive letter of "MyDrive":

driveget, list, list ; usb devices

stringtrimleft, len, list, 1 ; remove drive A
stringlen, len, list

loop, %len%
{
stringleft, drive, list, 1

driveget, label, label, %drive%:

if label = Koju's Ipod ; specify the label of

your drive here
mydrive = %drive%:

stringtrimleft, list, list, 1
}

; now get list of running programs:

WS_EX_APPWINDOW = 0x40000
WS_EX_TOOLWINDOW = 0x80
GW_OWNER = 4

WinGet, list, List

loop, %list%
{
wid := list%A_Index%

WinGet, es, ExStyle, ahk_id %wid%

if ( ( ! DllCall( "GetWindow", "uint", wid,

"uint", GW_OWNER ) and ! ( es & WS_EX_TOOLWINDOW )

)
or ( es & WS_EX_APPWINDOW ) )
{
WinGet, pid, PID, ahk_id %wid%

name := GetModuleFileNameEx( pid )

; use splitpath to obtain drive letter of

each app:
SplitPath, name, , , , , Drive

; close the program if drive matches:
if Drive = mydrive
WinClose, ahk_pid %pid%

}
}
return


GetModuleFileNameEx( p_pid )
{
if A_OSVersion in WIN_95,WIN_98,WIN_ME
{
MsgBox, This Windows version (%A_OSVersion%)

is not supported.
return
}

/*
#define PROCESS_VM_READ (0x0010)
#define PROCESS_QUERY_INFORMATION (0x0400)
*/
h_process := DllCall( "OpenProcess", "uint",

0x10|0x400, "int", false, "uint", p_pid )
if ( ErrorLevel or h_process = 0 )
{
MsgBox, [OpenProcess] failed
return
}

name_size = 255
VarSetCapacity( name, name_size )

result := DllCall(

"psapi.dll\GetModuleFileNameExA", "uint",

h_process, "uint", 0, "str", name, "uint",

name_size )
if ( ErrorLevel or result = 0 )
MsgBox, [GetModuleFileNameExA] failed

DllCall( "CloseHandle", h_process )

return, name
}
Back to top
Serenity



Joined: 08 Nov 2004
Posts: 1277

PostPosted: Wed Feb 22, 2006 3:35 am    Post subject: Reply with quote

Can you verify that the DriveGet section works? (See my previous post). Another thing, some programs do not respond to WinClose, so you might have to use Process, Close instead. You could check if the script works by replacing the section that uses WinClose with a msgbox::

Code:
 ; close the program if drive matches:
if Drive = mydrive
msgbox, %name%`n %pid% ; WinClose, ahk_pid %pid%

_________________
"Anything worth doing is worth doing slowly." - Mae West
Back to top
View user's profile Send private message Visit poster's website
selioneru



Joined: 19 Feb 2006
Posts: 8

PostPosted: Wed Feb 22, 2006 3:49 am    Post subject: Reply with quote

hmm i edited it and added the code there and no message box pops up though i know it should :^X

Quote:


if label = Koju's IPOD ; specify the label of your drive here
mydrive = %drive%:

stringtrimleft, list, list, 1
}

msgbox, %MyDrive% is assigned to drive %mydrive%


I added this code and it makes the letter of the drive pop up, but that's all it does. It must be somthing with the process list?
Back to top
View user's profile Send private message
selioneru



Joined: 19 Feb 2006
Posts: 8

PostPosted: Wed Feb 22, 2006 4:31 am    Post subject: Reply with quote

i just pasted this piece of code in

Quote:


; close the program if drive matches:
if MyDrive = %drive%
msgbox, %name%`n %pid% ,
WinClose, ahk_pid %pid%


that didn't work then i realized that it was commented so i changed it to this
Quote:

close the program if drive matches:
if MyDrive = %drive%
msgbox, %name%`n %pid% , WinClose, ahk_pid %pid%



it closes some of the programs but not any of the one's in the system tray, that's a slight problem but it's progress Smile

Btw thanks a ton for the help so far Serenity, evl ! you guys rock!


Last edited by selioneru on Wed Feb 22, 2006 4:59 am; edited 3 times in total
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group