Link to home
Start Free TrialLog in
Avatar of mikesteribar
mikesteribar

asked on

SendMessage to send text to any application - not just notepad

Hi,
I need to stop using sendkeys to send text to other applications on the same machine (mostly because it doesn't work properly with ie7).

I decided to use the SendMessage api function and have it working with notepad.  However, to fully replace the sendkeys functionality, it needs to work with other applications as well, e.g. ieX, wordpad, excel etc.

My code is below.  The problem is that in the case of notepad, I first find the window handle for notepad, then the handle for the Edit window which is the text editor window, then send the string.  But other applications don't always have a window called edit.

Is there a way to make this work just like sendkeys?  Or should I do it another way?

Bit of Code:

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
   
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal ByValByValwParam As Integer, ByVal lParam As String) As Integer
   
Private Const WM_SETTEXT = &HC

    Private Function SendMessage()

        Dim ihWnd As Integer = 0
        Dim ihEdit As Integer = 0
        Dim phEdit As IntPtr = Nothing

        'get the notepad window handle    
        ihWnd = FindWindowEx(0, 0, "Notepad", vbNullString)
        'get the notepad editor window handle
        ihEdit = FindWindowEx(ihWnd, 0, "EDIT", vbNullString)
        'change to intptr for SendMessage to work
        phEdit = New IntPtr(ihEdit)
        'send the string "hello", returns 1 for ok, 0 for fail
        Dim iRet As Integer = SendMessage(phEdit, WM_SETTEXT, 0, "Hello")

    End Function


Hope you can help...
Mike.
Avatar of tmwest
tmwest

Is there a reason you are using sendkey or sendmessage instead of writing to a text file or using Excel or other apps APIs?
Avatar of mikesteribar

ASKER

Hi tmwest, yes, the app has other export options to write to text files, excel etc as well.  But some users of it also like to use sendkeys so that they can send the data to any application at all, eg if they have an old existing bespoke program that expects keystrokes.
Avatar of Mike Tomlinson
"But other applications don't always have a window called edit."

Exactly.  So the answer is no.  SendMessage() requires a handle to a specific window and there isn't any way to do this generically for ALL applications.


damn, I also understand sendkeys won't work at all in Vista.  

Surely there must be some way of reproducing its behaviour?  

It was such a useful and very simple thing - seems like kind of a step back.



I found a way to do it with api commands at last :)

There is a good example which just takes a little tweaking to work in vb.net on:
http://www.visualbasic.happycodings.com/API_and_Miscellaneous/code38.html

I'll split the points unless anyone comes up with anything better.

Thanks,
mike.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Idle mind,
Thanks for your time and advice.

I appreciate what you are saying about the various methods requiring the target app to have focus, but this is ok in this instance.  I really just needed to replace exactly the sendkeys functionality so that it would work with ie7 and future apps - I didn't need to improve on sendkeys.
The main problem was that if you use sendkeys with ie7 then if you send e.g. "abc123" then it would intermittantly come out with "abbbc123" or similar.  I guess I didn't make that clear enough in my question.
The only other thing I will do is to ensure that a window does have focus, other than the desktop and that it is not my own application.
Cheers,
Mike.
ps.  I did use sendinput as advised...