Solved

sendmessage() notepad

Posted on 2008-10-16
8
1,320 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
[X]
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
  • 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

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…

751 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