Click here to Skip to main content
15,893,508 members
Articles / Programming Languages / Visual Basic

Retrieve the Winamp song title with .NET

Rate me:
Please Sign up or sign in to vote.
4.40/5 (17 votes)
5 Jan 20042 min read 118.8K   495   39  
An article on Winamp and Windows API
Module EntryPoint

    Private Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, _
                                                               ByVal lpWindowName As String) As IntPtr
    Private Declare Auto Function GetWindowText Lib "user32" (ByVal hwnd As IntPtr, _
                                                              ByVal lpString As String, _
                                                              ByVal cch As Integer) As Integer

    Private Const lpClassName = "Winamp v1.x"
    Private Const strTtlEnd = " - Winamp"

    Private Function GetWinampSong() As String
        Dim hwnd As IntPtr = FindWindow(lpClassName, vbNullString)
        If hwnd.Equals(IntPtr.Zero) Then Return "Not running"

        Dim lpText As New String(Chr(0), 100)
        Dim intLength As Integer = GetWindowText(hwnd, lpText, lpText.Length)

        If (intLength <= 0) OrElse (intLength > lpText.Length) Then Return "Unknown"

        Dim strTitle As String = lpText.Substring(0, intLength)
        Dim intName As Integer = strTitle.IndexOf(strTtlEnd)
        Dim intLeft As Integer = strTitle.IndexOf("[")
        Dim intRight As Integer = strTitle.IndexOf("]")

        If (intName >= 0) AndAlso (intLeft >= 0) AndAlso (intName < intLeft) AndAlso _
           (intRight >= 0) AndAlso (intLeft + 1 < intRight) Then _
            Return strTitle.Substring(intLeft + 1, intRight - intLeft - 1)

        If (strTitle.EndsWith(strTtlEnd)) AndAlso (strTitle.Length > strTtlEnd.Length) Then _
            strTitle = strTitle.Substring(0, strTitle.Length - strTtlEnd.Length)

        Dim intDot As Integer = strTitle.IndexOf(".")
        If (intDot > 0) AndAlso (IsNumeric(strTitle.Substring(0, intDot))) Then _
            strTitle = strTitle.Remove(0, intDot + 1)

        Return strTitle.Trim
    End Function

    Public Sub Main()
        Console.WriteLine(GetWinampSong())
        Console.ReadLine()
    End Sub

End Module

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions