I've tried this code in VS 2003 and 2005, and VB 2008. It doesn't work in any of them.
Source:
http://forums.fanatic.net.nz/lofiversion/index.php/t11311.html
code:
Imports System.Runtime.InteropServices
Module mdlMain
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd As Integer, _
ByVal hWndChild As Integer, ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As Integer, ByVal wMsg As Integer, _
ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const WM_COPYDATA As Short = &H4AS
Private Structure COPYDATASTRUCT
Dim dwData As Long
Dim cbData As Long
Dim lpData As Long
End Structure
Public Function VarPtr(ByVal o As Object) As Integer
Dim GC As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(o, System.Runtime.InteropServices.GCHandleType.Pinned)
Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32
GC.Free()
Return ret
End Function
Public Sub SetMSNNowPlaying(ByVal sTitle As String, ByVal sArtist As String)
Dim sOutput As String = "\0Music\01\0" + sTitle + " - " + sArtist + "\0\0\0\0\0" + vbNullChar
Dim udtData As COPYDATASTRUCT
Dim iMessengerHWnd As Long = 0
udtData.dwData = &H547S
udtData.lpData = VarPtr(sOutput)
udtData.cbData = Len(sOutput) * 2
Do
iMessengerHWnd = FindWindowEx(0, iMessengerHWnd, "MsnMsgrUIManager", vbNullString)
If iMessengerHWnd > 0 Then
SendMessage(iMessengerHWnd, WM_COPYDATA, 0, VarPtr(udtData))
End If
Loop Until iMessengerHWnd = 0
End Sub
End Module
Using Debug statements, I can see that the Messenger Window is found, and that the SendMessage function is returning 0 - which I assume means it went okay. But nothing happens to my PSM. Whats up with that?
Also, for further debugging - the sOuput string EXACTLY matches that passed by -dt-'s Now Playing Script, as does the hWnd of the Window. So I would imagine that the cock up is to do with the implementation of VarPtr. Anyone any ideas?