Shoutbox

Change Now Playing From Visual Basic .NET - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Skype & Live Messenger (/forumdisplay.php?fid=10)
+----- Thread: Change Now Playing From Visual Basic .NET (/showthread.php?tid=79266)

Change Now Playing From Visual Basic .NET by andrewdodd13 on 11-22-2007 at 02:59 PM

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?
RE: Change Now Playing From Visual Basic .NET by SuNcO on 11-23-2007 at 04:12 AM

On the same forum i see the answer (first i try to convert my own code but then i found it.. so i adapt to make it work)

code:
Public Class Form1

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 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 Structure COPYDATASTRUCT
        Dim dwData As Integer
        Dim cbData As Integer
        Dim lpData As Integer
    End Structure

    Private Const WM_COPYDATA As Short = &H4AS

    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

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PonerMusica(TextBox1.Text)
    End Sub

    Public Sub PonerMusica(ByVal Texto)
        'Public Sub PonerMusica(ByRef r_sArtist As String, ByRef r_sAlbum As String, ByRef r_sTitle As String, Optional ByRef r_sWMContentID As String = vbNullString, Optional ByRef r_sFormat As String = "{0}", Optional ByRef r_bShow As Boolean = True)
        Dim udtData As COPYDATASTRUCT
        Dim sBuffer As String
        Dim hMSGRUI As Integer

        'sBuffer = "\0Music\0" & 1 & "\0" & "{0}" & "\0" & Texto & "\0" & "" & "\0" & "" & "\0" & vbNullString & "\0" & vbNullChar
        sBuffer = "\0Music\0" & System.Math.Abs(CInt(True)) & "\0" & "{1} - {0}" & "\0" & Texto & "\0" & "" & "\0" & "" & "\0" & vbNullString & "\0" & vbNullChar

        udtData.dwData = &H547
        udtData.lpData = VarPtr(sBuffer)
        udtData.cbData = Len(sBuffer) * 2

        Do
            ' Buscamos la seccion del Msn donde se pone la musica

            hMSGRUI = FindWindowEx(0, hMSGRUI, "MsnMsgrUIManager", vbNullString)

            ' La encontramos ?
            If (hMSGRUI > 0) Then
                ' Esta es la linea que envia el Texto
                Call SendMessage(hMSGRUI, WM_COPYDATA, 0, VarPtr(udtData))
            End If
        Loop Until (hMSGRUI = 0)
    End Sub
End Class


Source: http://forums.fanatic.net.nz/index.php?s=&showtop...w=findpost&p=83457
RE: Change Now Playing From Visual Basic .NET by ShawnZ on 11-23-2007 at 06:02 AM

dt's now playing script is dodgy.

try this:

"WMP\0Music\01\0{0} - {1}\0Song Name\0Artist Name\0Album Name\0{00000000-0000-0000-0000-000000000000}\0"


RE: Change Now Playing From Visual Basic .NET by andrewdodd13 on 11-23-2007 at 06:07 PM

Sun: That's the exact same functions, etc, that I had, except it's got the variable names and comments in Spanish.

ShawnZ: Thanks for that... but it doesn't do anything either.

What's the name of the program you can use to monitor if a message is being sent to a process?


RE: Change Now Playing From Visual Basic .NET by SuNcO on 11-23-2007 at 06:27 PM

The code i give you works on VB 2005, i test it with Msn 8.5

The code you put here is for VB6 (VarPtr and LenB not exist on VB 2005)

What error you get ? You copy/paste mine or just see that is "the same" ?


RE: Change Now Playing From Visual Basic .NET by andrewdodd13 on 11-23-2007 at 06:34 PM

There is no error. It's just that nothing happens. And the code in my post is the exact same as yours, with different variable names - it uses Import for example, which is not available in VB6.

The post you linked was the exact same one I quoted, just mines was LoFi. There are two VB codes on that page, one is for VB6, the one I quoted .NET.


RE: Change Now Playing From Visual Basic .NET by SuNcO on 11-23-2007 at 07:29 PM

If after check the code still not working, maybe is because you have not enabled the option "Show what im listen to" on Msn

As i say, works fine here.. copy & paste all please


RE: RE: Change Now Playing From Visual Basic .NET by andrewdodd13 on 11-23-2007 at 07:52 PM

quote:
Originally posted by SuNcO
If after check the code still not working, maybe is because you have not enabled the option "Show what im listen to" on Msn

As i say, works fine here.. copy & paste all please
That wasn't the problem. COPYDATASTRUCTURE somehow was converted to long from Integer when I moved my project from VB 2005 to 2008. Stupid VB.

Thanks for the help.