diff --git a/TuneBladeSupressor/Program.fs b/TuneBladeSupressor/Program.fs index ac78298..5131fa0 100644 --- a/TuneBladeSupressor/Program.fs +++ b/TuneBladeSupressor/Program.fs @@ -1,4 +1,5 @@ open System +open System.Diagnostics open System.Runtime.InteropServices open System.Text @@ -12,9 +13,7 @@ extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpsz extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount) [] -extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam) - -let WM_CLOSE = 0x0010u +extern uint GetWindowThreadProcessId(IntPtr hWnd, uint& lpdwProcessId) let getClassName (hWnd: IntPtr) = let sb = StringBuilder(256) @@ -23,23 +22,62 @@ let getClassName (hWnd: IntPtr) = 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 + [] -let main _ = - printfn "Running TuneBlade error suppressor..." +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 — closing." - PostMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero) |> ignore + 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") - while child <> IntPtr.Zero do - printfn "Closing TuneBlade dialog window..." - PostMessage(child, WM_CLOSE, IntPtr.Zero, IntPtr.Zero) |> ignore - child <- FindWindowEx(IntPtr.Zero, child, "#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 + 0 \ No newline at end of file