I needed a way to get what WMP was currently playing, but I couldn't find any info on it so I had to do it myself
The code is based on THIS method which gets the same info from Winamp. The difference is while Winamp has the "Artist - Title"-part
in the caption by default, WMP does not. Unless, and I found this by accident, you use a skin. In my case the skin called "Proffessional" which came with WMP worked.
While the others didn't, I guess there are more skins this will work with.
VB Code:
Option Explicit Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _ (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Private Declare Function GetWindowTextLength Lib "user32" _ Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Function getWMP() Dim WMP As Long, StrSong As String, BufferLen As Long WMP& = FindWindow("WMP Skin Host", vbNullString) 'caption WMP has in skin-mode If WMP& = 0& Then 'the window was not found getWMP = "window not found1" Else ' window found BufferLen& = GetWindowTextLength(WMP&) StrSong$ = Space$(BufferLen& + 1) Call GetWindowText(WMP&, StrSong$, Len(StrSong$)) If InStr(StrSong$, "Windows Media Player") Then getWMP = "window not found2" 'checks if the window has that caption, if it does, you're using a skin that doesn't work Else getWMP = StrSong 'call getWMP End If End If End Function ' can be used like this: Private Sub Command1_Click() Text1.Text = getWMP End Sub
Comments are MORE than welcome! Would be nice to know if it works with other skins, other versions of WMP (Mine is 10.0.0.3646),
or maybe on how to improve the code.





Reply With Quote