Solved

Using SendMessage to execute a command in another app.

Posted on 2006-06-16
4
2,128 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

691 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