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

Conversation

MicahArmantrout
Copy link

I realize this is not going to work if it is minimized to systray. I don't have time bandwidth right now to worry about that but I'd love to get to it. Thanks @nachmore for the suggestion on how to make a better fix. Its not totally fixed yet but getting there

@GenesisFR
Copy link

GenesisFR commented Apr 9, 2018

I came up with the same solution myself. However, the original code will work even when Spotify is minimized:

private static IntPtr GetSpotify()
{
    return Win32.FindWindow("Chrome_WidgetWin_0", null);
}

You have to use a VS2017 tool called Spy++ to get the window class name. It changed from "SpotifyMainWindow" to "Chrome_WidgetWin_0" a month ago.

[edit]: my bad, didn't see you originally came up with the same conclusion.

@MicahArmantrout
Copy link
Author

Yeah but there could be multiple windows with "Chrome_Widgetwin_0" it's not unique enough

@GenesisFR
Copy link

GenesisFR commented Apr 9, 2018

Now that is an interesting problem, matching a process name against a given window class name. I'm gonna code it tonight using EnumWindows().

My only concern would be how to set up a test environment with multiple windows that share the same class name.

@GenesisFR
Copy link

GenesisFR commented Apr 14, 2018

https://gist.github.com/GenesisFR/de0ad5710fb5eb221b5a239e819728bf

Usage: List<IntPtr> handles = FindWindow.GetWindowHandles("Spotify", "Chrome_WidgetWin_0");

In the case of Spotify, the correct handle seems to always be the first item in the list. If this were to change, you can just pick a different index. I learnt while coding this that there is no perfect solution to this problem. Also, this method takes around 1 second to execute on my computer, versus <1ms for FindWindow().

@GenesisFR
Copy link

Optimized the method, now it takes around 7ms instead of 1 second to execute. It was caused by Process.GetProcessById() (and to a lesser extent process.ProcessName) that was super slow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants