Solved

Using SendMessage to execute a command in another app.

Posted on 2006-06-16
4
2,101 Views
Last Modified: 2013-12-25
I am using an app which has a window where if you type "@my.hcl <cr>",  it goes off and executes that file.  Using FindWindow and FindWindowEx, I am able to find the window's handle and using SendMessageBy String, I am able to write the "@my.vcl" into the window.  I am not able to get it to execute the command as if I typed it into the window directly though.  This is the command line I use to send the message:

slength = SendMessageByString(lhwnd6, WM_SETTEXT, -1, "VCL>@my.vcl" & Chr(13) & Chr(10))

The text "@my.vcl" and a cariiage return appear in the window but it doesn't do anything.  What am I missing?  Also, I'd like be able to append text to the window, not replace it.

Thanks!
0
Comment
Question by:planetiowa
  • 2
4 Comments
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 16925362
I hate to say this, but you could always use send keys.

AppActivate("OtherAPplicationTitleWindow")
SendKeys("VCL>@my.vcl" & vbcrlf)


Try that.
0
 

Author Comment

by:planetiowa
ID: 16934654
I tried that, but it only seemd to work very infrequently.  Most of the time the text is writeen into the current window (the VB project editor).  

I did some internet searchs and ran across a keybd_event api that seems to work a lot more consistently, however, I can't get the "@" to appear.  For example, if I use:

call keybd_event(64,0,0,0) '@
call keybd_event(86,0,0,0) 'V
call keybd_event(67,0,0,0) 'C
call keybd_event(76,0,0,0) 'L

I get:  vcl

without the "@" appearing.  Also, the vcl is lowercase, not uppercase for some reason??
Any ideas?

0
 
LVL 19

Accepted Solution

by:
BrianGEFF719 earned 65 total points
ID: 16940012
you need to use VK_SHIFT


take a look at this for more information on how to properly use keybd_event api.

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21279033.html
0
 
LVL 17

Assisted Solution

by:zzzzzooc
zzzzzooc earned 60 total points
ID: 16948060
Here's an example of entering a URL into the address bar of IE and then simulating an "Enter".

Form1:
--------------------
Option Explicit

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 Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2
Private Const WM_KEYDOWN = &H100
Private Const WM_SETTEXT = &HC
Private Const VK_RETURN = &HD
Private Sub Command1_Click()
    Dim wnd As Long
    Dim wnd2 As Long
    Dim wnd3 As Long
    wnd = FindWindowEx(0, 0, "IEFrame", "Yahoo! - Microsoft Internet Explorer")
    If (wnd <> 0) Then
        wnd2 = GetWindow(wnd, GW_CHILD)
        Do
            If (GetWndClass(wnd2) = "WorkerW") Then
                wnd3 = GetWindow(wnd2, GW_CHILD)
                If (GetWndClass(wnd3) = "ReBarWindow32") Then
                    wnd3 = FindWindowEx(wnd3, 0, "ComboBoxEx32", vbNullString)
                    wnd3 = FindWindowEx(wnd3, 0, "ComboBox", vbNullString)
                    wnd3 = FindWindowEx(wnd3, 0, "Edit", vbNullString)
                    If (wnd3 <> 0) Then
                        'FINALLY found the "Edit" window (where URLs are typed)
                        'set our own text in the window
                        Dim txt As String
                        txt = "http://www.google.com"
                        Call SendMessage(wnd3, WM_SETTEXT, Len(txt), ByVal txt)
                        'simulate pressing "enter"
                        Call PostMessage(wnd3, WM_KEYDOWN, VK_RETURN, 0)
                        'exit the loop
                        Exit Do
                    End If
                End If
            End If
            wnd2 = GetWindow(wnd2, GW_HWNDNEXT)
        Loop Until (wnd2 = 0)
    End If
End Sub
Private Function GetWndClass(ByVal hwnd As Long) As String
    Dim cls As String
    Dim clsLen As Integer
    cls = Space$(255)
    clsLen = GetClassName(hwnd, cls, Len(cls))
    GetWndClass = Left$(cls, clsLen)
End Function

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now