Showing posts with label API Call. Show all posts
Showing posts with label API Call. Show all posts

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

Friday, 12 March 2021

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

'TopMost.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, 0, 0, B(0), B(1), SWP_NOMOVE + SWP_NOZORDER)
			If Ret = 0 Then MsgBox("Set Pos Error is " & Err.LastDllError)
		End If
	End Sub
End Module

Thursday, 15 October 2020

IsAdmin.exe Returns Yes to standard out and a errorlevel of 0 if user is elevated else returns No and errorlevel of 1

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 IsAdmin.bat
REM This file compiles IsAdmin.vb to IsAdmin.exe
REM IsAdmin.exe Says if the user is running elevated
REM To Use
Rem          IsAdmin 
REM         Return Yes and Errorlevel = 0 if user is admin and elevated else returns No and Errorlevel = 1
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\IsAdmin.exe" "%~dp0\IsAdmin.vb" 
pause

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

Public Module MyApplication 
 
	Public Declare Function IsUserAnAdmin Lib "Shell32" () As Boolean

Sub Main()
	If IsUserAnAdmin() = True then
		Console.writeline("Yes")
		Environment.ExitCode = 0

	Else
		Console.writeline("No")
		Environment.ExitCode = 1
	End If
End Sub
End Module

Sunday, 9 August 2020

QuickEdit.exe Turns on or off Quick Edit mode in the console.

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 QuickEdit.bat
REM This file compiles QuickEdit.vb to QuickEdit.exe
REM QuickEdit.exe turns on or off Quick Edit mode in the command prompt.
REM To Use
Rem          QuickEdit [on|off]
REM              Without parameters reports on the state of Quick Edit mode.
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\QuickEdit.exe" "%~dp0\QuickEdit.vb" 
pause


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

Public Module MyApplication 
 
	Public Declare Function GetStdHandle Lib "kernel32" Alias "GetStdHandle" (ByVal nStdHandle As Long) As Long
	Public Declare Function GetConsoleMode Lib "kernel32" (ByVal hConsoleHandle As IntPtr, ByRef lpMode As Integer) As Integer
	Public Declare Function SetConsoleMode Lib "kernel32" (ByVal hConsoleHandle As Long, ByVal dwMode As Integer) As Integer

	Public Const STD_ERROR_HANDLE = -12&
	Public Const STD_INPUT_HANDLE = -10&
	Public Const STD_OUTPUT_HANDLE = -11&

	'Input
	Public Const ENABLE_EXTENDED_FLAGS = &h0080
	Public Const ENABLE_ECHO_INPUT = &h0004
	Public Const ENABLE_INSERT_MODE = &h0020
	Public Const ENABLE_LINE_INPUT = &h0002
	Public Const ENABLE_MOUSE_INPUT = &h0010
	Public Const ENABLE_PROCESSED_INPUT = &h0001
	Public Const ENABLE_QUICK_EDIT_MODE = &h0040
	Public Const ENABLE_WINDOW_INPUT = &h0008
	Public Const ENABLE_VIRTUAL_TERMINAL_INPUT = &h0200
	'Output
	Public Const ENABLE_PROCESSED_OUTPUT = &h0001
	Public Const ENABLE_WRAP_AT_EOL_OUTPUT = &h0002
	Public Const ENABLE_VIRTUAL_TERMINAL_PROCESSING = &h0004
	Public Const DISABLE_NEWLINE_AUTO_RETURN = &h0008
	Public Const ENABLE_LVB_GRID_WORLDWIDE = &h0010

Sub Main()
	Dim hIn As IntPtr
	Dim Ret As Integer
	Dim Mode As Integer
	hIn  = GetStdHandle(STD_INPUT_HANDLE)
	Ret = GetConsoleMode(hIn, Mode)
	If Command() = "" then
		If (Mode And ENABLE_QUICK_EDIT_MODE) = ENABLE_QUICK_EDIT_MODE then
			Console.writeline("Quick Edit On")
		Else
			Console.writeline("Quick Edit Off")
		End If
	ElseIf LCase(Command()) = "on"
		If (Mode And ENABLE_QUICK_EDIT_MODE) = 0 then Ret = SetConsoleMode(hIn, Mode + ENABLE_QUICK_EDIT_MODE)
		If Ret = 0 then Console.WriteLine(Hex(Ret) & " - " & err.lastdllerror)
	ElseIf LCase(Command()) = "off"
		If (Mode And ENABLE_QUICK_EDIT_MODE) = ENABLE_QUICK_EDIT_MODE then Ret = SetConsoleMode(hIn, Mode - ENABLE_QUICK_EDIT_MODE)
		If Ret = 0 then Console.WriteLine(Hex(Ret) & " - " & err.lastdllerror)
	End If
End Sub
End Module

Saturday, 13 June 2020

GetCurrentConsoleWindowRect

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

Use this to get the current console rect.

REM GetCurrentConsoleWindowRect.bat
REM This file compiles GetCurrentConsoleWindowRect.vb to GetWindowRect.exe
REM GetCurrentConsoleWindowRect.exe reports on console's windows position
REM To use 
REM GetCurrentConsoleWindowRect
REM EG
REM GetCurrentConsoleWindowRect
REM
REM Change /target:exe to /target:winexe and uncomment the 
REM msgbox line in main file to make it a non console program
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\GetCurrentConsoleWindowRect.exe" "%~dp0\GetCurrentConsoleWindowRect.vb"
pause

'GetCurrentConsoleWindowRect.vb
imports System.Runtime.InteropServices 
Public Module GetWindowRect  

   _
 Private Structure RECTL  
  Public Left As Int32
  Public Top As Int32
  Public Right As Int32
  Public Bottom As Int32
 End Structure

 Private Declare Function GetWindowRect Lib "User32" (ByVal hWnd as IntPtr, ByRef Rect as RectL) as Integer
 Public Declare UNICODE Function FindWindowW Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
 Public Declare UNICODE Function GetConsoleTitleW Lib "kernel32" (ByVal lpConsoleTitle As String, ByVal nSize As Integer) As Integer

 Sub Main
  On Error Resume Next
  Dim hWindows as IntPtr
  Dim Ret as Integer
  Dim ConsoleTitle as String
  Dim Size as Integer
  ConsoleTitle = StrDup(1024, ChrW(0))
  Size = 1020
  Ret = GetConsoleTitleW(ConsoleTitle, Size)
  
  hwindows = FindWindowW(vbNullString, ConsoleTitle)
  If hwindows = 0 then
   Msgbox(Command() & " cannot be found.")
  Else
   Dim x as RectL
   Ret = GetWindowRect(hWindows, x)
   If Ret = 0 Then 
    MsgBox("GetWindowRect Error " & Err.LastDllError)
   Else
    'Uncomment the MsgBox line if using as non console program
    'Msgbox(x.left & " " & x.top & " " & x.right & " " & x.bottom)
    Console.Writeline(x.left & " " & x.top & " " & x.right & " " & x.bottom)
   End If
  End If
 End Sub

End Module 

Monday, 25 May 2020

HideWindow Hides an existing window or shows a hidden 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

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


'HideWindow.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_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
  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 = 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)
   ElseIf LCase(Left(Command(), 4)) = LCase("Show") then
    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

Saturday, 9 May 2020

KeepDisplayOn - Runs a program preventing sleeping or the display turning off while the program runs

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.


@Echo Off
ECHO Three files follow
ECHO PreventSleep.bat
ECHO.
ECHO This file compiles KeepDisplayOn.vb and KeepSystemOn.vb to KeepDisplayOn.exe and KeepSystemOn.exe using the system VB.NET compiler.
ECHO.
ECHO Runs a program preventing sleeping or the display turning off while the program runs
ECHO.
ECHO To Use
ECHO      KeepDisplayOn "C:\windows\notepad"
ECHO      KeepSystemOn "C:\windows\notepad"
ECHO.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\KeepDisplayOn.vb" /out:"%~dp0\KeepDisplayOn.exe" /target:winexe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\KeepSystemOn.vb" /out:"%~dp0\KeepSystemOn.exe" /target:winexe
pause






'KeepSystemOn.vb
imports System.Runtime.InteropServices
Public Module MyApplication 
Public Declare UNICODE Function SetThreadExecutionState Lib "Kernel32" (ByVal esFlags as Integer) as Integer
Public Const  ES_AWAYMODE_REQUIRED = &h40
Public Const  ES_CONTINUOUS = &h80000000
Public Const  ES_DISPLAY_REQUIRED = &h2
Public Const  ES_SYSTEM_REQUIRED = &h1
Public Const  ES_USER_PRESENT = &h4

 Public Sub Main ()
  Dim wshshell as Object
  Dim Ret as Integer
  WshShell = CreateObject("WScript.Shell")
  Ret = SetThreadExecutionState(ES_Continuous + ES_System_Required + ES_Awaymode_Required)
  WshShell.Run(Command(), , True)
 End Sub
End Module





'KeepDisplayOn.vb
imports System.Runtime.InteropServices
Public Module MyApplication 
Public Declare UNICODE Function SetThreadExecutionState Lib "Kernel32" (ByVal esFlags as Integer) as Integer
Public Const  ES_AWAYMODE_REQUIRED = &h40
Public Const  ES_CONTINUOUS = &h80000000
Public Const  ES_DISPLAY_REQUIRED = &h2
Public Const  ES_SYSTEM_REQUIRED = &h1
Public Const  ES_USER_PRESENT = &h4

 Public Sub Main ()
  Dim wshshell as Object
  Dim Ret as Integer
  WshShell = CreateObject("WScript.Shell")
  Ret = SetThreadExecutionState(ES_Continuous + ES_Display_Required + ES_Awaymode_Required)
  WshShell.Run(Command(), , True)
 End Sub
End Module

Sunday, 19 January 2020

GetWindowRect.exe reports on Windows position. Use GetCurrentConsoleWindowRect to use with the current console.

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 GetWindowRect.bat
REM This file compiles GetWindowRect.vb to GetWindowRect.exe
REM GetWindowRect.exe reports on Windows position
REM To use 
REM GetWindowRect <Window Title>
REM EG
REM GetWindowRect Untitled - Notepad
REM
REM Change /target:exe to /target:winexe and uncomment the 
REM msgbox line in main file to make it a non console program
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\GetWindowRect.exe" "%~dp0\GetWindowRect.vb"
pause

'GetWindowRect.vb
imports System.Runtime.InteropServices 
Public Module GetWindowRect  

   _
 Private Structure RECTL  
  Public Left As UInt32
  Public Top As UInt32
  Public Right As UInt32
  Public Bottom As UInt32
 End Structure

 Private Declare Function GetWindowRect Lib "User32" (ByVal hWnd as IntPtr, ByRef Rect as RectL) as Integer
 Public Declare UNICODE Function FindWindowW Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

 Sub Main
  On Error Resume Next
  Dim hWindows as IntPtr
  Dim Ret as Integer
  hwindows = FindWindowW(vbNullString, Command())
  If hwindows = 0 then
   Msgbox(Command() & " cannot be found.")
  Else
   Dim x as RectL
   Ret = GetWindowRect(hWindows, x)
   If Ret = 0 Then 
    MsgBox("GetWindowRect Error " & Err.LastDllError)
   Else
    'Uncomment the MsgBox line if using as non console program
    'Msgbox(x.left & " " & x.top & " " & x.right & " " & x.bottom)
    Console.Writeline(x.left & " " & x.top & " " & x.right & " " & x.bottom)
   End If
  End If
 End Sub

End Module 

Thursday, 26 December 2019

ColourText changes the colour of the text to be printed. This technique is the ONLY one that will work on all Windows version.

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.

To use

ColourText <ColourOfText> <ColourOfTextWhenFinished> [Text]

Also the CLS command becomes interesting. Color command without parameters resets all colours to startup colours.

To get the colour code add the following numbers together. Use Calculator in programmers mode. These are hex numbers. They can be added together eg Red + Blue + FG Intensity = 13 = D. As 10+ wasn't used the background will be black. Colour codes MUST be two characters, eg 08 not 8.

FOREGROUND_RED = &H4     '  text color contains red.
FOREGROUND_INTENSITY = &H8     '  text color is intensified.
FOREGROUND_GREEN = &H2     '  text color contains green.
FOREGROUND_BLUE = &H1     '  text color contains blue.
BACKGROUND_BLUE = &H10    '  background color contains blue.
BACKGROUND_GREEN = &H20    '  background color contains green.
BACKGROUND_INTENSITY = &H80    '  background color is intensified.
BACKGROUND_RED = &H40    '  background color contains red.

So black background is 0 while white is F0 (adding 10 + 20 + 40 + 80). Red on white is f4.



REM 2 files follow
REM ColourText.bat
REM Compiles ColourText.vb to ColourText.exe
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\ColourText.exe" "%~dp0\ColourText.vb" /verbose
pause



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

Public Module MyApplication  
Public Declare Function GetStdHandle Lib "kernel32" Alias "GetStdHandle" (ByVal nStdHandle As Long) As Long
Public Declare Function SetConsoleTextAttribute Lib "kernel32" Alias "SetConsoleTextAttribute" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Public Const STD_ERROR_HANDLE = -12&
Public Const STD_INPUT_HANDLE = -10&
Public Const STD_OUTPUT_HANDLE = -11&

Sub Main()
    Dim hOut as Long
    Dim Ret as Long
    Dim Colour As Long
    Dim Colour1 As Long
    Dim Text As String
    hOut  = GetStdHandle(STD_OUTPUT_HANDLE)
    Colour = CLng("&h" & Split(Command(), " ")(0))
    Colour1 = Clng("&h" & Split(Command(), " ")(1))
    Text = Mid(Command(), 7)
    Ret = SetConsoleTextAttribute(hOut,  Colour)
    Console.Out.WriteLine(text)
    Ret = SetConsoleTextAttribute(hOut, Colour1)
End Sub
End Module

Wednesday, 25 December 2019

GetConsoleColour.exe prints the current console colour and returns an errorlevel with the value

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.
;Two files follow
REM GetConsoleColour.bat
REM This file compiles GetConsoleColour.vb to GetConsoleColour.exe
REM GetConsoleColour.exe prints the current console colour and returns an errorlevel with the value
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\GetConsoleColour.exe" "%~dp0\GetConsoleColour.vb" 
pause




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

Public Module MyApplication 
 
Public Declare Function GetStdHandle Lib "kernel32" Alias "GetStdHandle" (ByVal nStdHandle As Long) As Long
Public Declare Function SetConsoleTextAttribute Lib "kernel32" Alias "SetConsoleTextAttribute" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Public Declare Function GetConsoleScreenBufferInfo Lib "kernel32" (ByVal hConsoleOutput As Integer, ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Integer
Public Const STD_ERROR_HANDLE = -12&
Public Const STD_INPUT_HANDLE = -10&
Public Const STD_OUTPUT_HANDLE = -11&

  _
Public Structure COORD
 Public x As Short
 Public y As Short
End Structure

  _
Public Structure SMALL_RECT
 Public Left As Short
 Public Top As Short
 Public Right As Short
 Public Bottom As Short
End Structure

  _
Public Structure CONSOLE_SCREEN_BUFFER_INFO
 Public dwSize As COORD
 Public dwCursorPosition As COORD
 Public wAttributes As Integer
 Public srWindow As SMALL_RECT
 Public dwMaximumWindowSize As COORD
End Structure 


Sub Main()
 Dim hOut as IntPtr
 Dim Ret as Integer
 Dim CSBI as Console_Screen_Buffer_Info
 hOut  = GetStdHandle(STD_OUTPUT_HANDLE)
 Ret = GetConsoleScreenBufferInfo(hOut, CSBI)
 Console.Writeline(Hex(CSBI.wAttributes))
 Environment.ExitCode = CSBI.wAttributes
End Sub
End Module

Tuesday, 11 June 2019

ChangeWallpaper.exe - Changes the desktop wallpaper from the command line

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.

Although this is from the 2001 documentation and has been removed from current.


Setting pvParam to "" removes the wallpaper. Setting pvParam to VBNULL reverts to the default wallpaper.

See docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow

REM Compiles ChangeWallpaper.vb to ChangeWallpaper.exe
REM To use
REM ChangeWallpaper Wallpaper.bmp
REM EG ChangeWallpaper "C:\Windows\Web\Wallpaper\Theme1\img1.jpg"
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "ChangeWallpaper.vb" /out:"%~dp0\ChangeWallpaper.exe" /target:winexe
pause



;ChangeWallpaper.vb
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public Module SendWinKey
    Public Declare Unicode Function SystemParametersInfoW Lib "user32" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer
    Public Const SPI_SETDESKWALLPAPER = 20
    Public Const SPIF_SENDWININICHANGE = &H2
    Public Const SPIF_UPDATEINIFILE = &H1

Public Sub Main()    
    Dim Ret as Integer
    Dim FName As String
    'Takes a filename on the command line removing quotes
    FName = Replace(Command(), """", "")
    Ret = SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, FName, SPIF_SENDWININICHANGE + SPIF_UPDATEINIFILE)
    If Ret = 0 Then Msgbox(err.lastdllerror)
End Sub

End Module

Friday, 7 June 2019

Simulates the PrintScreen Key - SendKeys implemented in Windows Scripting and Basic languages cannot send PrintScreen key, this program allows the PrintScreen key to be simulated.

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 Compiles PrintScreen.vb to PrintScreen.exe
REM SendKeys implemented in Windows Scripting and Basic languages cannot send PrintScreen key, this program allows the PrintScreen key to be simulated.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\PrintScreen.vb" /out:"%~dp0\PrintScreen.exe" /target:winexe
pause





Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public Module SendWinKey
    Const KEYEVENTF_KEYDOWN As Integer = &H0
    Const KEYEVENTF_KEYUP As Integer = &H2

    Declare Sub keybd_event Lib "User32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As UInteger, ByVal dwExtraInfo As UInteger)

Public Sub Main()    
        keybd_event(CByte(Keys.PrintScreen), 0, KEYEVENTF_KEYDOWN, 0) 'press the PrintScreen key down
        keybd_event(CByte(Keys.PrintScreen), 0, KEYEVENTF_KEYUP, 0) 'release the PrintScreen key
End Sub

End Module

Monday, 13 May 2019

ClearClipboard.exe clears any data on the Windows' clipboard

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 ClearClipboard.bat
REM This file compiles ClearClipboard.vb to ClearClipboard.exe using the system VB.NET compiler
REM ClearClipboard clears any data on the Windows' clipboard
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\ClearClipboard.exe" "%~dp0\ClearClipboard.vb" 
pause



'ClearClipboard.vb
Imports System
Imports System.Windows.Forms.Clipboard
Public Module MyApplication  


 Sub Main()
  System.Windows.Forms.Clipboard.Clear()
 End Sub
End Module

Sunday, 12 May 2019

ListAttr.exe lists all 19 of the attributes of a file, folder, volume, or device.

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.
@echo off
ECHO Three files follow
ECHO ListAttr.bat
ECHO This file compiles ListAttr.vb to ListAttr.exe using the system VB.NET compiler.
ECHO ListAttr.exe lists all 19 of the attributes of a file, folder, volume, or device.
Echo To Use
Echo         ListAttr ^
Echo -----------------------------------------------------------------------------------------
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\ListAttr.vb" /out:"%~dp0\ListAttr.exe" /target:exe
Pause



Echo ListAttrTest.Bat
"%~dp0\ListAttr" C:
"%~dp0\ListAttr" C:\
"%~dp0\ListAttr" C:\Windows\System32\Catroot
"%~dp0\ListAttr" nul
"%~dp0\ListAttr" c:\bootnxt
For /d %%A in ("C:\Windows\*") Do @"%~dp0\ListAttr" %%~A
Pause



'ListAttr.vb
imports System.Runtime.InteropServices 
Public Module MyApplication  

Public Declare Unicode Function GetFileAttributesW Lib "Kernel32" (ByVal Path As String) As Integer
Public Const FILE_ATTRIBUTE_ARCHIVE = 32 
Public Const FILE_ATTRIBUTE_COMPRESSED = 2048 
Public Const FILE_ATTRIBUTE_DEVICE = 64 
Public Const FILE_ATTRIBUTE_DIRECTORY = 16 
Public Const FILE_ATTRIBUTE_ENCRYPTED = 16384 
Public Const FILE_ATTRIBUTE_HIDDEN = 2 
Public Const FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768 
Public Const FILE_ATTRIBUTE_NORMAL = 128 
Public Const FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192 
Public Const FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072 
Public Const FILE_ATTRIBUTE_OFFLINE = 4096 
Public Const FILE_ATTRIBUTE_READONLY = 1 
Public Const FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS = 4194304 
Public Const FILE_ATTRIBUTE_RECALL_ON_OPEN = 262144 
Public Const FILE_ATTRIBUTE_REPARSE_POINT = 1024 
Public Const FILE_ATTRIBUTE_SPARSE_FILE = 512 
Public Const FILE_ATTRIBUTE_SYSTEM = 4 
Public Const FILE_ATTRIBUTE_TEMPORARY = 256 
Public Const FILE_ATTRIBUTE_VIRTUAL = 65536 
  
Public Sub Main ()
 Dim Ret as Integer
 Dim OutPut As String
 Output = Command() & " - " & vbtab & vbtab
 Ret = GetFileAttributesW(Command())
 If Ret = -1 Then 
  Console.writeline("Error " & err.lastdllerror)
 Else
  If (Ret And FILE_ATTRIBUTE_ARCHIVE) = FILE_ATTRIBUTE_ARCHIVE Then Output = OutPut & "Archive "
  If (Ret And FILE_ATTRIBUTE_COMPRESSED) = FILE_ATTRIBUTE_COMPRESSED Then Output = OutPut & "Compressed "
  If (Ret And FILE_ATTRIBUTE_DEVICE) = FILE_ATTRIBUTE_DEVICE Then Output = OutPut & "Device "
  If (Ret And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then Output = OutPut & "Directory "
  If (Ret And FILE_ATTRIBUTE_ENCRYPTED) = FILE_ATTRIBUTE_ENCRYPTED Then Output = OutPut & "Encrypted "
  If (Ret And FILE_ATTRIBUTE_HIDDEN) = FILE_ATTRIBUTE_HIDDEN Then Output = OutPut & "Hidden "
  If (Ret And FILE_ATTRIBUTE_INTEGRITY_STREAM) = FILE_ATTRIBUTE_INTEGRITY_STREAM Then Output = OutPut & "Integrity_Stream "
  If (Ret And FILE_ATTRIBUTE_NORMAL) = FILE_ATTRIBUTE_NORMAL Then Output = OutPut & "Normal "
  If (Ret And FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) = FILE_ATTRIBUTE_NOT_CONTENT_INDEXED Then Output = OutPut & "Not_Content_Indexed "
  If (Ret And FILE_ATTRIBUTE_NO_SCRUB_DATA) = FILE_ATTRIBUTE_NO_SCRUB_DATA Then Output = OutPut & "No_Scrub_Data "
  If (Ret And FILE_ATTRIBUTE_READONLY) = FILE_ATTRIBUTE_READONLY Then Output = OutPut & "ReadOnly "
  If (Ret And FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS) = FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS Then Output = OutPut & "Recall_On_Data_Access "
  If (Ret And FILE_ATTRIBUTE_RECALL_ON_OPEN) = FILE_ATTRIBUTE_RECALL_ON_OPEN Then Output = OutPut & "Recall_On_Open "
  If (Ret And FILE_ATTRIBUTE_REPARSE_POINT) = FILE_ATTRIBUTE_REPARSE_POINT Then Output = OutPut & "Reparse "
  If (Ret And FILE_ATTRIBUTE_SPARSE_FILE) = FILE_ATTRIBUTE_SPARSE_FILE Then Output = OutPut & "Sparse "
  If (Ret And FILE_ATTRIBUTE_SYSTEM) = FILE_ATTRIBUTE_SYSTEM Then Output = OutPut & "System "
  If (Ret And FILE_ATTRIBUTE_TEMPORARY) = FILE_ATTRIBUTE_TEMPORARY Then Output = OutPut & "Temporary "
  If (Ret And FILE_ATTRIBUTE_VIRTUAL) = FILE_ATTRIBUTE_VIRTUAL Then Output = OutPut & "Virtual "
 End If
 Console.writeline(OutPut)
End Sub
End Module 

Saturday, 11 May 2019

SetWindowText.exe sets the titlebar text for 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

@Echo Off
Echo Two files follow
Echo SetWindowText.bat
Echo This file compiles SetWindowText.vb to SetWindowText.exe using the system VB.NET compiler.
Echo SetWindowText.exe sets the titlebar text for a window
Echo     SetWindowText.exe "OldWindowTitle" "NewWindowTitle"
Echo EG Open notepad and type in a command prompt
Echo     SetWindowText.exe "Untitled - Notepad" "My Window Title"
Echo ----------------------------------------------------------------------
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\SetWindowText.exe" "%~dp0\SetWindowText.vb" 
pause



Imports System
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module MyApplication  


Public Declare UNICODE Function FindWindowW Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

Public Declare UNICODE Function SetWindowTextW Lib "user32" (ByVal hwnd As IntPtr, ByVal lpString As String) As Integer

Sub Main()
On Error Resume Next
Dim CmdLine As String
Dim Ret as Integer
Dim A() as String
Dim hwindows as IntPtr

CmdLine = Command()
If Left(CmdLine, 2) = "/?" Then
    MsgBox("Usage:" & vbCrLf & vbCrLf & "SetText ""OldWindowTitle"" ""NewWindowTitle""")
Else
    A = Split(CmdLine, Chr(34), -1, vbBinaryCompare)
    hwindows = FindWindowW(vbNullString, A(1))
    Ret = SetWindowTextW(hwindows, A(3))
End If
End Sub
End Module


Friday, 10 May 2019

TopMost.exe set a window on top or not

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

@Echo Off
Echo TopMost.bat
Echo This file compiles TopMost.vb to TopMost.exe
Echo TopMost.exe set a window on top or not
Echo To use 
Echo     TopMost Top ^
Echo     TopMost Not ^
Echo E.G.
Echo     TopMost Top Untitled - Notepad
Echo -----------------------------------------------------
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\TopMost.exe" "%~dp0\TopMost.vb" 
pause



'TopMost.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 SWP_NOMOVE = &H2
 Public Const SWP_NOSIZE = &H1
 Public Const HWND_NOTOPMOST = -2


 Sub Main()
  On Error Resume Next
  Dim hWindows as IntPtr
  Dim CmdLine as String
  Dim Ret as Integer
  CmdLine = Mid(Command(),5)
  hwindows = FindWindowW(vbNullString, CmdLine)
  If hwindows = 0 then
   Msgbox(Cmdline & " cannot be found.")
  Else
   If LCase(Left(Command(), 3)) = LCase("Top") then
    Ret = SetWindowPos(hwindows, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE)
    If Ret = 0 Then MsgBox("Set Pos Error is " & Err.LastDllError)
   ElseIf LCase(Left(Command(), 3)) = LCase("Not") then
    Ret = SetWindowPos(hwindows, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE)
    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