public static Process FindSpotifyProcess() { if (logger.IsDebugEnabled) { logger.Debug("Looking for Jirafy process..."); } List <Process> spotifyProcesses = Process.GetProcessesByName(ProcessName).ToList(); List <Process> windowedProcesses = spotifyProcesses.Where(p => p.MainWindowHandle != IntPtr.Zero).ToList(); if (windowedProcesses.Count > 1) { IEnumerable <string> classNames = windowedProcesses.Select(p => $"\"{NativeWindows.GetClassName(p.MainWindowHandle)}\""); logger.Warn($"More than one ({windowedProcesses.Count}) \"{ProcessName}\" process has a non-null main window: {string.Join(", ", classNames)}"); } Process process = windowedProcesses.FirstOrDefault(); // If none of the Spotify processes found has a valid MainWindowHandle, // then Spotify has probably been minimized to the tray: we need to check every window. if (process == null) { foreach (Process p in spotifyProcesses) { if (IsMainSpotifyProcess((uint)p.Id)) { return(p); } } } return(process); }
public static IntPtr GetMainWindowHandle(uint pid) { if (pid == 0) { return(IntPtr.Zero); } List <IntPtr> windows = NativeWindows.GetProcessWindows(pid); List <IntPtr> possibleMainWindows = windows.Where(h => { string className = NativeWindows.GetClassName(h); string windowName = NativeWindows.GetWindowTitle(h); return(!string.IsNullOrWhiteSpace(windowName) && spotifyMainWindowNames.Contains(className)); }).ToList(); if (possibleMainWindows.Count > 1) { IEnumerable <string> classNames = possibleMainWindows.Select(h => $"\"{NativeWindows.GetClassName(h)}\""); logger.Warn($"More than one ({possibleMainWindows.Count}) possible main windows located for Spotify: {string.Join(", ", classNames)}"); } return(possibleMainWindows.FirstOrDefault()); }
public static bool IsMainSpotifyProcess(uint pid) { List <IntPtr> windows = NativeWindows.GetProcessWindows(pid); IntPtr hWnd = windows.FirstOrDefault(h => spotifyMainWindowNames.Contains(NativeWindows.GetClassName(h))); return(hWnd != IntPtr.Zero); }