Files
TunebladeSupressor/TuneBladeSupressor/Program.fs
2025-11-09 20:04:49 +10:00

83 lines
2.7 KiB
Forth

open System
open System.Diagnostics
open System.Runtime.InteropServices
open System.Text
[<DllImport("user32.dll", SetLastError = true)>]
extern IntPtr FindWindow(string lpClassName, string lpWindowName)
[<DllImport("user32.dll", SetLastError = true)>]
extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow)
[<DllImport("user32.dll", SetLastError = true)>]
extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount)
[<DllImport("user32.dll", SetLastError = true)>]
extern uint GetWindowThreadProcessId(IntPtr hWnd, uint& lpdwProcessId)
let getClassName (hWnd: IntPtr) =
let sb = StringBuilder(256)
if GetClassName(hWnd, sb, sb.Capacity) > 0 then
sb.ToString()
else
""
let getProcessIdFromWindow (hWnd: IntPtr) =
let mutable processId = 0u
GetWindowThreadProcessId(hWnd, &processId) |> ignore
int processId
let killTuneBlade () =
try
let processes = Process.GetProcessesByName("TuneBlade")
for proc in processes do
printfn "Killing TuneBlade process (PID: %d)..." proc.Id
proc.Kill()
proc.WaitForExit(5000) |> ignore
processes.Length > 0
with
| ex ->
printfn "Error killing TuneBlade: %s" ex.Message
false
let startTuneBlade (exePath: string) =
try
printfn "Starting TuneBlade from: %s" exePath
Process.Start(exePath) |> ignore
true
with
| ex ->
printfn "Error starting TuneBlade: %s" ex.Message
false
[<EntryPoint>]
let main argv =
let tuneBladeExe = @"C:\Program Files (x86)\TuneBlade\TuneBlade\TuneBlade.exe"
printfn "TuneBlade Auto-Restarter"
printfn "Monitoring for errors and restarting from: %s" tuneBladeExe
printfn "Press Ctrl+C to exit"
printfn ""
while true do
let hwnd = FindWindow(null, "TuneBlade")
if hwnd <> IntPtr.Zero then
let cls = getClassName hwnd
if cls = "#32770" then
printfn "TuneBlade error dialog detected - restarting application..."
if killTuneBlade() then
System.Threading.Thread.Sleep(1000)
startTuneBlade tuneBladeExe |> ignore
System.Threading.Thread.Sleep(3000)
let mutable child = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "TuneBlade")
if child <> IntPtr.Zero then
printfn "TuneBlade dialog window detected restarting application..."
if killTuneBlade() then
System.Threading.Thread.Sleep(1000)
startTuneBlade tuneBladeExe |> ignore
System.Threading.Thread.Sleep(3000)
System.Threading.Thread.Sleep(2000)
0