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 

No comments:

Post a Comment