michael447887

I am writing an application (VB.NET in VS2003) that controls a third party application in the background (both applications run as the SYSTEM user) and it uses BM_CLICK in the SendMessage Windows API Function. However, when I debug the program, my application always hangs when I try to use SendMessage but it works if I use SendKeys. I can use SendKeys while debugging because I have the applications running in the foreground so I can see the windows. SendKeys won't work when the application runs in the background. Does anyone know how I could get the SendMessage function to work


Re: Visual Basic General Application hangs when using BM_CLICK in SendMessage

TaDa

We'll need to see your code for that...



Re: Visual Basic General Application hangs when using BM_CLICK in SendMessage

michael

Code Snippet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Try

Timer1.Start()

Catch ex As Exception

Call HandleErrors(ex, 3, 1)

End Try

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Try

Timer1.Stop()

Call EncryptionProcess()

i = i + 1

Timer1.Start()

Catch ex As Exception

Call HandleErrors(ex, 3, 2)

End Try

End Sub

Private Sub ClickButton(ByVal pt As Point)

Try

Dim sb As New StringBuilder("")

SendMessage(New IntPtr(WindowFromPoint(pt)), WndMsg.BM_CLICK, 0, sb)

Catch ex As Exception

Call HandleErrors(ex, 3, 3)

End Try

End Sub

Private Sub EncryptionProcess()

Try

txtProcess.Text = txtProcess.Text & i & " - "

Dim sk As SendKeys

Select Case i

Case 0

Try 'Start D3P

prcD3P.StartInfo.FileName = "path\to\3rd\party\app.exe"

prcD3P.Start()

Catch ex As Exception

Call HandleErrors(ex, 3, 4)

End Try

Case 1

Try 'Type the admin password

Dim adminPW As String = "123456"

Dim sb As New StringBuilder(adminPW)

Dim pt As New Point(453, 396)

SendMessage(New IntPtr(WindowFromPoint(pt)), WndMsg.WM_SETTEXT, 0, sb)

Catch ex As Exception

Call HandleErrors(ex)

End Try

Case 2

Try 'Click the "OK" button

Call ClickButton(New Point(519, 489))

Catch ex As Exception

Call HandleErrors(ex, 3, 5)

End Try

Case 3

Try 'Click the "Encrypt Files" button

'{X=668,Y=447}

Call ClickButton(New Point(668, 447))

Catch ex As Exception

Call HandleErrors(ex, 3, 6)

End Try

Case 4

Try 'Set the text for the path for the files to encrypt

Dim sb As New StringBuilder(Application.StartupPath & "\Exported")

Dim pt As New Point(387, 297)

SendMessage(New IntPtr(WindowFromPoint(pt)), WndMsg.WM_SETTEXT, 0, sb)

Catch ex As Exception

Call HandleErrors(ex, 3, 7)

End Try

Case 5

Try 'Click the refresh button

'{X=485,Y=371}

Call ClickButton(New Point(485, 371))

Catch ex As Exception

Call HandleErrors(ex, 3, 8)

End Try

Case 6

Try 'Click the encrypt button again

'{X=665,Y=580}

Call ClickButton(New Point(665, 580))

Catch ex As Exception

Call HandleErrors(ex, 3, 9)

End Try

Case 7

Try 'Wait for the OK button to show up. Click it when it does

'{X=740,Y=487}

Dim GetCtrlHndl As Integer = WindowFromPoint(New Point(714, 489))

Dim sb As New StringBuilder(GetWindowTextLength(New IntPtr(GetCtrlHndl)) + 1)

GetWindowText(New IntPtr(GetCtrlHndl), sb, GetWindowTextLength(New IntPtr(GetCtrlHndl)) + 1)

If sb.ToString = "OK" Then

Call ClickButton(New Point(740, 487))

Else

i = i - 1

End If

Catch ex As Exception

Call HandleErrors(ex, 3, 10)

End Try

Case Else

Try

Me.Close()

Catch ex As Exception

Call HandleErrors(ex, 3, 19)

End Try

End Select

Catch ex As Exception

Call HandleErrors(ex)

End Try

End Sub






Re: Visual Basic General Application hangs when using BM_CLICK in SendMessage

TaDa

Try changing your sb parameter to intptr.Zero on your click sendmessage call.



Re: Visual Basic General Application hangs when using BM_CLICK in SendMessage

michael

its still hanging... it tends to hang when a new dialog of the program opens even though I am using GetWindowFromPoint to get the control




Re: Visual Basic General Application hangs when using BM_CLICK in SendMessage

TaDa

This is working fine for me, but I've hard coded the handle that I got from Spy. Have you used spy to make sure your getting the right handle Also, when using spy, make sure it's acutally a "BUTTON" and not some other class.

Code Snippet

Public Declare Auto Function SendMessage Lib "user32.dll" Alias "SendMessage" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _

ByVal wParam As Integer, ByRef lParam As IntPtr) As IntPtr

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

Const BM_CLICK As Integer = &HF5&

Dim Hnld As IntPtr = New IntPtr(&H180902)

LocalAPI.SendMessage(Hnld, BM_CLICK, 0, IntPtr.Zero)

End Sub