using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using Microsoft.Win32; namespace XbmcLauncher { static class Program { [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] private static extern bool ShowWindow(IntPtr hWnd, int cmdShow); private const int SW_SHOWMAXIMIZED = 3; /// /// The main entry point for the application. /// static void Main() { // Attempt to bring an existing XBMC to the foreground. // If none exists, open XBMC. if(!BringProcessToForeground()) OpenXbmc(); } private static bool BringProcessToForeground() { Process[] processes = Process.GetProcessesByName("XBMC"); if (processes.Length != 0) { // If XBMC is currently running, bring it to the foreground IntPtr hWnd = processes[0].MainWindowHandle; ShowWindow(hWnd, SW_SHOWMAXIMIZED); SetForegroundWindow(hWnd); return true; } return false; } private static void OpenXbmc() { string xbmcPath = null; // Attempt to find the XBMC executable location via the registry RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\XBMC"); if (key != null) // If the path is in the registry use it to open XBMC { xbmcPath = key.GetValue("") as string; if (LaunchXbmcProcess(xbmcPath + @"\XBMC.exe")) return; } else // Otherwise, we'll try to use the default locations { string x86Path = @"C:\Program Files\XBMC\XBMC.exe"; string x64Path = @"C:\Program Files (x86)\XBMC\XBMC.exe"; if (LaunchXbmcProcess(x86Path)) return; else LaunchXbmcProcess(x64Path); } } private static bool LaunchXbmcProcess(string path) { if (path != null && File.Exists(path)) { string args = ""; Process proc = new Process(); if(File.Exists("XBMCLaunchArgs.txt")) { using (StreamReader argStream = File.OpenText("XBMCLaunchArgs.txt")) { args = argStream.ReadLine(); argStream.Close(); } } proc.StartInfo = new ProcessStartInfo(path, args); proc.Start(); BringProcessToForeground(); return true; } return false; } } }