Shannon Shang-I think therefore I am

We came here, you and I, to this place and this profession, to be great, to do great things, and give form to great dreams - and we have

Get the playinf filename from winamp with VB

If we do that with c++,it will be a simply thing.

int index = SendMessage(hwnd_winamp, WM_USER, 0, IPC_GETLISTPOS);
char *name = SendMessage(hwnd_winamp, WM_USER, index, IPC_GETPLAYLISTFILE);

But as a VB programmer ,i want to do that with VB.Because the pointer of file in playlist is relative to winamp process.We can't access it directly.

However we can use GetWindowThreadProcessId,OpenProcess to access memory of other process.

So we can get the playing file name with vb with following code:

 


Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpszClassName As String, ByVal lpszWindowName As String) _
As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hWnd As Long, ByRef lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Const PROCESS_VM_READ As Long = &H10
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Const WM_USER As Long = &H400
Private Const IPC_GETPLAYLISTFILE As Long = 211
Private Const IPC_GETLISTPOS As Long = 125
Private Declare Function ReadProcessMemory Lib "kernel32" _
(ByVal hProcess As Long, ByVal lpBaseAddress As Long, _
ByRef lpBuffer As Any, ByVal nSize As Long, _
ByRef lpNumberOfBytesRead As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Const MAX_PATH As Long = 260
Private hWndWinamp As Long

Private Function GetWinampWindow() As Long
GetWinampWindow = FindWindow( "Winamp v1.x", vbNullString)
End Function
Public Function GetPlayingFileName() As String
Dim strFileName As String
Dim lp As Long, lpWinamp As Long
Dim iIndex As Long
Dim PID As Long
Dim bRet As Long
Dim dwRead As Long
Dim Buffer(MAX_PATH) As Byte
Dim Temp As String

hWndWinamp = GetWinampWindow
If hWndWinamp = 0 Then
GetPlayingFileName = ""
Exit Function
End If

iIndex = SendMessage(hWndWinamp, WM_USER, 0, IPC_GETLISTPOS)
lp = SendMessage(hWndWinamp, WM_USER, iIndex, IPC_GETPLAYLISTFILE)
If lp = 0 Then
GetPlayingFileName = ""
Exit Function
End If
Call GetWindowThreadProcessId(hWndWinamp, PID)
lpWinamp = OpenProcess(PROCESS_VM_READ, 0, PID)
If lpWinamp = 0 Then
GetPlayingFileName = ""
Exit Function
End If
bRet = ReadProcessMemory(lpWinamp, lp, Buffer(0), MAX_PATH, dwRead)
Call CloseHandle(lpWinamp)

Temp = StrConv(Buffer, vbUnicode)
strFileName = Left$(Temp, InStr(Temp, Chr$(0)) - 1)

GetPlayingFileName = strFileName

End Function

Comments

TrackBack said:

^_^,Pretty Good!
# April 15, 2005 1:50 PM

ch21st said:

worked for me but i had to insert "byval" into sendmessage functions like this

iIndex = SendMessage(hWndWinamp, WM_USER, byval 0, byval IPC_GETLISTPOS)
lp = SendMessage(hWndWinamp, WM_USER, iIndex, byval IPC_GETPLAYLISTFILE)

don't know why though .. but without these it returns empty string
# May 3, 2005 7:58 PM

TrackBack said:

^_~,pretty good!csharpsseeoo
# May 18, 2005 6:14 AM

TrackBack said:

Get the playinf filename from winamp with VBooeess
# June 15, 2005 9:56 PM

TrackBack said:

Get the playinf filename from winamp with VBooeess
# July 17, 2005 2:18 AM

TrackBack said:

Get the playinf filename from winamp with VBooeess
# July 31, 2005 10:20 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)