|
-
Aug 2nd, 2008, 11:26 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Using SendMessage to send "enter" to an app
I want to have a VB6 program open an app, then send a keystroke (enter) to the app. The app may or may not be in focus. I thought if I use SendMessage as shown below, this would work whether or not the app window is in focus...
SendMessage (Hwnd,WM_CHAR,13,ByVal 0)
I have two problems... first, I don't know what declarations need to be made for SendMessage. Last of all, I don't know how to get the Hwnd when I launch the app. I thought maybe it would be like this...
Hwnd = Shell("c:\Programs\MyApp.exe", vbNormalFocus)
I know this isn't correct, but I don't know how to do it. Any help would be appreciated!
-
Aug 2nd, 2008, 11:33 AM
#2
Re: Using SendMessage to send "enter" to an app
Check out the definition and code example of SendMessage.
http://allapi.mentalis.org/apilist/SendMessage.shtml
Note: You will need FindWindow and FindWindowEx to obtain the proper window handle of the textbox or button that is to recceive the keypress message.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Aug 2nd, 2008, 02:19 PM
#3
Thread Starter
Hyperactive Member
Re: Using SendMessage to send "enter" to an app
Before I started this thread, I tried this code...
http://vbnet.mvps.org/index.html?cod.../shellhwnd.htm
It opens notepad, but then nothing more happens. I also tried the link you gave, but I can't even get that to work. Granted it's probably due to my limited knowledge of VB or poor programming skills, but it seems like there should be an easier way that only requires a few lines of code to do something this simple.
Is there a better method than SendMessage to open an application (like notepad) and send a keystroke to it?
-
Aug 2nd, 2008, 02:36 PM
#4
Re: Using SendMessage to send "enter" to an app
Its by no means simple or just a few lines. Automating another app is mid level complexity.
The example should place text into notepad although shell may not be returning the PId. You could use FindWindow to be sure.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Aug 2nd, 2008, 10:37 PM
#5
Thread Starter
Hyperactive Member
Re: Using SendMessage to send "enter" to an app
Ok, I have the following code which almost seems to work. I have the classname & title for both the window and the close button I'm trying to push. I tried passing ascii 13 (enter) and ascii 20 (space bar) to try to push the close button. I don't get an error. I know I have the hwnd's correct because if I change either name, I get the message that it couldn't find the window or the button. Any idea what I'm doing wrong?
vb Code:
Option Explicit Private Const WM_CHAR = &H102 Declare Function SendMessageBynum Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long Private Sub Main() Dim HandleWin As Long Dim hwnd As Long ' Find the handle to the Reatogo window HandleWin = FindWindow("#32770", "Reatogo PE Builder v3.1.10a - Build") If HandleWin = 0 Then MsgBox "Couldn't find the window handle" Exit Sub End If ' Find the handle to close button on the window found hwnd = FindWindowEx(HandleWin, 0, "Button", "&>>") 'vbNullString) If hwnd = 0 Then MsgBox "Couldn't find the button" Exit Sub End If Call SendMessageBynum(hwnd, WM_CHAR, 13, ByVal 0) MsgBox "key sent" MsgBox hwnd End Sub
-
Aug 3rd, 2008, 01:03 AM
#6
Re: Using SendMessage to send "enter" to an app
 Originally Posted by BrianPaul
Any idea what I'm doing wrong?
Have you tried using "SendMessage" or "PostMessage" with the BM_CLICK param?
Const BM_CLICK As Long = &HF5&
PostMessage hwnd, BM_CLICK, 0, 0
-
Aug 3rd, 2008, 08:38 AM
#7
Thread Starter
Hyperactive Member
Re: Using SendMessage to send "enter" to an app
BM_CLICK worked! Thanks Edgemeal!
-
Dec 13th, 2012, 12:07 PM
#8
Registered User
Re: Using SendMessage to send "enter" to an app
 Originally Posted by BrianPaul
BM_CLICK worked! Thanks Edgemeal!
I am stuck in same kind of problem...please help me...
I am trying to click the "OK" button when it is prompted after clicking the "Update" button.
The challenge is that when I am running the full code its not clicking the OK button which comes.
But when I am clicking the "Update" button manually in Debug mode its working....!
Please help...
Set UpdateBtn = ie.document.getElementsByName("update")
UpdateBtn(0).Click 'Click Update button
Application.Wait (Now + TimeValue("00:00:02"))
For i = 1 To 2
WIN_Dialog = FindWindow(vbNullString, Dialog_Caption) 'Get the dialogbox hwnd
If WIN_Dialog = 0 Then
Exit For
End If
Dlg_ChildWIN = FindWindowEx(WIN_Dialog, 0, Button_ClassName, ButtonTxt)
SendMessage Dlg_ChildWIN, BM_CLICK, 0, 0 'click OK button
Next
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|