I am experimenting with sendmessage from a simple form to control the various buttons in iTunes.
The problem is some sendmessages appear to work just fine and others don't. Now before we get into "do you have the right handles child windows etc." Heres whats weird, I can take my code to my laptop and run the exact same form with iTunes on the laptop and everything works just as the code says it should.
I go to my other p.c. and only half the sendmessage code seems to work...what is going on?
Just to cover bases, heres what i've tried, since it appears to be specific to the pc i run on, i uninstalled and reinstalled iTunes, updated vb 2010 express to latest .netframework and service pack.
Heres a sample of code with one button (the play button) that doesnt work, and the restore button that does work...although to be honest i dont think this issue is code so much as environment.
Imports System.Runtime.InteropServices
Imports System.Threading
Public Class Form1
' Get a handle to an application window.
Declare Auto Function FindWindow Lib "USER32.DLL" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
'Get childwindows for an app
Declare Auto Function FindWindowEx Lib "USER32.DLL" (ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
'Send Message to target window.
Declare Auto Function SendMessage Lib "USER32.DLL" (ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Dim WindowH As IntPtr = 0
Dim ChildWindowH As IntPtr = 0
'constant value for a mouse click, used by send message
Const BM_CLICK As Long = &HF5
Private Sub PlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayButton.Click
WindowH = FindWindow("iTunes", "iTunes")
ChildWindowH = FindWindowEx(WindowH, IntPtr.Zero, "Button", "play")
SendMessage(ChildWindowH, BM_CLICK, IntPtr.Zero, IntPtr.Zero)
End Sub
Private Sub RestoreButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RestoreButton.Click
WindowH = FindWindow("iTunes", "iTunes")
ChildWindowH = FindWindowEx(WindowH, IntPtr.Zero, "Button", "Restore")
SendMessage(ChildWindowH, BM_CLICK, IntPtr.Zero, IntPtr.Zero)
End Sub
End Class
Any ideas on whats going on here?