Results 1 to 14 of 14

Thread: [RESOLVED] Stuck with API, SendMessage for TEdit control

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    28

    Resolved [RESOLVED] Stuck with API, SendMessage for TEdit control

    Hi everyone,
    I have troubles, I'm stuck, trying to send some text string to one TextBox of other application.
    This TextBox have TEdit class, and I dont reach to find the solution to fill this text box from my app.
    Code identify without problem the TEdit TextBox handle based on SP++ identification, but I don't succeed to sent a message using WM_SETTEXT


    Name:  TEdit.JPG
Views: 242
Size:  21.9 KB

    This is my code

    Imports System.Runtime.InteropServices
    Imports System.Text

    Public Class Form2

    Dim arr1 As New List(Of ApiWindow)
    Const WM_SETTEXT As Integer = &HC
    Const GW_CHILD As Integer = 5



    Private Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Private Declare Auto Function FindWindowEx Lib "user32.dll" (ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr

    <DllImport("User32.dll")>
    Private Shared Function EnumChildWindows(ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, ByVal lParam As IntPtr) As Boolean
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto)>
    Private Shared Function GetClassName(ByVal hWnd As System.IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer
    End Function

    Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean

    Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As IntPtr

    Public Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
    Dim ChildrenList As New List(Of IntPtr)
    Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
    Try
    EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
    Finally
    If ListHandle.IsAllocated Then ListHandle.Free()
    End Try
    Return ChildrenList.ToArray
    End Function
    Public Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
    Dim ChildrenList As List(Of IntPtr) = GCHandle.FromIntPtr(Parameter).Target
    If ChildrenList Is Nothing Then Throw New Exception("GCHandle Target could not be cast as List(Of IntPtr)")
    ChildrenList.Add(Handle)
    Return True
    End Function

    Public Structure ApiWindow
    Public hndl As IntPtr
    Public MainWindowTitle As String
    Public ClassName As String
    Public Style As String
    Public hWnd As Int32
    Public Left As Integer
    Public Top As Integer
    Public Right As Integer
    Public Bottom As Integer
    End Structure
    Structure RECT
    Public Left As Integer
    Public Top As Integer
    Public Right As Integer
    Public Bottom As Integer
    End Structure
    Public Function GetWindowIdentification(ByVal hwnd As Integer, ByVal myhndl As IntPtr) As ApiWindow

    Const WM_GETTEXT As Int32 = &HD
    Const WM_GETTEXTLENGTH As Int32 = &HE

    Dim sClassName As New StringBuilder("", 256)
    Dim myRect As RECT
    Dim window As ApiWindow

    Dim title As New StringBuilder()
    Dim SearchString As String = "TEdit"

    Dim size As Int32 = SendMessage(myhndl, WM_GETTEXTLENGTH, 0, 0)

    GetClassName(myhndl, sClassName, 256)

    title = New StringBuilder(size + 1)
    SendMessage(hwnd, WM_GETTEXT, title.Capacity, title.ToString)


    If sClassName.ToString().IndexOf(SearchString) <> -1 Then



    window.hndl = hwnd
    window.MainWindowTitle = title.ToString()
    window.ClassName = sClassName.ToString()
    window.hWnd = hwnd
    window.Top = myRect.Top
    window.Left = myRect.Left

    End If
    Return window

    End Function
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    arr1.Clear()
    Dim hWnd1 As IntPtr
    hWnd1 = FindWindow("ParentClass", "FormName")

    Dim hndls() As IntPtr = GetChildWindows(hWnd1)

    Dim window As ApiWindow

    Dim SearchString As String = "TEdit"

    For Each hnd In hndls
    window = GetWindowIdentification(hnd, hnd)
    If Not window.ClassName Is Nothing Then
    If window.ClassName.IndexOf(SearchString) <> -1 Then
    arr1.Add(window)
    End If
    End If
    Next
    SendMessage(arr1(0).hndl, WM_SETTEXT, 0, "Hello World")
    End Sub
    End Class

    Thanks in advance for any suggestion

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,659

    Re: Stuck with API, SendMessage for TEdit control

    These things tend to be really tricky.
    You should ask this to be moved to the API thread IMO.
    So initial notes.

    Use this:

    Code:
    <DllImport("user32.dll", CharSet:=CharSet.Auto)>
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInt32, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
    
    Const WM_SETTEXT As Uint32 = &HC
    
    '' send the message
    SendMessage(arr1(0).hndl, WM_SETTEXT, intptr.zero, "Hello World")
    Also try first with straight hndls, dim hndls() As IntPtr = GetChildWindows(hWnd1) just to be sure

    Also I know for a fact the FindWindowEx tend to be problematic so I suppose that would stop sendmessage to also be problematic.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    28

    Re: Stuck with API, SendMessage for TEdit control

    Hi, Thanks Sapator,
    Unfortunately is not working,
    Is strange for me, I cannot find the difference, same piece of code it's working on win7, now I upgraded system to WIN10 and is not working anymore

    Any suggestion, help is helpful
    Thanks in advance

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,659

    Re: Stuck with API, SendMessage for TEdit control

    Yes that w10 raises questions, as I've said Also I know for a fact the FindWindowEx tend to be problematic. Maybe there is some other sendmessage call that works better with w10 (except if I missed something in the code, that's why I suggested to be moved to API as the knowledge is more focused on API).
    Are you able to get the intptr to the actually textbox? Are you sure you have the correct handle? Also are you running this with administrative permissions? After w7 MS went berserk with the permissions needed.

    I'm also reading this one: https://codingvision.net/c-send-text-to-notepad
    It's notepad but what I see is the call has integers not unsighned int32. So maybe it's worth a try.

    Also this: Const WM_SETTEXT As Integer = 0X000C basically Const WM_SETTEXT As Integer = &H000C
    Last edited by sapator; Jul 9th, 2024 at 07:38 AM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    28

    Re: Stuck with API, SendMessage for TEdit control

    HI,
    \
    this is what Spy++ capture when I copy-paste string "123456789" to app textbox (TEdit class)

    Attachment 192211

    Thanks in advance for any hint, at the moment I'm not having any success
    Attached Images Attached Images  

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,659

    Re: Stuck with API, SendMessage for TEdit control

    Have you tried anything from the above? Are you sure you get the correct handle?
    Also if everything fails maybe you should try the SendKeys API function.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    28

    Re: Stuck with API, SendMessage for TEdit control

    Yes, I tried, also I tried with send keys, no success
    SetForegroundWindow(AppHwnd)
    SetFocus(te_hdl)

    My.Computer.Keyboard.SendKeys("I ", True)
    My.Computer.Keyboard.SendKeys("M", True)

  8. #8
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,659

    Re: Stuck with API, SendMessage for TEdit control

    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    28

    Re: Stuck with API, SendMessage for TEdit control

    Yes Saptor,

    I tried in different form, SentWait, Send.... no success
    SendKeys.SendWait("123456789")
    SendKeys.SendWait("{Enter}")
    'SendKeys.Send("aaa")
    ' SendKeys.Send("{Enter}")

  10. #10
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,659

    Re: Stuck with API, SendMessage for TEdit control

    This (fix it on your project of course) ?

    Code:
    Dim HWND As IntPtr
    Dim windowTitle As String
    HWND = FindWindow(Nothing, windowTitle )
    If HWND.ToString() <> "0" Then
        SetForegroundWindow(HWND)
    
    SendKeys.......
    End If
    Also try with postmessage:

    Code:
    
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
    End Function
    Code:
    Private Const WM_KEYDOWN As uint32 = 100
    Private Const WM_RBUTTONDOWN As uint32 = 204
    
    ''etc...
    
    PostMessage(HWND_MESSAGE, WM_RBUTTONDOWN, 0, "TEXT TO SEND")
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    28

    Re: Stuck with API, SendMessage for TEdit control

    Yes Sapator,
    Already I tried also both approaches without success.
    If I try to send in one TextBox control created in Visual Studio, it's working, something is different with this TEdit class

  12. #12
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,659

    Re: Stuck with API, SendMessage for TEdit control

    Well TEdit is different. What can I help with is you have a look at the last piece of code I posted here that worked and maybe you can get the correct handle.
    https://www.vbforums.com/showthread....istview-object
    As you probably can see this is for a TListview but the idea is to get the correct handle. Try to bring it to your measures, after that I don't think I have any other solution.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    28

    Re: Stuck with API, SendMessage for TEdit control

    Finally I found the issue,
    I succeed to communicate with handler, using classic SendMessage format
    SendMessage(txthnd.hndl, WM_SETTEXT, 0&, SendBuilder)

    Problem was from win10 security mode, if the app is running like "run as administrator", everything works perfectly
    I created a manifest in app projects, adding
    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
    and everythings work perfectly

    Thanks again Sapator for your kindness and time spent for my issue

  14. #14
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,659

    Re: Stuck with API, SendMessage for TEdit control

    No problem.
    Of course these would all be avoided if in a certain posts you tried to.....
    Quote Originally Posted by sapator View Post
    Yes that w10 raises questions, as I've said Also I know for a fact the FindWindowEx tend to be problematic. Maybe there is some other sendmessage call that works better with w10 (except if I missed something in the code, that's why I suggested to be moved to API as the knowledge is more focused on API).
    Are you able to get the intptr to the actually textbox? Are you sure you have the correct handle? Also are you running this with administrative permissions? After w7 MS went berserk with the permissions needed.

    I'm also reading this one: https://codingvision.net/c-send-text-to-notepad
    It's notepad but what I see is the call has integers not unsighned int32. So maybe it's worth a try.

    Also this: Const WM_SETTEXT As Integer = 0X000C basically Const WM_SETTEXT As Integer = &H000C
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width