Solved

sendmessage() notepad

Posted on 2008-10-16
8
1,315 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

821 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