[VB2008] Afficher ce que j'écoute dans MSN

Résolu
cs_Vince300 Messages postés 11 Date d'inscription lundi 10 mars 2008 Statut Membre Dernière intervention 16 mars 2010 - 14 mars 2010 à 17:56
cs_Vince300 Messages postés 11 Date d'inscription lundi 10 mars 2008 Statut Membre Dernière intervention 16 mars 2010 - 16 mars 2010 à 19:21
Bonjour

Voilà, j'essaye de faire afficher "Ce que j'écoute" dans msn messenger (juste en dessous du message perso). J'utilise ce code :
Imports System.Runtime.InteropServices

Public Class MSNMsgSend
<DllImport("user32", EntryPoint:="SendMessageA")> _
Public Shared Function SendMessage(ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef lParam As COPYDATASTRUCT) As Integer
End Function
<DllImport("user32", EntryPoint:="FindWindowExA")> _
  Public Shared Function FindWindowEx(ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
End Function
Public Const WM_COPYDATA As Short = &H4A
Public Shared data As New COPYDATASTRUCT
Public Structure COPYDATASTRUCT
Public dwData As Long
Public cbData As Long
Public lpData As Long
End Structure

Public Shared Function VarPtr(ByVal e As Object) As Integer
Dim GC As GCHandle = GCHandle.Alloc(e, GCHandleType.Pinned)
Dim gc_32 As Integer = GC.AddrOfPinnedObject().ToInt32
GC.Free()
Return gc_32
End Function

Public Shared Sub SendMSNMessage(ByVal enable As Boolean, ByVal category As String, ByVal message As String)
'\0category\0enabled\0{0}\0message\0
Dim buffer As String = "\0" & category & "\0" & If(enable, "1", "0") & "\0{0}\0" & message & "\0" & vbNullChar
Dim handle As Integer = 0
data.dwData = &H547S
data.lpData = VarPtr(buffer)
data.cbData = Len(buffer) * 2
handle = FindWindowEx(0, 0, "MsnMsgrUIManager", Nothing)
If handle > 0 Then
SendMessage(handle, WM_COPYDATA, 0, data)
End If
End Sub

End Class
'Pour changer la musique affichée
SendMSNMessage(True, "Music", "MaMusique - MonArtiste")

Aucune exception n'est levée mais il ne se passe rien dans MSN, et en regardant les messages windows avec Spy++, aucun message n'arrive alors que ça a marché quelques fois au début.

Merci d'avance

1 réponse

cs_Vince300 Messages postés 11 Date d'inscription lundi 10 mars 2008 Statut Membre Dernière intervention 16 mars 2010
16 mars 2010 à 19:21
Bon ben en changeant 2-3 trucs ça marche. Enfin, jusqu'à la prochaine fois...
Imports System.Text.Encoding
Imports System.Runtime.InteropServices

Public Class MSNMsgSend
<DllImport("user32", EntryPoint:="SendMessageA")> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As UInteger, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
End Function
<DllImport("user32", EntryPoint:="FindWindowExA")> _
  Private Shared Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
End Function
Private Const WM_COPYDATA As Short = &H4A
<MarshalAs(UnmanagedType.Struct)> _
 Private Shared data As New COPYDATASTRUCT
<StructLayout(LayoutKind.Sequential)> _
 Public Structure COPYDATASTRUCT
Public dwData As Integer
Public cbData As Integer
Public lpData As IntPtr
End Structure

Private Shared Function StrPtr(ByVal e As String) As IntPtr
Return Marshal.StringToHGlobalAuto(e)
End Function
Private Shared Function StructPtr(ByVal e As Object) As IntPtr
Dim ptr As IntPtr
ptr = Marshal.AllocHGlobal(Marshal.SizeOf(e))
Marshal.StructureToPtr(e, ptr, True)
Return ptr
End Function
'Procédure pour changer "Ce que j'écoute"
'Enable = Afficher le champ ce que j'écoute
'FormatStr = Format utilisé par msn, du style {0} - {1} ({2})
'arg0 = Le titre de la chanson
'arg1 = L'artiste
'arg2 = L'album (non utilisé)
Public Shared Sub SendMSNMessage(ByVal enable As Boolean, ByVal formatStr As String, ByVal arg0 As String, ByVal arg1 As String, ByVal arg2 As String)
Dim buffer As String = "\0Music\0" & If(enable, "1", "0") & "\0" & formatStr & "\0" & arg0 & "\0" & arg1 & "\0" & arg2 & "\0" & vbNullChar
Dim handle As IntPtr = IntPtr.Zero
data.dwData = &H547
data.lpData = StrPtr(buffer)
data.cbData = Len(buffer) * 2
handle = FindWindowEx(IntPtr.Zero, handle, "MsnMsgrUIManager", Nothing)
If handle <> IntPtr.Zero Then
Dim ptr As IntPtr = StructPtr(data)
SendMessage(handle, WM_COPYDATA, FrmMain.Handle.ToInt32, ptr)
Marshal.FreeHGlobal(ptr)
End If
Marshal.FreeHGlobal(data.lpData)
End Sub
End Class
3
Rejoignez-nous