AutoHotkey Community

It is currently May 27th, 2012, 9:16 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 42 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: February 19th, 2006, 7:34 am 
Offline

Joined: February 19th, 2006, 7:31 am
Posts: 8
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 19th, 2006, 12:17 pm 
Offline

Joined: August 24th, 2005, 5:17 pm
Posts: 1237
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/viewtop ... 2&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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 19th, 2006, 12:49 pm 
Offline

Joined: December 30th, 2005, 5:01 pm
Posts: 448
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.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: February 21st, 2006, 3:46 am 
Offline

Joined: February 19th, 2006, 7:31 am
Posts: 8
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 :)

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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 21st, 2006, 12:46 pm 
Offline

Joined: December 30th, 2005, 5:01 pm
Posts: 448
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 21st, 2006, 12:59 pm 
Offline

Joined: August 24th, 2005, 5:17 pm
Posts: 1237
Try figuring out how to close one specific program first and then building up from there.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 21st, 2006, 1:16 pm 
Offline

Joined: November 8th, 2004, 12:46 am
Posts: 1271
*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
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2006, 2:38 am 
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. :/


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2006, 2:43 am 
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. :/


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2006, 2:51 am 
Offline

Joined: August 24th, 2005, 5:17 pm
Posts: 1237
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2006, 2:51 am 
Offline

Joined: November 8th, 2004, 12:46 am
Posts: 1271
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
Image


Last edited by Serenity on February 22nd, 2006, 3:32 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2006, 3:24 am 
It's an iPod that i am running from i changed the code to this and it still has no effect :(


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
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2006, 3:35 am 
Offline

Joined: November 8th, 2004, 12:46 am
Posts: 1271
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
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2006, 3:49 am 
Offline

Joined: February 19th, 2006, 7:31 am
Posts: 8
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2006, 4:31 am 
Offline

Joined: February 19th, 2006, 7:31 am
Posts: 8
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 :)

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


Last edited by selioneru on February 22nd, 2006, 4:59 am, edited 3 times in total.

Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 42 posts ]  Go to page 1, 2, 3  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], BrandonHotkey, Google Feedfetcher and 65 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group