Solved

sendmessage() notepad

Posted on 2008-10-16
8
1,318 Views
Last Modified: 2010-12-28
I'm sending a message to notepad.  I want to be able to understand the FindWindow function so I'm asking for the integer that it is returning to the variable hwnd to be displayed in notepad.  It is doing this and is printing the value à.  When I look at the value that I think it should be returing through a program called Winspector it isn't matching.  Does anyone have any suggestions.

Public Class Form1
 
    Private Declare Ansi Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
    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
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
 
    Public Const WM_CHAR = &H102
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        ' assuming you have notepad open.
        Dim hwnd As Integer = FindWindow(vbNullString, "Untitled - NotePad")
        Dim x As Integer = FindWindowEx(hwnd, 0, "Edit", vbNullString)
 
        ' send some keys
        SendMessage(x, WM_CHAR, hwnd, 0)
 
    End Sub
 
End Class

Open in new window

0
Comment
Question by:meteorelec
  • 5
  • 3
8 Comments
 
LVL 10

Accepted Solution

by:
c0ldfyr3 earned 500 total points
ID: 22729503
Hey,

FindWindowEx returns a window handle, not a character, so it should be something like 2954232. When you use SendMessage you are telling it to send the message WM_CHAR, and the character code is 2954232 so it isn't showing you anything relative at all. Try this...



Public Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
 
Public Const WM_SETTEXT = &HC
 
Call SendMessageByString(x, WM_SETTEXT, 0&, CStr(hwnd)) 'Not sure if CStr is a function in VB7?'

Open in new window

0
 
LVL 2

Author Comment

by:meteorelec
ID: 22729577
That worked a treat.  Now if I was to use this value to send the message to the window to send a message to it would it work?

Dim hwnd As Integer = FindWindow(vbNullString, 397024)

397024 is the number that was returned to hwnd for notepad.
0
 
LVL 10

Expert Comment

by:c0ldfyr3
ID: 22729675
I don't understand your question?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:meteorelec
ID: 22729771
Sorry I'll rephrase if you don't understand it again I'll award the points and open a new question.  The window handle that I got when I printed it in Notepad was 331470 (this was for the edit window in notepad). what I want to know is if I know the window handle already and want to send a message to this window can I not just enter the window handle into the send message command e.g.

Instead of using the below
Call SendMessageByString(x, WM_SETTEXT, 0&, CStr(x))
I can use
Call SendMessageByString(331470, WM_SETTEXT, 0&, CStr(x))

Ultimately what I'm trying to do away with is running through the whole higherarchy such as:

Dim hwnd As Integer = FindWindow(vbNullString, "Untitled - NotePad")
Dim x As Integer = FindWindowEx(hwnd, 0, "Edit", vbNullString)

to get the window that I want to send the message to.

I hope this makes sense.
0
 
LVL 10

Expert Comment

by:c0ldfyr3
ID: 22729784
Hey,

Yes you can do that, but please bear in mind that window handles change each time you open a window. So, let's say you find the window handle for notepad, run a few things and then close and re-open notepad. The window handle you had the first time will not work on the second instance. Also, if you open two versions of notepad the window handles will be different for each!
0
 
LVL 10

Expert Comment

by:c0ldfyr3
ID: 22729785
Hey,

Yes you can do that, but please bear in mind that window handles change each time you open a window. So, let's say you find the window handle for notepad, run a few things and then close and re-open notepad. The window handle you had the first time will not work on the second instance. Also, if you open two versions of notepad the window handles will be different for each!
0
 
LVL 2

Author Comment

by:meteorelec
ID: 22729793
Ok got you, thats great and it throws a little bit of light on the situation.  Thanks for the help.
0
 
LVL 10

Expert Comment

by:c0ldfyr3
ID: 22730980
No worries, if you feel generous open another question on how to get all the handles for all the open notepad windows :D ;)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

735 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