Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sendmessage() notepad

Posted on 2008-10-16
8
Medium Priority
?
1,359 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

972 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