Re: VB, sending messages to another app using SendMessage / PostMessage.




Read my comment sent to Robert how to send data to second process!


<DarthContinent@xxxxxxxxx> wrote in message news:1125982172.882901.168660@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
| Hello, could someone more fluent than I in VB please examine the
| following and help me understand where the problem may lie?
|
| Basically I begin by grabbing the handle to an "edit" control within
| the main program (a game, Jedi Academy), and try to input text into
| that control using SendMessage.
|
| However, no matter what I've tried so far I can't seem to get any
| reaction from the game, I get no feedback whatsoever indicating that
| the messages I'm expecting have been successfully received. I get a
| nonzero value in response to FindWindow / FindWindowEx, which I
| understand indicates that the handle was acquired successfully, so I'm
| pretty sure I'm grabbing the right handle, it's just not going from
| there.
|
| Could it be an issue with focus? Does the app I'm grabbing a handle to
| need to have the focus before it will accept and process messages in
| this way?
|
| Or are my function definitions somehow incorrect / incomplete?
|
| Any suggestions would be appreciated, I've scoured the newsgroups and
| I'm afraid I haven't been able to gather much to help me out of my own
| hole.
|
| ---
|
| <BEGIN CODE>
|
|
| Imports System.Runtime.InteropServices
| Public Class Form1
| Inherits System.Windows.Forms.Form
|
| #Region " Windows Form Designer generated code "
|
| Public Sub New()
| MyBase.New()
|
| 'This call is required by the Windows Form Designer.
| InitializeComponent()
|
| 'Add any initialization after the InitializeComponent() call
|
| End Sub
|
| 'Form overrides dispose to clean up the component list.
| Protected Overloads Overrides Sub Dispose(ByVal disposing As
| Boolean)
| If disposing Then
| If Not (components Is Nothing) Then
| components.Dispose()
| End If
| End If
| MyBase.Dispose(disposing)
| End Sub
|
| 'Required by the Windows Form Designer
| Private components As System.ComponentModel.IContainer
|
| 'NOTE: The following procedure is required by the Windows Form
| Designer
| 'It can be modified using the Windows Form Designer.
| 'Do not modify it using the code editor.
| Friend WithEvents Button1 As System.Windows.Forms.Button
| Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
| Friend WithEvents Label2 As System.Windows.Forms.Label
| Friend WithEvents Label3 As System.Windows.Forms.Label
| <System.Diagnostics.DebuggerStepThrough()> Private Sub
| InitializeComponent()
| Me.Button1 = New System.Windows.Forms.Button
| Me.TextBox1 = New System.Windows.Forms.TextBox
| Me.Label2 = New System.Windows.Forms.Label
| Me.Label3 = New System.Windows.Forms.Label
| Me.SuspendLayout()
| '
| 'Button1
| '
| Me.Button1.Location = New System.Drawing.Point(16, 48)
| Me.Button1.Name = "Button1"
| Me.Button1.Size = New System.Drawing.Size(264, 32)
| Me.Button1.TabIndex = 0
| Me.Button1.Text = "send string in textbox to other app"
| '
| 'TextBox1
| '
| Me.TextBox1.Location = New System.Drawing.Point(16, 16)
| Me.TextBox1.Name = "TextBox1"
| Me.TextBox1.Size = New System.Drawing.Size(264, 20)
| Me.TextBox1.TabIndex = 1
| Me.TextBox1.Text = "some sample text"
| '
| 'Label2
| '
| Me.Label2.Location = New System.Drawing.Point(16, 96)
| Me.Label2.Name = "Label2"
| Me.Label2.Size = New System.Drawing.Size(256, 24)
| Me.Label2.TabIndex = 3
| Me.Label2.Text = "Label2"
| '
| 'Label3
| '
| Me.Label3.Location = New System.Drawing.Point(16, 128)
| Me.Label3.Name = "Label3"
| Me.Label3.Size = New System.Drawing.Size(256, 24)
| Me.Label3.TabIndex = 4
| Me.Label3.Text = "Label3"
| '
| 'Form1
| '
| Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
| Me.ClientSize = New System.Drawing.Size(292, 165)
| Me.Controls.Add(Me.Label3)
| Me.Controls.Add(Me.Label2)
| Me.Controls.Add(Me.TextBox1)
| Me.Controls.Add(Me.Button1)
| Me.Name = "Form1"
| Me.Text = "WM_send"
| Me.ResumeLayout(False)
|
| End Sub
|
| #End Region
|
|
| Dim wndh As Integer 'handle to some app window
| Dim wndh2 As Integer
|
|
| 'Win32 api declarations
| Private Structure COPYDATASTRUCT
| Public dwData As Integer
| Public cbData As Integer
| Public lpData As Integer
| End Structure
|
| Const WM_COPYDATA As Int32 = &H4A
| Const EM_GETSEL As Int32 = &HB0
| Const EM_SETSEL As Int32 = &HB1
| Const EM_REPLACESEL As Int32 = &HC2
| Const WM_CHAR As Int32 = &H102
| Const WM_SETTEXT As Int32 = &HC
| Const WM_GETTEXT As Int32 = &HD
| Const WM_GETTEXTLENGTH As Int32 = &HE
| Const WM_CLOSE As Int32 = &H10
|
|
| Declare Function GetFocus Lib "user32" () As Long
| Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
|
| Declare Function GetDlgCtrlID Lib "user32" (ByVal hwnd As Int32) As
| Int32
|
| Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA"
| (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
|
| Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA"
| (ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String,
| ByVal lpsz2 As String) As Int32
|
| Declare Function PostMessageByString Lib "user32" Alias
| "PostMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam
| As Int32, ByVal lParam As String) As Int32
|
| Declare Function SendMessage Lib "user32" Alias "SendMessageA"
| (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal
| ByVallParam As Int32) As Int32
|
| Declare Function SendMessageLong& Lib "user32" Alias "SendMessageA"
| (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal
| lParam As Int32)
|
| Declare Function SendMessageBystring Lib "user32" Alias
| "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam
| As Int32, ByVal lParam As String) As Int32
|
| Declare Function SetWindowText Lib "User32.dll" Alias
| "SetWindowTextA" (ByVal hWnd As Int32, ByVal lpString As String) As
| Int32
|
|
|
| 'Button to send message
| Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles Button1.Click
|
| 'Get handle to app (wndh) and control within app (wndh2)
|
| wndh = FindWindow("JAMP WinConsole", "Jedi Knight Academy MP
| Console")
| wndh2 = FindWindowEx(wndh, 0, "Edit", vbNullString)
| wndh2 = FindWindowEx(wndh, wndh2, "Edit", vbNullString)
|
| SetFocus(wndh2)
|
| SendMessage(wndh2, EM_SETSEL, -1, -1)
| SendMessageBystring(wndh2, EM_REPLACESEL, 0, "say test")
| SendMessageLong(wndh2, WM_CHAR, 13, 0&)
|
| End Sub
|
|
| <END CODE>
|
|
| Processed by 'SolarAngel-yVBProxy Ver:1.03.0100' contact yvbproxy@xxxxxx for more information!


.