Saturday, 27 November 2021

CreateJobObject.exe and CreateJobObjectTimeout starts a program (such as a batch file). That program and any programs started by that program will be terminated as a group.

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
REM CreateJobObject.bat
REM This file compiles CreateJobObject.vb to CreateJobObject.exe
REM CreateJobObject.exe starts a program (such as a batch file). That program and any programs started by that program will be terminated as a groupREM CreateJobObject.exe starts a program (such as a batch file). That program and any programs started by that program will be terminated as a groupwhen you click Ok. 
REM CreateJobObjectTimeout.exe starts a program (such as a batch file). That program and any programs started by that program will be terminated as a group after the specified number of seconds.
REM To use 
REM     CreateJobObject Program.exe
REM To use 
REM     CreateJobObjectTimeout  Program.exe
REM EG
REM CreateJobObject cmd /k "start notepad & start mspaint"
REM CreateJobObjectTimeout 4 notepad
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\CreateJobObject.exe" "%~dp0\CreateJobObject.vb" 
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\CreateJobObjectTimeOut.exe" "%~dp0\CreateJobObjectTimeOut.vb" 
pause

'CreateJobObject.vb
Imports System.Runtime.InteropServices
Imports System.Diagnostics.Process

Public Module Editor
	Private Declare UNICODE Function CreateJobObjectW Lib "Kernel32" (lpJobAttributes as IntPtr, ByVal lpName As String) As IntPtr
	Private Declare Function AssignProcessToJobObject Lib "Kernel32" (ByVal hJob As IntPtr, hProcess As IntPtr) As Boolean
	Private Declare Function TerminateJobObject Lib "Kernel32" (ByVal HJob as IntPtr, ExitCode As Integer) As Boolean

	Public Sub Main()
		Dim hJob as IntPtr
		Dim hProcess as Integer
		Dim wshshell as Object
		wshshell = CreateObject("Wscript.Shell")
		hJob = CreateJobObjectW(0, "MyJobObject")
		hProcess = -1
		AssignProcessToJobObject(hJob, hProcess)
		wshshell.run(Command(),, vbfalse)
		If Msgbox("Press Ok to terminate this program and any child programs") = 1 then TerminateJobObject(hJob, 0)
		msgbox(err.lastdllerror)
	End Sub
End Module

'CreateJobObjectTimeout.vb
Imports System.Runtime.InteropServices
Imports System.Diagnostics.Process

Public Module Editor
	Private Declare UNICODE Function CreateJobObjectW Lib "Kernel32" (lpJobAttributes as IntPtr, ByVal lpName As String) As IntPtr
	Private Declare Function AssignProcessToJobObject Lib "Kernel32" (ByVal hJob As IntPtr, hProcess As IntPtr) As Boolean
	Private Declare Function TerminateJobObject Lib "Kernel32" (ByVal HJob as IntPtr, ExitCode As Integer) As Boolean
	Private Declare Sub Sleep Lib "Kernel32" (ByVal TimeOut As Integer) 

	Public Sub Main()
		Dim hJob as IntPtr
		Dim hProcess as Integer
		Dim wshshell as Object
		wshshell = CreateObject("Wscript.Shell")
		hJob = CreateJobObjectW(0, "MyJobObject")
		hProcess = -1
		AssignProcessToJobObject(hJob, hProcess)
		wshshell.run(Split(Command()," ", 3)(1),, vbfalse)
		Sleep(CInt(Split(Command(), " ", 2)(0)) * 1000)
		TerminateJobObject(hJob, 0)
		msgbox(err.lastdllerror)
	End Sub
End Module

Saturday, 17 April 2021

WaitForInputIdle.exe Starts a graphical program and returns when when any of its windows is waiting for user input.

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
Window Manipulation Posts

REM Two files follow
REM WaitForInputIdle.bat
REM This file compiles WaitForInputIdle.vb to WaitForInputIdle.exe using the system VB.NET compiler.
REM Starts a program and returns when the program has finished initialising and waiting for the user
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\WaitForInputIdle.vb" /out:"%~dp0\WaitForInputIdle.exe" /target:exe
REM To use
REM       WaitForInputIdle <Timeout> <"Command to run"> <Other Parameters>
REM            -1 is no timeout. Program name must be enclosed in quotes.
REM       WaitForInputIdle 10 "notepad" c:\windows\win.ini
pause


'CreateRemoteProcess.vbs
imports System.Runtime.InteropServices 


Public Module MyApplication  
		
	Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Boolean, ByVal processId As UInt32) As IntPtr
	Public Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As IntPtr, ByVal dwMilliseconds As Integer) As Integer 

	Public Const AllAccess = &H1F0FFF
	Public Const Terminate = &H1
	Public Const CreateThread = &H2
	Public Const VirtualMemoryOperation = &H8
	Public Const VirtualMemoryRead = &H10
	Public Const VirtualMemoryWrite = &H20
	Public Const DuplicateHandle = &H40
	Public Const CreateProcess = &H80
	Public Const SetQuota = &H100
	Public Const SetInformation = &H200
	Public Const QueryInformation = &H400
	Public Const QueryLimitedInformation = &H1000
	Public Const Synchronize = &H100000

	Public Const WAIT_TIMEOUT = 258 
	Public Const WAIT_Failed = -1 

	Public Sub Main ()
		Dim Proc As Object
		Dim hProcess As IntPtr
		Dim Ret As IntPtr
		Dim CmdLine As String
		Dim A as String()
		Dim B as String()

		CmdLine = Command()
		If CmdLine = "" then 
			Console.writeline("WaitForInputIdle <Timeout> <""Program name""> [<arguments>] -1 is indefinite timeout, program name must be in quotes")
			exit sub
		End If
		A = Split(CmdLine, Chr(32), 2, 1)
		B = Split(A(1), """", 3, 1)
		On Error Resume Next
		console.writeline("WaitForInputIdle")
		console.writeline("Waiting for " & B(1) & " started with args " & Trim(B(2)) & " to be ready")
		Proc = System.Diagnostics.Process.Start(B(1), Trim(B(2)))
		If err.number <> 0 then
			Console.writeline("Program could not be started - Error is " & err.description)
			Console.writeline("WaitForInputIdle <Timeout> <""Program name""> [<arguments>] -1 is indefinite timeout, program name must be in quotes")
			Exit Sub
		End If
			
		hProcess = OpenProcess(QueryInformation, False, Proc.ID)
		Ret = WaitForInputIdle(hProcess, CInt(A(0)) * 1000)
		If Ret = 0 then
			Console.Writeline("Program is ready for user input")
		ElseIf Ret = 258
			Console.Writeline("Program timed out")
		Else
			Console.Writeline("Error " & err.lastdllerror)
		End If


		Environment.ExitCode = Ret
	End Sub 
End Module 

Monday, 12 April 2021

HideTaskbarBtn.exe Hides or shows a window on the taskbar.

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
Window Manipulation Posts

Windows that are normal, a titlebar with a system menu, appear on the taskbar. Windows that want to appear on the taskbar but don't meet the requirements can set an extended window's style of an AppWindow. To remove a window from the taskbar requires the window to be hidden, the AppWindow extended style forcing it onto the taskbar to be removed and the extended style of a tool palette window applied. Then show the window. The new window won't have a titlebar icon.

This does not work with UWP apps, only Win32 applications and consoles.

If you run this on a minimised window there is no way of activating the window. See Assigns a hotkey to a window if you need to. If you run this on a non-minimised program then minimise the program a Windows 3.11 minimised desktop icon appears.


@Echo Off
Echo HideTaskbarBtn.bat
Echo This file compiles HideTaskbarBtn.vb to HideTaskbarBtn.exe
Echo HideTaskbarBtn.exe hides or shows a window'a button on the taskbar
Echo To use 
Echo     HideTaskbarBtn Hide ^<Window Title^>
Echo     HideTaskbarBtn Show ^<Window Title^>
Echo E.G.
Echo     HideTaskbarBtn Hide Untitled - Notepad
Echo     HideTaskbarBtn Show Untitled - Notepad
Echo -----------------------------------------------------
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\HideTaskbarBtn.exe" "%~dp0\HideTaskbarBtn.vb" 
pause

'HideTaskbarBtn.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module TopMost
	Public Declare UNICODE Function FindWindowW Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
	Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
	Public Declare Function SetWindowLongPtrW Lib "user32" (ByVal hwnd As IntPtr, ByVal Index As Integer, ByVal NewValue As Integer) As Integer
	Public Declare Function GetWindowLongPtrW Lib "user32" (ByVal hwnd As IntPtr, ByVal Index As Integer) As Integer
	Public Declare Function GetParent Lib "user32.dll" (ByVal hwnd As Intptr) As IntPtr

	Public Const WS_EX_APPWINDOW = &h40000
	Public Const WS_EX_TOOLWINDOW = &h80
	Public Const WS_MINIMIZEBOX = &h20000
	Public Const GWL_EXSTYLE = -20
	Public Const GWL_STYLE = -16

	Public Const HWND_TOPMOST = -1
	Public Const HWND_NOTOPMOST = -2
	Public Const SWP_NOMOVE = &H2
	Public Const SWP_NOSIZE = &H1
	Public Const SWP_SHOWWINDOW = &H40
	Public Const SWP_HIDEWINDOW = &H80
	Public Const SWP_NOOWNERZORDER = &H200      '  Don't do owner Z ordering
	Public Const SWP_NOREDRAW = &H8
	Public Const SWP_NOREPOSITION = &H200
	Public Const SWP_NOZORDER = &H4

	Sub Main()
		On Error Resume Next
		Dim hWindows as IntPtr
		Dim CmdLine as String
		Dim Ret as Integer
		Dim ExStyle as Integer
		Dim Style as Integer
		CmdLine = Mid(Command(),6)
		hwindows = FindWindowW(vbNullString, CmdLine)
		If hwindows = 0 then
			Msgbox(Cmdline & " cannot be found.")
		Else
			If LCase(Left(Command(), 4)) = LCase("Hide") then
				Ret = GetWindowLongPtrW(hWindows, GWL_EXSTYLE)
				ExStyle = Ret
				'Test AppWindow is set and if so remove it
				If (ExStyle And WS_EX_APPWINDOW) = WS_EX_APPWINDOW then ExStyle = ExStyle - WS_EX_APPWINDOW
				If (ExStyle And WS_EX_TOOLWINDOW) <> WS_EX_TOOLWINDOW then ExStyle = ExStyle + WS_EX_TOOLWINDOW
				Ret = SetWindowPos(hwindows, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE + SWP_NOZORDER + SWP_HIDEWINDOW)
				Ret = GetWindowLongPtrW(hWindows, GWL_EXSTYLE)
				'SetWindowLongPtr does not clear GetLastError if sucessful.
				err.clear
				Ret = SetWindowLongPtrW(hWindows, GWL_EXSTYLE, ExStyle)
				If (Ret = 0 And err.LastDLLError <> 0) Then MsgBox("SetWindowLongPtrW is " & Err.LastDllError)
				err.clear
'				Ret = SetWindowLongPtrW(hWindows, GWL_STYLE, Style)
'				If (Ret = 0 And err.LastDLLError <> 0) Then MsgBox("SetWindowLongPtrW is " & Err.LastDllError)
				Ret = SetWindowPos(hwindows, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE + SWP_NOZORDER + SWP_SHOWWINDOW)
				If Ret = 0 Then MsgBox("Set Pos Error is " & Err.LastDllError)
			ElseIf LCase(Left(Command(), 4)) = LCase("Show") then
				Ret = GetWindowLongPtrW(hWindows, GWL_EXSTYLE)
				'Test AppWindow is set and if so remove it
				ExStyle = Ret
				If (ExStyle And WS_EX_APPWINDOW) <> WS_EX_APPWINDOW then ExStyle = ExStyle + WS_EX_APPWINDOW
				If (ExStyle And WS_EX_TOOLWINDOW) = WS_EX_TOOLWINDOW then ExStyle = ExStyle - WS_EX_TOOLWINDOW
				Ret = SetWindowPos(hwindows, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE + SWP_NOZORDER + SWP_HIDEWINDOW)
				If Ret = 0 Then MsgBox("Set Pos Error is " & Err.LastDllError)
				err.clear
				Ret = SetWindowLongPtrW(hWindows, GWL_EXSTYLE, ExStyle)
				If (Ret = 0 And err.LastDLLError <> 0) Then MsgBox("SetWindowLongPtrW is " & Err.LastDllError)
				Ret = SetWindowPos(hwindows, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE + SWP_NOZORDER + SWP_SHOWWINDOW)
				If Ret = 0 Then MsgBox("Set Pos Error is " & Err.LastDllError)
			Else
				Msgbox("Command line not recognised")
			End If
		End If
	End Sub
End Module

Monday, 29 March 2021

HotkeyToWindow.exe Assigns a hotkey to a window. When pressed will activate that window.

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
Window Manipulation Posts

There are two types of hotkeys in Windows. RegisterHotkey requires a program to expect it. WM_SetHotkey is supported by the default behaviour of a window so the program doesn't need to be aware of it.

Hotkeys are system keys so work all the time. To take the example below, using sendkeys to send Ctrl+N to any application will activate Notepad. This is one way around restrictions on setting the active window.


REM HotkeyToWindow.bat
REM This file compiles TopMost.vb to TopMost.exe
REM HotkeyToWindow.exe sets a hotkey to activate the window
REM To use 
REM     HotkeyToWindow &ltModifier&gt  &ltvirtualkey&gt &ltWindowtitle&gt
REM E.G.
REM To assign Ctrl (2) and N key (78) to notepad
REM     HotkeyToWindow 2 78 Untitled - Notepad
REM To remove the hotkey send 0
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\HotkeyToWindow.exe" "%~dp0\HotkeyToWindow.vb"
pause


'HotkeyToWindow.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module TopMost
	Public Declare UNICODE Function FindWindowW Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
	Public Declare UNICODE Function SendMessageInt Lib "user32" alias "SendMessageW" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, lParam as Integer) As Integer
	Public Const WM_SETHOTKEY = &h32
	Public const HOTKEYF_SHIFT = 1
	Public const HOTKEYF_CONTROL = 2
	Public const HOTKEYF_ALT = 4
	Public const HOTKEYF_EXT = 8

	Sub Main()
		On Error Resume Next
		Dim hWindows as IntPtr
		Dim CmdLine as String
		Dim Ret as Integer
		Dim vKey as UInteger

		CmdLine = Command()
		Dim A as String()
		A = Split(CmdLine, Chr(32), 3, 1)
		hwindows = FindWindowW(vbNullString, A(2))
		If hwindows = 0 then
			Msgbox(A(2) & " cannot be found.")
		Else
			vKey = (cByte(A(0)) * &h100) 
			vKey = vKey + cByte(A(1))
			Ret = SendMessageInt(hWindows, WM_SETHOTKEY, vkey, 0)
			If Ret = -1 then MsgBox("Invalid Hotkey")
			If Ret = 0 then MsgBox("Invalid Window")
		End If
	End Sub
End Module

Modifiers - add them together to use more than one

SHIFT = 1
CONTROL = 2
ALT = 4
EXT = 8
Virtual Keycodes
Key Virtual Code
Decimal
Virtual Code
Hexadecimal
Left mouse button 1 0x1
Right mouse button 2 0x2
Control-break processing 3 0x3
Middle mouse button (three-button mouse) 4 0x4
X1 mouse button 5 0x5
X2 mouse button 6 0x6
Undefined 7 0x7
Backspace 8 0x8
Tab 9 0x9
Clear 12 0xc
Enter 13 0xd
Shift 16 0x10
Ctrl 17 0x11
Alt 18 0x12
Pause 19 0x13
Caps Lock 20 0x14
IME Kana mode 21 0x15
Undefined 22 0x16
IME Junja mode 23 0x17
IME final mode 24 0x18
IME Hanja mode 25 0x19
Esc 27 0x1b
IME Convert 28 0x1c
IME Nonconvert 29 0x1d
IME Accept 30 0x1e
IME Mode Change Request 31 0x1f
Spacebar 32 0x20
Page Up 33 0x21
Page Down 34 0x22
End 35 0x23
Home 36 0x24
Left Arrow 37 0x25
Up Arrow 38 0x26
Right Arrow 39 0x27
Down Arrow 40 0x28
Select 41 0x29
Print 42 0x2a
Execute 43 0x2b
Print Screen 44 0x2c
Ins 45 0x2d
Del 46 0x2e
Help 47 0x2f
0 48 0x30
1 49 0x31
2 50 0x32
3 51 0x33
4 52 0x34
5 53 0x35
6 54 0x36
7 55 0x37
8 56 0x38
9 57 0x39
A 65 0x41
B 66 0x42
C 67 0x43
D 68 0x44
E 69 0x45
F 70 0x46
G 71 0x47
H 72 0x48
I 73 0x49
J 74 0x4a
K 75 0x4b
L 76 0x4c
M 77 0x4d
N 78 0x4e
O 79 0x4f
P 80 0x50
Q 81 0x51
R 82 0x52
S 83 0x53
T 84 0x54
U 85 0x55
V 86 0x56
W 87 0x57
X 88 0x58
Y 89 0x59
Z 90 0x5a
Left Windows key 91 0x5b
Right Windows key 92 0x5c
Applications key 93 0x5d
Reserved 94 0x5e
Computer Sleep 95 0x5f
Numeric keypad 0 96 0x60
Numeric keypad 1 97 0x61
Numeric keypad 2 98 0x62
Numeric keypad 3 99 0x63
Numeric keypad 4 100 0x64
Numeric keypad 5 101 0x65
Numeric keypad 6 102 0x66
Numeric keypad 7 103 0x67
Numeric keypad 8 104 0x68
Numeric keypad 9 105 0x69
Multiply 106 0x6a
Add 107 0x6b
Separator 108 0x6c
Subtract 109 0x6d
Decimal 110 0x6e
Divide 111 0x6f
F1 112 0x70
F2 113 0x71
F3 114 0x72
F4 115 0x73
F5 116 0x74
F6 117 0x75
F7 118 0x76
F8 119 0x77
F9 120 0x78
F10 121 0x79
F11 122 0x7a
F12 123 0x7b
F13 124 0x7c
F14 125 0x7d
F15 126 0x7e
F16 127 0x7f
F17 128 0x80
F18 129 0x81
F19 130 0x82
F20 131 0x83
F21 132 0x84
F22 133 0x85
F23 134 0x86
F24 135 0x87
Num Lock 144 0x90
Scroll Lock 145 0x91
Left Shift 160 0xa0
Right Shift 161 0xa1
Left Control 162 0xa2
Right Control 163 0xa3
Left Menu 164 0xa4
Right Menu 165 0xa5
Browser Back 166 0xa6
Browser Forward 167 0xa7
Browser Refresh 168 0xa8
Browser Stop 169 0xa9
Browser Search 170 0xaa
Browser Favorites 171 0xab
Browser Start And Home 172 0xac
Volume Mute 173 0xad
Volume Down 174 0xae
Volume Up 175 0xaf
Next Track 176 0xb0
Previous Track 177 0xb1
Stop Media 178 0xb2
Play/Pause Media 179 0xb3
Start Mail 180 0xb4
Select Media 181 0xb5
Start Application 1 182 0xb6
Start Application 2 183 0xb7
; : 186 0xba
+ 187 0xbb
, 188 0xbc
- 189 0xbd
. 190 0xbe
/ ? 191 0xbf
` ~ 192 0xc0
[ { 219 0xdb
\ | 220 0xdc
] } 221 0xdd
' " 222 0xde
223 0xdf
Reserved 224 0xe0
Oem Specific 225 0xe1
Either The Angle Bracket Key Or The Backslash Key On The Rt 102-Key Keyboard 226 0xe2
Windows 95/98/Me, Windows Nt 4.0, Ime Process 229 0xe5
Used to pass unicode characters as if they were keystrokes 231 0xe7
Attn 246 0xf6
Crsel 247 0xf7
Exsel 248 0xf8
Erase Eof 249 0xf9
Play 250 0xfa
Zoom 251 0xfb
Pa1 253 0xfd
Clear 254 0xfe

Saturday, 13 March 2021

MoveWindow.exe - Moves a window

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
Window Manipulation Posts

REM MoveWindow.bat
REM This file compiles MoveWindow.vb to MoveWindow.exe
REM MoveWindow changes the position of a window
REM To use 
REM MoveWindow nnnnxnnnn <Windowtitle>
REM E.G.
REM MoveWindow 300x400 Untitled - Notepad
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\MoveWindow.exe" "%~dp0\MoveWindow.vb" /verbose
pause

'MoveWindow.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module TopMost
	Public Declare UNICODE Function FindWindowW Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
	Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
	Public Const HWND_TOPMOST = -1
	Public Const HWND_NOTOPMOST = -2
	Public Const SWP_NOMOVE = &H2
	Public Const SWP_NOSIZE = &H1
	Public Const SWP_NOZORDER = &h4

	Sub Main()
		On Error Resume Next
		Dim hWindows as IntPtr
		Dim CmdLine as String
		Dim Ret as Integer
		CmdLine = Command()
		Dim A as String()
		Dim B as String()
		A = Split(CmdLine, Chr(32), 2, 1)
		B = Split(A(0), "x", 2, 1)
		hwindows = FindWindowW(vbNullString, A(1))
		If hwindows = 0 then
			Msgbox(A(1) & " cannot be found.")
		Else
			Ret = SetWindowPos(hwindows, HWND_NOTOPMOST, B(0), B(1), 0, 0, SWP_NOSIZE + SWP_NOZORDER)
			If Ret = 0 Then MsgBox("Set Pos Error is " & Err.LastDllError)
		End If
	End Sub
End Module