|
-
Oct 12th, 2006, 03:13 AM
#1
Thread Starter
New Member
I need SendMessage or PostMessage Help
I'm tryingt to send text (the word "Hello" for now) to an "Untitled - Notepad" document. I can change the title bar, but not the text field. What am I doing wrong?
I'm using VB 5.0 in WinXP OS.
VB Code:
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 FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare Function SendMessageByStr& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String)
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 PostMessageA Lib "user32" (ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function PostMessageByStr& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String)
Private Const WM_SETTEXT = &HC
'Private Const EM_SETSEL = &HC
'Private Const WM_CHAR As Long = &H102
Private Sub Command1_Click()
Dim x&
Dim richedit&
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
Ret = "Untitled - Notepad"
WinWnd = FindWindow(vbNullString, Ret)
If WinWnd = 0 Then MsgBox "Could not find the window ...": Exit Sub
MsgBox WinWnd '####PRROLLLY wanna taket his out at some point
Dim sText As String
sText = "Hello"
SendMessageByStr WinWnd, WM_SETTEXT, 0, "hello" ' PUTS hello into the title bar of the notepad. Kinda cool, but... not my goal
SendMessage WinWnd, WM_SETTEXT, 0, "hello" ' PUTS bizar characters into the App bar... Definatly, not my goal
PostMessage WinWnd, WM_SETTEXT, 0, "hello" ' Type Mismatch Error
PostMessageByStr& WinWnd, WM_SETTEXT, 0, "hello" ' Does absolutely nothing
End Sub
-
Oct 12th, 2006, 06:09 AM
#2
Re: I need SendMessage or PostMessage Help
It's been awhile, since I used VB5, but I think you need to use the FindWindowEx, to get the "edit" portion of the notepad.
I seen an example doing this somewhere.
Also, always double check quirky MsgBox areas, as they can interfere with execution flow. In VB.NET, MessageBox.Show works much better.
-
Oct 12th, 2006, 11:21 AM
#3
Re: I need SendMessage or PostMessage Help
as TTn said, you need to send it to the Edit window:
VB Code:
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 SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_SETTEXT = &HC
Private Sub Command1_Click()
Dim lhWnd As Long
lhWnd = FindWindowEx(0&, 0&, vbNullString, "Untitled - Notepad")
lhWnd = FindWindowEx(lhWnd, 0&, "Edit", vbNullString)
If lhWnd Then
SendMessage lhWnd, WM_SETTEXT, 0&, ByVal "Hello"
Else
MsgBox "No way"
End If
End Sub
-
Oct 12th, 2006, 05:00 PM
#4
Thread Starter
New Member
Re: I need SendMessage or PostMessage Help
That worked wonderfully thanks...
On to my next problem. The "Untitled - Notepad" was my test subject. The program I actualy want to send the text to obviously is designed to accept text, but apparently doesn't have the specified "Edit" field.
I hear some handy program called spy++ can get the title of the desired field from windows. I just wanna know... Where do I get this spy++? I searched these forums and the web at large and find all kinds of info on spyware and spying. Could you please point me in the right direction to learn more?
-
Oct 12th, 2006, 05:53 PM
#5
Re: I need SendMessage or PostMessage Help
Spy++ is an application that comes in Visual Studio 6, along with it's dll.
You can always spy on your own windows, by iterating through, certain class names, like "Edit", or "ComboBox" etc.
VB Code:
Dim lhWndChild As Long
Dim lhWndParent As Long
Dim i As Integer
i = 1
lhWndParent = FindWindow(vbNullString, "AppCaption")
Do
lhWndChild = FindWindowEx(lhWndParent, lhWndChild, "Edit", vbNullString)
If i = 1 Then
'do something to the first text
SendMessage lhWndChild, WM_SETTEXT, 0&, ByVal "Hello"
End If
If i = 2 Then
'do something to the second text
SendMessage lhWndChild, WM_SETTEXT, 0&, ByVal "GoodBye"
End If
If i = 3 Then
'do something to the third text
SendMessage lhWndChild, WM_SETTEXT, 0&, ByVal "Exit"
'exit loop if there are three text boxes.
Exit Do
End If
i = (i + 1)
Loop
Last edited by TTn; Oct 12th, 2006 at 11:57 PM.
-
Oct 12th, 2006, 06:56 PM
#6
Thread Starter
New Member
Re: I need SendMessage or PostMessage Help
 Originally Posted by TTn
Spy++ is an application that comes in Visual Studio 6, along with it's dll.
You can always spy on your own windows, by iterating through, certain class names, like "Edit", or "ComboBox" etc.
Damn... I guess I need to get a copy of visual studio 6.
I tried your code, and it appears to have executed correctly, but there was no text showing up.
What other kinds of class names are there? What I'm wanting to recieve the keystrokes may not even have a class name, as the app i'm trying to send the keystrokes to isn't a windows form. It is an area to enter a log in name for a video game. I've been using sendkeys and it works just fine, but I'm tired of having to AppActivate and loosing the focus on my other windows I may have open. Any other suggestions?
On the plus side, I am learning alot =) Thanks so far !!
Last edited by VB5Tony; Oct 12th, 2006 at 07:15 PM.
-
Oct 12th, 2006, 11:55 PM
#7
Re: I need SendMessage or PostMessage Help
Send me your email in a private message here, so I can send you spy++ and it's dll. You might get lost at first, but I can give you basic instruction.
It should be a window then, from how it sounds, and it should have class name/s.
VB Code:
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
'A few class names not used here
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Dim lpClassName As String
Dim RetVal As Long
'Create a buffer
lpClassName = Space(256)
'retrieve the class name
RetVal = GetClassName(lhWndParent, lpClassName, 256)
MsgBox "Classname: " + Left$(lpClassName, RetVal)
Although, you can reset the focus and foreground window after the appactivate.
Using the additional GetForegroundWindow, SetForegroundWindow, GetFocus, and SetFocus API.
But then you're still using the inferior sendkeys, which may never be sent to where you want. *Crosses fingers* So SendMessage is best here. I think.
Last edited by TTn; Oct 13th, 2006 at 12:10 AM.
-
Oct 13th, 2006, 02:09 PM
#8
Thread Starter
New Member
Re: I need SendMessage or PostMessage Help
Interesting. In my original code (at the top of the page) I dropped this in there, and it worked...
VB Code:
PostMessage WinWnd, WM_CHAR, Asc("Hello"), 0&
but it only typed "H", as it is the first character and WM_CHAR only sees one character. So I'm thinking about some kind of loop that reads each character in a string variable one at a time then posts it to the desired location until it reaches the end of the string. Problem is,.. I don't know how to read one character at a time in a string.
It would also be neat-o to send Control+Keys also.
Any advice?
-
Oct 13th, 2006, 02:53 PM
#9
Re: I need SendMessage or PostMessage Help
It is in fact Asc that only sees one character, but you are right about the solution - you need to work with just one letter at a time. To do that you can use a loop, and the Mid function, eg:
VB Code:
Dim strHello as String
Dim intCount as Integer
strHello = "Hello"
For intCount = 1 To Len(strHello)
MsgBox Asc( Mid(strHello, intCount, 1) )
Next intCount
-
Oct 16th, 2006, 10:02 AM
#10
Frenzied Member
Re: I need SendMessage or PostMessage Help
Just thought i'd chime in here.
patorjk's api spy is a great utility, and i'd recommend it to anyone working with API.
patorjk.com i think is the website, but i haven't checked in a few years sooo you might want to verify that.
I think there should be a stickied thread in this forum with a list of useful tools and websites to find them.
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|