Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Windows Messages - SendMessage (Finding/Sending Windows Messages) - By Ace
#1
http://msdn.microsoft.com/en-us/library/...85%29.aspx
http://msdn.microsoft.com/en-us/library/...85%29.aspx

I was fooling around with the FindWindow function and found out how to send windows messages to your system. With this method, you can actually press buttons on external programs of your choice and do almost anything you want inside of them.

Code:
Public Class Form1

    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
        (ByVal lpClassName As String, ByVal lpWindowName As String) _
        As IntPtr

    Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" _
        (ByVal hWndParent As Integer, ByVal hWndChildAfter As Integer, ByVal lpszClass As String, ByVal lpszWindow As String) _
        As Integer

    Private Declare Function SendMSG Lib "user32.dll" Alias "SendMessageA" _
        (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) _
        As Integer

    'Declare constant Windows Message Set Text with the int value of 12
    Private Const WM_SETTEXT As Integer = &HC

    'hwnd is now a system handle and Int_hwnd
    Private hwnd As IntPtr
    Private Int_hwnd As IntPtr

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'setting lpwindowname with the value of textbox1.text for the window we are trying to "find"
        hwnd = FindWindow(vbNullString, TextBox1.Text)
        'send a windows message to set the text of the window with Textbox2.text
        SendMSG(hwnd, WM_SETTEXT, 0, TextBox2.Text)

        'Here we set Textbox3.text with the value of the specified new "title" we
        'inputted earlier so that we don't have to input the new "title" twice
        '(optional) but it helps
        TextBox3.Text = TextBox2.Text
    End Sub

    'Same idea below but we use the handler defined for Int_hwnd instead
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        hwnd = FindWindow(vbNullString, TextBox3.Text)
        Int_hwnd = FindWindowEx(hwnd, 0, "Edit", vbNullString)
        SendMSG(Int_hwnd, WM_SETTEXT, 0, TextBox4.Text)
    End Sub

End Class

&HC is just a hexadecimal value of "12"

You can have the code as this instead if you wanted, but hexidecimal is much easier in my opinion:
Code:
Private Const WM_SETTEXT As Integer = 12


Previews:
[Image: ickQQO.png]

[Image: ickXpm.png]

[Image: ie1b6Y.gif]



One more thing:

If you aren't familiar with hexidecimal you can cheat and use a simple vb.net code that I sometimes use.

Code:
Dim input As String = "&HC"
        Dim value As Integer = Val(input)
        MsgBox(value)

That will messagebox the result of &HC in integer format which is "12". Change "&HC" to whatever you want to find the value of, or you can use this in a reverse way to get the hexadecimal format.

basically by calling the windows api, we should be able to invoke the windows message to be able to send it.

Reading windows messages is even easier, as we don't have to call the windows api to do it.
Reply
#2
Looks quite nice, well done. I may release a few of my random VB6 programs soon.
Reply
#3
(07-30-2011, 12:47 AM)Believable Wrote: Looks quite nice, well done. I may release a few of my random VB6 programs soon.

VB6 is really old, but i'd like to see some stuff from you Smile
Reply
#4
Nice tutorial. I need to write a tutorial for VB.
Coder | Skype: EleganceHF | AIM: Chief
Reply
#5
That is actually pretty neat. I could use this to mess with a few people, thanks.
Reply
#6
That's a pretty good post.
If you are willing to join SF Webmasters.
Reply
#7
I was fooling around some more with it today, checking different windows messages and processes with Spy++ and I found out how to send the windows message to close the program with the window Title in Textbox1.Text

Here is my code:
Code:
Dim hWnd As Integer
hWnd = FindWindow(vbNullString, TextBox1.Text)
SendMSG(hWnd, WM_CLOSE, 0, 0)

If you add that as a button, and you open Notepad and type "Untitled - Notepad" into Textbox1 before pressing the button, it will close notepad, assuming the title of that window for notepad is "Untitled - Notepad" in the Notepad class.

It will actually close any window if you get the proper window name.

[Image: ijfRfg.gif]
Reply
#8
That's actually really interesting. I wonder how much of the found application you can actually control. There must be a limit I guess.
Reply
#9
(08-01-2011, 02:09 AM)Fragma Wrote: That's actually really interesting. I wonder how much of the found application you can actually control. There must be a limit I guess.

No limit at all, provided you have the right permissions though, you can do anything. There's 100s of messages that get sent just by hovering over the start menu orb, you can read all of those and send you own for the animation if you wanted.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Preview] Windows 7 Tools [VB.NET] ƃu∀ ıʞƃu∀ 4 3,649 10-14-2014, 11:55 AM
Last Post: 'Onam
  [Discuss]Console Coding Or Windows Form? [Discuss] Business.Gov 6 2,212 10-01-2012, 11:27 PM
Last Post: XDarkCoder
  Download Updates & Broadcast Messages SOURCE kpn37z 0 627 06-24-2012, 04:39 AM
Last Post: kpn37z
  Code Challenge - Created by Ace AceInfinity 5 1,713 04-24-2012, 10:27 AM
Last Post: AceInfinity
  File Profiler Demo - Preview - Developed by Ace AceInfinity 4 1,420 12-30-2011, 08:17 PM
Last Post: AceInfinity

Forum Jump:


Users browsing this thread: 1 Guest(s)