Get full Filename of running processes (solved)

Just starting out? Need help? Post your questions and find answers here.
User avatar
Michael Vogel
Addict
Addict
Posts: 2807
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Get full Filename of running processes (solved)

Post by Michael Vogel »

Hi,

I'd like to write a "Shutdown" program for my USB memory stick, but before "ejecting" the stick I need to kill all programs, which have been started directly from the stick...

I found a program which lists all running processes, but I would need the information from which drive they have been started. How to do that?

What I tried, does not work:

Code: Select all

handle=FindWindow_(0,0)
handle=GetWindow_(handle,#GW_HWNDFIRST)

While handle<>0
	name.s = Space(128)
	GetWindowText_(handle,@name,127)
	
	path.s = Space(#MAX_PATH+1)
GetModuleFileName_(GetWindowLong_(handle,#GWL_HINSTANCE),@path,#MAX_PATH)
	Debug Hex(handle)+" - "+path
				
	If Len(name)
		;MessageBox_(0,name,"Information",#MB_ICONERROR| #MB_OK)
		Debug name+"."
		If killautohotkey
		If FindString(LCase(name),"autohotkey v",1)
			Debug ">> Autohotkey found"
			PostMessage_(handle, #WM_CLOSE, 0, 0)
			Debug ">> Autohotkey killed"
		EndIf
		EndIf
	EndIf
	handle=GetWindow_(handle,#GW_HWNDNEXT)
	
	
Wend
Last edited by Michael Vogel on Thu Jun 29, 2006 1:08 pm, edited 1 time in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

One simple way to do it would be to write a little shell for the stick that starts/shutsdown any programs on it. A popup menu running in the system tray, maybe. Anyway, the shell would always know what was running from the stick and when you pulled it out it would shut down those processes. It could even autostart selected programs from the stick upon sensing its presence.
BERESHEIT
User avatar
Michael Vogel
Addict
Addict
Posts: 2807
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

Good idea, netmaestro, but my shell is also started from the stick and cannot be killed before the same shell program should eject the USB stick...

There is also a problem when I would start something "manually" and not from the shell.

What I did for now is to seperate the shell (the "runner") from a second program (the "killer") which copies itself automatically to the harddisk, so it can not lock the USB while running...

It must be possible to see the full path (e.g. http://www.teamcti.com/pview/PrcView_5_2_15.zip), but it seems that I do not get the right handle for the GetModuleFileName_() command...
xgp
Enthusiast
Enthusiast
Posts: 128
Joined: Mon Jun 13, 2005 6:03 pm

Post by xgp »

This has already been posted here, i think by freak.
Anyway, here is a sample on how to get the process path from a window handle.
(Note: It won't run on 9x/Me, because they do not have the psapi.dll library)

Update: Read again your post and i think, this can do the job.

Code: Select all

Declare.s ProcessNameFromHwnd(hwnd)
Declare   CheckWindowsProcess(WndHwnd, p)


Global  USB_ROOT.s  = "F:\"   ;  the root of your USB stick

; assuming that all applications started on your USB stick have a window, we could enumerate all windows
; and send a #wm_close message to the ones that have started on your USB stick.

Procedure CheckWindowsProcess(WndHwnd, p)
  
  ; check if the first third caracters of the process path are the USB_ROOT.s
  Protected ProcessPath.s = ProcessNameFromHwnd(WndHwnd)
  Protected Caption.s = Space(255)
  
  
  If UCase(Mid(ProcessPath, 1, 3)) = UCase(USB_ROOT)
    
    GetWindowText_(WndHwnd, @Caption, 255)
    Debug "Title: " + Caption
    Debug "Path:  " + ProcessPath
    Debug "Hwnd:  " + Str(WndHwnd)
    Debug "---------------------------"
    
    ; here, as you already have the Window Handle, you could send the #wm_close message
    
  EndIf
  
  ProcedureReturn #True   ; return 1 to continue the enumeration
EndProcedure
Procedure.s ProcessNameFromHwnd(hwnd)
  
  Protected   ProcessID
  Protected   hProcess
  Protected   hModule
  Protected   ProcessName.s   =   Space(#MAX_PATH)
  Protected   EnumProcessModules
  Protected   GetModuleFileNameEx
  
  If  OpenLibrary(0, "psapi.dll")
    EnumProcessModules  =   GetFunction(0, "EnumProcessModules")
    GetModuleFileNameEx =   GetFunction(0, "GetModuleFileNameExA")
    
    GetWindowThreadProcessId_(hwnd, @ProcessID)
    hProcess    =   OpenProcess_(#PROCESS_QUERY_INFORMATION|#PROCESS_VM_READ, 0, ProcessID)
    
    CallFunctionFast(EnumProcessModules, hProcess, @hModule, 1, 0)
    CallFunctionFast(GetModuleFileNameEx, hProcess, hModule, @ProcessName, #MAX_PATH)
    
    CloseHandle_(hProcess)
    CloseLibrary(0)
  EndIf
  
  ProcedureReturn ProcessName
EndProcedure 


Debug "applications started on " + USB_ROOT
Debug "------------------------------------"
EnumWindows_(@CheckWindowsProcess(), 1)
Debug "------------------------------------"
Debug "end"
Hope this helps.
User avatar
Michael Vogel
Addict
Addict
Posts: 2807
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

Thanks xgp,
I didn't find that in the forum - that's exact what I need!
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Get full Filename of running processes (solved)

Post by NoahPhense »

How about my lib?

http://www.liquidbuzz.com/pb/libs/

- np
Post Reply