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?

As an addendum to this issue, i have tried a build of the program on several machines. the only ones that yhave problems are Windows XP based.
Windows 7 machines seem to work just fine with my sendmessage calls.

>>...button (the play button) that doesnt work, and the restore button that does work...
Just thinking here; it might have to do with the ProperCase of the "play", which should actually be capitalized as "Play".
.reason provided: In JavaScript, if you use ".innerHtml" instead of ".innerHTML" you will get no results.

m I not a programmer if i dont understand this code ?
m starting to get worried now..
i have built simple database apps before ...but this looks like alien language to me :(

>>this looks like alien language to me
.vb.net?or the api.stuff in the posted.code?

I thought I know vb.net a while ago
never heard of the following things..not even in my college textbook


System.Runtime.InteropServices,
handle,
USER32.DLL,
&HF5,

.that's "api" stuff, gets.stuff about Windows on.Desktop and does sh.t.

So since I started this thread a while back and I'm having a similar problem I figured I'd just post my status here.

So I got sendkeys to work, and as you saw I can get some of sendmessage to work.

Currently I am dealing with tryin to do a CTRL C to copy some text, but while the ALT function seems to work and the SHIFT function seems to work, i cant seem to get the CTRL function to work, what is it I am missing?

heres a sample of the code,

bear in mind that the code works fine for the ALT and SHIFT key combos, just not for the CTRL key combos...

   Thread.Sleep(1000)
            SendKeys.SendWait("+(I)")
            Thread.Sleep(1000)
            SendKeys.SendWait("+(M)")
            Thread.Sleep(1000)
            SendKeys.SendWait("+(C)")
            Thread.Sleep(1000)
            SendKeys.SendWait("+(I)")
            Thread.Sleep(1000)
            SendKeys.SendWait("^(N)")
            Thread.Sleep(1000)
            SendKeys.SendWait("^(C)")
            Thread.Sleep(1000)
            SendKeys.SendWait("%{F4}")

I just don't get it....the combos seem to work fine except CTRL

:(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.