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


Messages In This Thread
Windows Messages - SendMessage (Finding/Sending Windows Messages) - By Ace - by AceInfinity - 07-29-2011, 11:15 PM

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 629 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: 3 Guest(s)