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
Solved

Using SendMessage to execute a command in another app.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

828 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