Page 1 of 1

The Runner

Posted: Sun Apr 30, 2006 7:48 pm
by Michael Vogel
Here's a small tool (nice for USB-Sticks) which can be assigned to a shortcut (e.g. <windows>-x), it executes selected (and self configured) programs by typing some significant letters...


What you have to do before running it:

1) take a nice 16x16 icon and colorize it, so you will have 4 different icons in red, orange, green and white (I made a small arrow for me)

2) enter commonly used directories in the first DataSection (as I use 'The Runner' on my USB stick, the program checks, if it has been started from my USB-Stick, please adapt it's name - the drive letter will replace all "*" in the directories you entered above)

3) enter your programs, starting with some chars, a program description, the exe name, parameters and it's starting directory (see the examples in the second DataSection)

4) Compile it and start it...

You will see a small input field, where you can enter the program names, if you test it with the example code, just enter:

* 'p?' - you will see all entries starting with 'p'
* delete the '?' and complete to...
* ...'pur' - you will see "Pure Basic" and if you wait for a second, Purebasic is started (if you have it in the same directory I have)
* ...'pure' - this would have opened the source directory of Purebasic

Michael



Code: Select all

; Define
	Declare.c FirstChar(string.s)
; EndDefine
Procedure Init()

	USBDrive=FirstChar(PeekS(GetCommandLine_())); 		...Runner vom USB-Stick gestartet?

	If USBDrive='C';											...wohl nicht, also "händisch" prüfen...
		USBDrive='H';											...von "H" beginnend ("G", "F", "E",...) wird's wohl reichen...
		VolumeLabel.s=Space(64)
		Repeat
			If GetVolumeInformation_(Chr(USBDrive)+":\",@VolumeLabel,64,#Null,#Null,#Null,#Null,#Null)
				If LCase(volumelabel)="usb-stick name"  ;### Configure that ###
					Break
				EndIf
			EndIf
			USBDrive-1
		Until USBDrive='C'
	EndIf


	#MaxShortcuts=20;										... maximale Zahl von Kürzeln
	#MaxPrograms=100;										... maximale Zahl von Programmeinträgen
	#WinH=36
	#WinW=142

	Global Dim Shortcut.s(#MaxShortcuts,1)
	Global Dim Program.s(#MaxPrograms,4)

	Global i
	Global Shortcuts=0;										... Kürzel einlesen...
	Repeat
		Shortcuts+1
		Read Shortcut(Shortcuts,0)
		Read Shortcut(Shortcuts,1)
		Shortcut(Shortcuts,1)=ReplaceString(Shortcut(Shortcuts,1),"*",Chr(USBDrive))
		;Debug Shortcut(Shortcuts,1)
	Until Shortcut(Shortcuts,0)="*"

	Global Programs=0;										... Programme einlesen...
	Repeat
		Programs+1
		Read Program(Programs,0)
		Read Program(Programs,1)
		Read Program(Programs,2)
		Read Program(Programs,3)
		Read Program(Programs,4)

		i=Shortcuts;												... Kürzel expandieren...
		Repeat
			i-1
			Program(Programs,2)=ReplaceString(Program(Programs,2),Shortcut(i,0),Shortcut(i,1))
			Program(Programs,3)=ReplaceString(Program(Programs,3),Shortcut(i,0),Shortcut(i,1))
			Program(Programs,4)=ReplaceString(Program(Programs,4),Shortcut(i,0),Shortcut(i,1))
		Until i=1
		If Len(Program(Programs,4)) And Right(Program(Programs,4),1)<>"\"
			Program(Programs,4)+"\"
		EndIf
		;Debug Program(Programs,2)
		;Debug Program(Programs,4)
	Until Program(Programs,0)="*"


	DataSection;### Configure that ###
		; Shortcut, Expandierter Text
		Data.s "<U>","*:"
		Data.s "<T>","*:\Programs\Tools\"
		Data.s "<V>","*:\Programs\"
		Data.s "<D>","C:\Dokumente und Einstellungen\vo\Eigene Dateien"
		Data.s "<O>","C:\Dos\Win\"
		Data.s "<I>","C:\Daten\Infos\"
		Data.s "<P>","C:\Programme\"
		Data.s "<W>","C:\Win\"
		Data.s "<Z>","C:\Programme\Zubehör\"
		Data.s "*","*"
	EndDataSection

	DataSection;### Configure that ###
		; Kürzel, Name, Programm,Parameter, Pfad
		Data.s "fox","Foxit Reader","<V>Tools\Viewer\Foxit Reader.exe","",""
		Data.s "fs","FastStone Viewer","<V>Tools\Viewer\FSViewer.exe","",""
		Data.s "neo","NeoMem","<V>NeoMem\NeoMem.exe <V>NeoMem\Getronics.neo","",""
		Data.s "tom","TomeRaider","<V>TomeRaider\TomeRaider.exe","",""
		Data.s "max","Maxthon Browser","<V>Tools\Internet\Maxthon.exe","",""
		Data.s "vv","Ultra VNC Viewer","<V>Tools\Internet\VNC Viewer.exe","",""
		Data.s "vs","Ultra VNC Server","<V>Tools\Internet\VNC Server.exe","",""
		Data.s "xy","XYplorer","<V>Tools\XYplorer\XYplorer.exe","",""
		Data.s "mem","Memory","<V>Games\Memory.Exe","",""
		Data.s "quiz","Quiz","Quiz.Exe","","<V>Games"
		Data.s "su","Sudoku","<V>Games\Sudoku.Exe","",""
		Data.s "take","Take1","Take1.Exe","","<V>Games"
		Data.s "pur","Pure Basic","PureBasic.exe","","<Z>PureBasic"
		Data.s "pure","Pure Basic Sources","Examples","","<Z>PureBasic"
		Data.s "c","Console Calculator","<V>Math\Calc\Ccalc.exe","",""
		Data.s "hp","Free42","<V>Math\Calc\Free42.exe","",""
		Data.s "pl","Process Mate","<V>Tools\System\ProcessMate.exe","",""
		Data.s "sl","Safarp","<V>Tools\System\Safarp.exe","",""
		Data.s "snif","Smart Sniffer","<V>Tools\System\SmSniff.exe","",""
		Data.s "tree","TreeSize","<V>Tools\System\Treesize.exe <U>\","",""
		Data.s "lex","Brockhaus","PCBib_Bi.Exe","","<I>Langenscheidt"
		Data.s "wiki","Wikipedia","TomeRaider.exe","Books\Wikipedia.tr3 TomeRaider","<I>Tomeraider"
		Data.s "ping","VPing Utility","VPing.exe","*","<T>Cool Tools"
		Data.s "xon","TrueCrypt on","TrueCrypt.exe","/q /e /l x /a /v TrueCrypt.dev","<V>Tools\TrueCrypt"
		Data.s "xoff","TrueCrypt off","<V>Tools\TrueCrypt\TrueCrypt.exe","/q /d",""
		Data.s "*","","","","",""
	EndDataSection


	DataSection
		Icon0: IncludeBinary "red.ico"
		Icon1: IncludeBinary "orange.ico"
		Icon2: IncludeBinary "green.ico"
		Icon3: IncludeBinary "white.ico"
	EndDataSection

	Global Dim Icon(3)

	Icon(0)=CatchImage(0,?Icon0)
	Icon(1)=CatchImage(1,?Icon1)
	Icon(2)=CatchImage(2,?Icon2)
	Icon(3)=CatchImage(3,?Icon3)

EndProcedure
Procedure.c FirstChar(string.s)
	Protected char
	char=PeekB(@string)
	If char=34 : char=PeekB(@string+1) : EndIf
	ProcedureReturn char
EndProcedure
Procedure.l FirstProgram(string.s)

	Protected i=Programs

	Repeat
		i-1
		If string=Program(i,0)
			ProcedureReturn i
		EndIf
	Until i=1

	ProcedureReturn 0
EndProcedure
Procedure Main()
	Protected quit=0;			Schleife
	Protected oldstart=-1;	gemerkte Programmwahl
	Protected start;				aktuelle gewähltes Programm
	Protected timer;				Wartezähler
	Protected icon=0;			Bildnummer
	Protected oldicon=-1;		gemerkte Bildnummer
	Protected list1.s,list2.s;	Programmlisten
	Protected input.s;			aktuelle Eingabe
	Protected oldinput.s;		letzte Eingabe mit "?"
	Protected l,r;					Hilfsvariablen


	Init()
	Protected Font=LoadFont(0,"Tahoma",7)

	win=OpenWindow(1,0,0,#WinW,#WinH+150,"Runner...",#PB_Window_BorderLess|#PB_Window_Invisible|#WS_THICKFRAME|#PB_Window_ScreenCentered)
	SmartWindowRefresh(1,#True)
	SetWindowLong_(win,#GWL_EXSTYLE,GetWindowLong_(hide,#GWL_EXSTYLE)|#WS_EX_TOOLWINDOW); Fenster nicht in Taskleiste anzeigen
	SetWindowPos_(win,#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE|#SWP_FRAMECHANGED|#SWP_NOZORDER); Fenster zurechtrücken (Update)
	CreateGadgetList(win)
	StringGadget(1,22,0,120,20,"")
	SetGadgetFont(#PB_Default,Font)
	TextGadget(2,24,23,120,16,"")
	TextGadget(3,24,40,120,999,"")
	TextGadget(4,50,40,120,999,"")

	SetActiveGadget(1)

	;kann keine transparenten Flächen...
	;ImageGadget(9,0,0,20,20,Icon(0));,#PB_Image_Border)
	;SetGadgetState(9,Icon(0))

	ResizeWindow(1,#PB_Ignore,#PB_Ignore,#PB_Ignore,#WinH)
	SetTimer_(win,1,100,0)
	StickyWindow(1,1)
	HideWindow(1,0)

	Repeat

		If icon<>oldicon
			oldicon=icon
			DCwin=StartDrawing(WindowOutput(1))
			DrawIconEx_(DCwin,2,2,Icon(icon),0,0,0,#Null,#DI_NORMAL|#DI_COMPAT)
			StopDrawing()
		EndIf

		Select WaitWindowEvent()

		Case #WM_TIMER
			input=LCase(GetGadgetText(1))
			oldstart=start
			start=FirstProgram(input)

			If start
				ResizeWindow(1,#PB_Ignore,#PB_Ignore,#PB_Ignore,#WinH)
				If start<>oldstart
					SetGadgetText(2,Program(start,1))
					SetGadgetText(3,"")
					SetGadgetText(4,"")

					timer=0
					icon=0
				Else
					timer+1
					Select timer
					Case 4
						icon=1
					Case 8
						icon=2
					Case 10
						quit=999
					EndSelect
				EndIf

			ElseIf Right(input,1)="?"
				If input<>oldinput
					oldinput=input
					SetGadgetText(2,"Display Programs...")

					l=Len(input)-1
					input=Left(input,l)

					list1=""
					list2=""
					i=1
					r=0
					While i<Programs
						If Left(Program(i,0),l)=input
							r+1
							list1+Program(i,0)+#CRLF$
							list2+Program(i,1)+#CRLF$
						EndIf
						i+1
					Wend
					ResizeWindow(1,#PB_Ignore,#PB_Ignore,#PB_Ignore,#WinH+r*11+10)
					SetGadgetText(3,list1)
					SetGadgetText(4,list2)

				EndIf

			Else
				ResizeWindow(1,#PB_Ignore,#PB_Ignore,#PB_Ignore,#WinH)
				SetGadgetText(2,"Programmkürzel eingeben")
				SetGadgetText(3,"")
				SetGadgetText(4,"")
				timer=0
				icon=0
				oldinput=""
			EndIf

		Case #WM_CHAR
			Select EventwParam()
			Case 27
				quit=#True
			Case 13
				input=LCase(GetGadgetText(1))
				If Right(input,1)="?"
					input=Left(input,Len(input)-1)
				EndIf
				oldstart=start
				start=FirstProgram(input)
				quit=1000
			EndSelect

		EndSelect

		If quit>666
			If start>0
				;SetErrorMode_(#SEM_NOOPENFILEERRORBOX)
				If RunProgram(Program(start,4)+Program(start,2),Program(start,3),Program(start,4))
					quit=#True
				ElseIf quit=1000; bei Enter immer Aussteigen...
					quit=#True
				Else
					SetGadgetText(2,"Fehler beim Starten...")
					quit=0
					icon=3
				EndIf
			ElseIf quit=1000; bei Enter immer Aussteigen...
				quit=#True
			EndIf
		EndIf

	Until quit=#True

EndProcedure

Main()

Posted: Sun Apr 30, 2006 9:07 pm
by HAnil
very nice tool.
Thanks for sharing.

The Killer

Posted: Tue May 02, 2006 8:59 am
by Michael Vogel
Ok, so you get the second part for it - "The Killer"...

As I'm using "The Runner" to start my programs from an USB stick, I needed a tool which makes something like a "Shutdown" for the USB stick as well, it should...

...close applications started from the stick (e.g. AutoHotkey)
...close encrypted devices on the stick (done by Truecrypt)
..."eject" the stick

The last point is a (small) problem, a program started from the stick can't eject it - so the program copies itself to the harddisk and start from there... (applause!)

(the USB ejecting routine is from "bingo", thanks for that)

To use "Killer.exe" with the "Runner", just enter a data line like...
data.s "shut","Do a USB shutdown","Killer.exe","* ahk tcr run usb","<U>"

Code: Select all

; Define

	Global win
	Global WindowOpen=#False
	Global Anzahl
	Global First

	DataSection
		Icon0: IncludeBinary "icons\red.ico"
		Icon1: IncludeBinary "icons\orange.ico"
;		Icon2: IncludeBinary "icons\green.ico"
		Icon3: IncludeBinary "icons\white.ico"
	EndDataSection

	Global Dim Icon(3)

	Icon(0)=CatchImage(0,?Icon0)
	Icon(1)=CatchImage(1,?Icon1)
;	Icon(2)=CatchImage(2,?Icon2)
	Icon(3)=CatchImage(3,?Icon3)

; EndDefine

Procedure.s AddSlash(string.s)
	If Len(string)
		If Right(string,1)<>"\"
			string+"\"
		EndIf
	EndIf
	ProcedureReturn string
EndProcedure
Procedure.c FirstChar(string.s)
	Protected char
	char=PeekB(@string)
	If char=34 : char=PeekB(@string+1) : EndIf
	ProcedureReturn char
EndProcedure
Procedure Tool_EjectUSB()
	#DIGCF_PRESENT=2
	#DIGCF_ALLCLASSES=4
	#SPDRP_SERVICE = 4

	Protected hDeviceInfoSet=SetupDiGetClassDevs_(0,0,0,#DIGCF_PRESENT|#DIGCF_ALLCLASSES)

	Structure SP_DEVINFO_DATA
		cbSize.l
		ClassGuid.GUID
		DevInst.l
		Reserved.l
	EndStructure

	Global DeviceInfoData.SP_DEVINFO_DATA
	DeviceInfoData\cbSize=SizeOf(DeviceInfoData)

	Protected *MemoryID = AllocateMemory(500)
	Protected i.l
	Protected devinst

	OpenLibrary(1,"setupapi.dll")

	Protected *F1 = GetFunction(1,"SetupDiGetDeviceInstanceIdA")
	Protected *F2 = GetFunction(1,"SetupDiGetDeviceRegistryPropertyA")
	Protected *F3 = GetFunction(1,"CM_Locate_DevNodeA")
	Protected *F4 = GetFunction(1,"CM_Request_Device_Eject_ExA")

	While SetupDiEnumDeviceInfo_(hDeviceInfoSet,i,@DeviceInfoData)
		i+1
		CallFunctionFast(*F2,hDeviceInfoSet,DeviceInfoData,#SPDRP_SERVICE,0,*MemoryID,500,0)
		;Debug UCase(PeekS(*MemoryID))
		If UCase(PeekS(*MemoryID)) = "USBSTOR"
			CallFunctionFast(*F1,hDeviceInfoSet,DeviceInfoData,*MemoryID,500,0)
			If CallFunctionFast(*F3,@devinst,PeekS(*MemoryID),0) = 0
				CallFunctionFast(*F4,devinst,0,0,0,0,0)
			EndIf
		EndIf
	Wend

	SetupDiDestroyDeviceInfoList_(hDeviceInfoSet)
	CloseLibrary(1)
	FreeMemory(*MemoryID)
EndProcedure
Procedure Tool_KillProgram(program.s)
	Protected oldhandle
	Protected handle
	Protected name.s

	handle=FindWindow_(0,0)
	handle=GetWindow_(handle,#GW_HWNDFIRST)
	While handle<>0
		name.s=Space(128)
		GetWindowText_(handle,@name,127)

		oldhandle=handle; handle merken (damit mehrfache Programminstanzen gekillt werden)
		handle=GetWindow_(handle,#GW_HWNDNEXT)

		If Len(name)
			If FindString(LCase(name),program,1)
				PostMessage_(oldhandle,#WM_CLOSE,0,0)
			EndIf
		EndIf
	Wend
EndProcedure
Procedure Tool_StopTrueCrypt()
	Protected USBDrive=FirstChar(PeekS(GetCommandLine_())); 		...Runner vom USB-Stick gestartet?
	Protected Pfad.s=""

	If USBDrive='C';											...wohl nicht, also "händisch" prüfen...
		USBDrive='H';											...von "H" beginnend ("G", "F", "E",...) wird's wohl reichen...
		Protected Label.s=Space(128)
		Repeat
			If GetVolumeInformation_(Chr(USBDrive)+":\",@Label,128,#Null,#Null,#Null,#Null,#Null)
				If LCase(Label)="no mo' mem"
					Break
				EndIf
			EndIf
			USBDrive-1
		Until USBDrive='C'
	EndIf
	If USBDrive<>'C'
		Pfad=Chr(USBDrive)+":\Programs\Tools\TrueCrypt\"
	EndIf

	RunProgram(Pfad+"TrueCrypt.exe","/q /d","")
EndProcedure

Procedure ShowWindow(akt)
	Protected y

	If (WindowOpen=#False) And (akt<Anzahl)
		First=akt

		win=OpenWindow(1,0,0,200,45+(Anzahl-akt)*25,"Killer",#PB_Window_Invisible|#WS_THICKFRAME|#PB_Window_ScreenCentered)
		SmartWindowRefresh(1,#True)
		SetWindowLong_(win,#GWL_EXSTYLE,GetWindowLong_(win,#GWL_EXSTYLE)|#WS_EX_TOOLWINDOW); Fenster nicht in Taskleiste anzeigen
		SetWindowPos_(win,#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE|#SWP_FRAMECHANGED|#SWP_NOZORDER); Fenster zurechtrücken (Update)
		CreateGadgetList(win)

		y=0
		While akt<Anzahl
			akt+1
			y+1
			TextGadget(y,50,25*y-10,150,20,"...")
		Wend

		;SetTimer_(win,1,100,0)
		StickyWindow(1,1)
		HideWindow(1,0)

		DCwin=StartDrawing(WindowOutput(1))
		For y=First+1 To Anzahl
			DrawIconEx_(DCwin,20,(y-First)*25-10,Icon(3),0,0,0,#Null,#DI_NORMAL|#DI_COMPAT)
		Next y

		StopDrawing()

		WindowOpen=#True
	EndIf
EndProcedure
Procedure ShowText(nr,string.s)
	If WindowOpen
		nr-First
		SetGadgetText(nr,string)

		DCwin=StartDrawing(WindowOutput(1))
		DrawIconEx_(DCwin,20,nr*25-10,Icon(0),0,0,0,#Null,#DI_NORMAL|#DI_COMPAT)
		Delay(150)
		DrawIconEx_(DCwin,20,nr*25-10,Icon(1),0,0,0,#Null,#DI_NORMAL|#DI_COMPAT)
		StopDrawing()
		Delay(150)

	EndIf
EndProcedure
Procedure Main()
	#Programm="Killer.exe"

	Global i

	Global Params.s
	Global StartDir.s=PeekS(GetCommandLine_())
	Global WinDir.s=Space(128)

	Anzahl=CountProgramParameters()

	; Windows-Verzeichnis bestimmen...
	GetWindowsDirectory_(@WinDir,128)
	WinDir=AddSlash(WinDir)

	; Start-Verzeichnis bestimmen...
	i=FindString(StartDir,"\"+#Programm,1)

	; Heißt das Programm noch "Killer"? Und wurden auch Parameter übergeben?
	If (i>0) And (Anzahl>0)
		; Parameter merken...
		Params=Mid(StartDir,i+Len(#Programm)+3,#MAXSHORT)

		; Startverzeichnis (von Programmnamen und Parametern) abschneiden
		StartDir=Mid(StartDir,2,i-1)

		; Wurde "Killer" aus dem Windows-Verzeichnis gestartet?
		If LCase(StartDir)=LCase(WinDir)
		
			; MessageRequester("Started from Win-Directory",params)

			i=0
			; Parameter abarbeiten...
			While i<Anzahl

				If WindowOpen
					UpdateWindow_(win)
					;WaitWindowEvent()
				EndIf

				i+1
				Select LCase(ProgramParameter())

				Case "*"
					ShowWindow(i)

				Case "usb"
					ShowText(i,"USB Stick auswerfen...")
					Tool_EjectUSB()

				Case "ahk"
					ShowText(i,"AutoHotkey stoppen...")
					Tool_KillProgram("autohotkey v")

				Case "run"
					ShowText(i,"Runner stoppen...")
					Tool_KillProgram("runner...")

				Case "trc"
					ShowText(i,"TrueCrypt stoppen...")
					Tool_StopTrueCrypt()

				EndSelect
			Wend

		Else

			; "Killer" ins Windows-Verzeichnis kopieren und von dort nochmals aufrufen...
			CopyFile(StartDir+#Programm,WinDir+#Programm)
			Delay(250)
			RunProgram(WinDir+#Programm,Params,WinDir)

		EndIf
	EndIf
EndProcedure

Main()