Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed how Spotify is found #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions Toastify/Spotify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ namespace Toastify
{
internal class Win32
{

[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

internal delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

[DllImport("user32.dll")]
internal static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);

[DllImport("user32.dll")]
internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

Expand Down Expand Up @@ -318,9 +319,16 @@ public static void KillSpotify()

private static IntPtr GetSpotify()
{
var windowClassName = "SpotifyMainWindow";
var procs = System.Diagnostics.Process.GetProcessesByName("Spotify");

return Win32.FindWindow(windowClassName, null);
foreach (var proc in procs)
{
if (proc.MainWindowHandle != IntPtr.Zero)
{
return proc.MainWindowHandle;
}
}
return IntPtr.Zero;
}

public static bool IsRunning()
Expand Down Expand Up @@ -429,7 +437,7 @@ public static void SetCoverArt(Song song)

if (response != null)
{
Telemetry.TrackEvent(TelemetryCategory.SpotifyWebService, Telemetry.TelemetryEvent.SpotifyWebService.NetworkError,
Telemetry.TrackEvent(TelemetryCategory.SpotifyWebService, Telemetry.TelemetryEvent.SpotifyWebService.NetworkError,
"URL: " + spotifyTrackSearchURL + " \nError Code: " + response.StatusCode + " Dump: " + e.ToString());
}
else
Expand All @@ -448,7 +456,7 @@ public static void SetCoverArt(Song song)
jsonResponse.Substring(0,
jsonResponse.Length > 100 ? 100 : jsonResponse.Length));

Telemetry.TrackEvent(TelemetryCategory.SpotifyWebService, Telemetry.TelemetryEvent.SpotifyWebService.ResponseError, "URL: " + spotifyTrackSearchURL + " \nType: " + e.GetType().Name + " JSON excerpt: " + jsonSubstring +" dump: " + e.ToString());
Telemetry.TrackEvent(TelemetryCategory.SpotifyWebService, Telemetry.TelemetryEvent.SpotifyWebService.ResponseError, "URL: " + spotifyTrackSearchURL + " \nType: " + e.GetType().Name + " JSON excerpt: " + jsonSubstring + " dump: " + e.ToString());

throw;
}
Expand Down