Solved

Using SendMessage to execute a command in another app.

Posted on 2006-06-16
4
2,104 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

910 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

19 Experts available now in Live!

Get 1:1 Help Now