Solved

sendmessage() notepad

Posted on 2008-10-16
8
1,312 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now