Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Send String/Text to a other window [vb.net / API]

Send String/Text to a other window [vb.net / API]

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpjson
10 Posts 6 Posters 20 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Seek51
    wrote on last edited by
    #1

    Hello, I want to send a message/test to a other windows. It should save me many time in putting me many time strings/textes in MS Project, Outlook, ... ... Therefore i want to use the Windwos API in VB.net. I tried it in this way, but it doesn't work. I get the windows handle, but the text isn't set at notepad or in a other window.

    Private Sub tmr_Worktimer_Tick(sender As Object, e As EventArgs) Handles tmr_Worktimer.Tick
    If IsKeyPressed(Keys.VK_SHIFT) Then
    Dim aPoint As New POINTAPI
    GetCursorPos(aPoint)
    Dim hWnd As IntPtr = WindowFromPoint(aPoint.x, aPoint.y)
    SetForegroundWindow(hWnd)
    SetWindowTextUnicode(hWnd, WM_SETTEXT, IntPtr.Zero, "Test")
    End If
    End Sub

    My API Calls are:

    Public Structure Keys
    Const VK_BACK As Short = &H8 ' Backspace key
    Const VK_TAB As Short = &H9 ' Tab key
    Const VK_RETURN As Short = &HD ' Enter key
    Const VK_SHIFT As Short = &H10 ' Shift key
    Const VK_CONTROL As Short = &H11 ' STRG key
    Const VK_MENU As Short = &H12 ' Alt key
    Const VK_CAPITAL As Short = &H14 ' Caps Lock
    Const VK_ESCAPE As Short = &H1B ' Escape
    Const VK_SPACE As Short = &H20 ' Space
    Const VK_PRIOR As Short = &H21 ' PageUp
    Const VK_NEXT As Short = &H22 ' PageDown
    End Structure

    Public Function GetAsyncKeyState(ByVal vKey As Int32) As Short
    End Function
    

    Public Function IsKeyPressed(ByVal KeyToCheck As Short) As Boolean
    Dim res As Short
    res = GetAsyncKeyState(KeyToCheck)
    If res < 0 Then ' -32768 Then
    IsKeyPressed = True
    Else
    IsKeyPressed = False
    End If
    End Function

    Public Structure POINTAPI
        Dim x As Integer
        Dim y As Integer
    End Structure
    
        
    Public Function GetCursorPos(ByRef lpPoint As POINTAPI) As  Boolean
    End Function
    
        
    Public Function WindowFromPoint(xPoint As Integer, yPoint As Integer) As IntPtr
    End Function
    
        
    Public Function SetForegroundWindow(hWnd As IntPtr) As Boolean
    End Fun
    
    L J A 4 Replies Last reply
    0
    • S Seek51

      Hello, I want to send a message/test to a other windows. It should save me many time in putting me many time strings/textes in MS Project, Outlook, ... ... Therefore i want to use the Windwos API in VB.net. I tried it in this way, but it doesn't work. I get the windows handle, but the text isn't set at notepad or in a other window.

      Private Sub tmr_Worktimer_Tick(sender As Object, e As EventArgs) Handles tmr_Worktimer.Tick
      If IsKeyPressed(Keys.VK_SHIFT) Then
      Dim aPoint As New POINTAPI
      GetCursorPos(aPoint)
      Dim hWnd As IntPtr = WindowFromPoint(aPoint.x, aPoint.y)
      SetForegroundWindow(hWnd)
      SetWindowTextUnicode(hWnd, WM_SETTEXT, IntPtr.Zero, "Test")
      End If
      End Sub

      My API Calls are:

      Public Structure Keys
      Const VK_BACK As Short = &H8 ' Backspace key
      Const VK_TAB As Short = &H9 ' Tab key
      Const VK_RETURN As Short = &HD ' Enter key
      Const VK_SHIFT As Short = &H10 ' Shift key
      Const VK_CONTROL As Short = &H11 ' STRG key
      Const VK_MENU As Short = &H12 ' Alt key
      Const VK_CAPITAL As Short = &H14 ' Caps Lock
      Const VK_ESCAPE As Short = &H1B ' Escape
      Const VK_SPACE As Short = &H20 ' Space
      Const VK_PRIOR As Short = &H21 ' PageUp
      Const VK_NEXT As Short = &H22 ' PageDown
      End Structure

      Public Function GetAsyncKeyState(ByVal vKey As Int32) As Short
      End Function
      

      Public Function IsKeyPressed(ByVal KeyToCheck As Short) As Boolean
      Dim res As Short
      res = GetAsyncKeyState(KeyToCheck)
      If res < 0 Then ' -32768 Then
      IsKeyPressed = True
      Else
      IsKeyPressed = False
      End If
      End Function

      Public Structure POINTAPI
          Dim x As Integer
          Dim y As Integer
      End Structure
      
          
      Public Function GetCursorPos(ByRef lpPoint As POINTAPI) As  Boolean
      End Function
      
          
      Public Function WindowFromPoint(xPoint As Integer, yPoint As Integer) As IntPtr
      End Function
      
          
      Public Function SetForegroundWindow(hWnd As IntPtr) As Boolean
      End Fun
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You need to use the debugger to see which of those API calls is failing. As it stands your code has no idea whether anything works.

      S E 2 Replies Last reply
      0
      • L Lost User

        You need to use the debugger to see which of those API calls is failing. As it stands your code has no idea whether anything works.

        S Offline
        S Offline
        Seek51
        wrote on last edited by
        #3

        The problem is, it didn't fail. So i ask, if i use the right api call. Or is there a better way to copy a string to a other textbox on a foreight form/app, then the "SendMessageW" API call?

        L J R 3 Replies Last reply
        0
        • S Seek51

          The problem is, it didn't fail. So i ask, if i use the right api call. Or is there a better way to copy a string to a other textbox on a foreight form/app, then the "SendMessageW" API call?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Seek51 wrote:

          The problem is, it didn't fail.

          That is not what you said in your original question. So you need to be much more specific about the actual problem you have.

          1 Reply Last reply
          0
          • S Seek51

            The problem is, it didn't fail. So i ask, if i use the right api call. Or is there a better way to copy a string to a other textbox on a foreight form/app, then the "SendMessageW" API call?

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            Seek51 wrote:

            The problem is, it didn't fail.

            How exactly do you know that? Did you run it in the debugger? Otherwise I don't see an exception handler. And you are not looking at return values. For example SetForegroundWindow() which returns a boolean. And that value tells you whether it worked or not.

            1 Reply Last reply
            0
            • S Seek51

              Hello, I want to send a message/test to a other windows. It should save me many time in putting me many time strings/textes in MS Project, Outlook, ... ... Therefore i want to use the Windwos API in VB.net. I tried it in this way, but it doesn't work. I get the windows handle, but the text isn't set at notepad or in a other window.

              Private Sub tmr_Worktimer_Tick(sender As Object, e As EventArgs) Handles tmr_Worktimer.Tick
              If IsKeyPressed(Keys.VK_SHIFT) Then
              Dim aPoint As New POINTAPI
              GetCursorPos(aPoint)
              Dim hWnd As IntPtr = WindowFromPoint(aPoint.x, aPoint.y)
              SetForegroundWindow(hWnd)
              SetWindowTextUnicode(hWnd, WM_SETTEXT, IntPtr.Zero, "Test")
              End If
              End Sub

              My API Calls are:

              Public Structure Keys
              Const VK_BACK As Short = &H8 ' Backspace key
              Const VK_TAB As Short = &H9 ' Tab key
              Const VK_RETURN As Short = &HD ' Enter key
              Const VK_SHIFT As Short = &H10 ' Shift key
              Const VK_CONTROL As Short = &H11 ' STRG key
              Const VK_MENU As Short = &H12 ' Alt key
              Const VK_CAPITAL As Short = &H14 ' Caps Lock
              Const VK_ESCAPE As Short = &H1B ' Escape
              Const VK_SPACE As Short = &H20 ' Space
              Const VK_PRIOR As Short = &H21 ' PageUp
              Const VK_NEXT As Short = &H22 ' PageDown
              End Structure

              Public Function GetAsyncKeyState(ByVal vKey As Int32) As Short
              End Function
              

              Public Function IsKeyPressed(ByVal KeyToCheck As Short) As Boolean
              Dim res As Short
              res = GetAsyncKeyState(KeyToCheck)
              If res < 0 Then ' -32768 Then
              IsKeyPressed = True
              Else
              IsKeyPressed = False
              End If
              End Function

              Public Structure POINTAPI
                  Dim x As Integer
                  Dim y As Integer
              End Structure
              
                  
              Public Function GetCursorPos(ByRef lpPoint As POINTAPI) As  Boolean
              End Function
              
                  
              Public Function WindowFromPoint(xPoint As Integer, yPoint As Integer) As IntPtr
              End Function
              
                  
              Public Function SetForegroundWindow(hWnd As IntPtr) As Boolean
              End Fun
              
              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Myself that certainly does not look to me like that code would work. Best I can see you are perhaps sending text to the desktop? In other words the Windows UI is not the same as the processes that run. When I googled for this solutions the Microsoft code I found suggests exactly what I would expect the process to be. How to: start an Application and send it Keystrokes - Visual Basic | Microsoft Learn[^] I suppose it might be possible to find an app using the UI, but sending key presses to it will still require the proc id.

              1 Reply Last reply
              0
              • S Seek51

                Hello, I want to send a message/test to a other windows. It should save me many time in putting me many time strings/textes in MS Project, Outlook, ... ... Therefore i want to use the Windwos API in VB.net. I tried it in this way, but it doesn't work. I get the windows handle, but the text isn't set at notepad or in a other window.

                Private Sub tmr_Worktimer_Tick(sender As Object, e As EventArgs) Handles tmr_Worktimer.Tick
                If IsKeyPressed(Keys.VK_SHIFT) Then
                Dim aPoint As New POINTAPI
                GetCursorPos(aPoint)
                Dim hWnd As IntPtr = WindowFromPoint(aPoint.x, aPoint.y)
                SetForegroundWindow(hWnd)
                SetWindowTextUnicode(hWnd, WM_SETTEXT, IntPtr.Zero, "Test")
                End If
                End Sub

                My API Calls are:

                Public Structure Keys
                Const VK_BACK As Short = &H8 ' Backspace key
                Const VK_TAB As Short = &H9 ' Tab key
                Const VK_RETURN As Short = &HD ' Enter key
                Const VK_SHIFT As Short = &H10 ' Shift key
                Const VK_CONTROL As Short = &H11 ' STRG key
                Const VK_MENU As Short = &H12 ' Alt key
                Const VK_CAPITAL As Short = &H14 ' Caps Lock
                Const VK_ESCAPE As Short = &H1B ' Escape
                Const VK_SPACE As Short = &H20 ' Space
                Const VK_PRIOR As Short = &H21 ' PageUp
                Const VK_NEXT As Short = &H22 ' PageDown
                End Structure

                Public Function GetAsyncKeyState(ByVal vKey As Int32) As Short
                End Function
                

                Public Function IsKeyPressed(ByVal KeyToCheck As Short) As Boolean
                Dim res As Short
                res = GetAsyncKeyState(KeyToCheck)
                If res < 0 Then ' -32768 Then
                IsKeyPressed = True
                Else
                IsKeyPressed = False
                End If
                End Function

                Public Structure POINTAPI
                    Dim x As Integer
                    Dim y As Integer
                End Structure
                
                    
                Public Function GetCursorPos(ByRef lpPoint As POINTAPI) As  Boolean
                End Function
                
                    
                Public Function WindowFromPoint(xPoint As Integer, yPoint As Integer) As IntPtr
                End Function
                
                    
                Public Function SetForegroundWindow(hWnd As IntPtr) As Boolean
                End Fun
                
                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                All MS products support remote automation. "Sending keystrokes" is the equivalent of two cans and a string in this case. [Project Interface (Microsoft.Office.Interop.MSProject) | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.msproject.project?view=office-project-server) [UI Automation Overview - .NET Framework | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-overview)

                "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                1 Reply Last reply
                0
                • S Seek51

                  Hello, I want to send a message/test to a other windows. It should save me many time in putting me many time strings/textes in MS Project, Outlook, ... ... Therefore i want to use the Windwos API in VB.net. I tried it in this way, but it doesn't work. I get the windows handle, but the text isn't set at notepad or in a other window.

                  Private Sub tmr_Worktimer_Tick(sender As Object, e As EventArgs) Handles tmr_Worktimer.Tick
                  If IsKeyPressed(Keys.VK_SHIFT) Then
                  Dim aPoint As New POINTAPI
                  GetCursorPos(aPoint)
                  Dim hWnd As IntPtr = WindowFromPoint(aPoint.x, aPoint.y)
                  SetForegroundWindow(hWnd)
                  SetWindowTextUnicode(hWnd, WM_SETTEXT, IntPtr.Zero, "Test")
                  End If
                  End Sub

                  My API Calls are:

                  Public Structure Keys
                  Const VK_BACK As Short = &H8 ' Backspace key
                  Const VK_TAB As Short = &H9 ' Tab key
                  Const VK_RETURN As Short = &HD ' Enter key
                  Const VK_SHIFT As Short = &H10 ' Shift key
                  Const VK_CONTROL As Short = &H11 ' STRG key
                  Const VK_MENU As Short = &H12 ' Alt key
                  Const VK_CAPITAL As Short = &H14 ' Caps Lock
                  Const VK_ESCAPE As Short = &H1B ' Escape
                  Const VK_SPACE As Short = &H20 ' Space
                  Const VK_PRIOR As Short = &H21 ' PageUp
                  Const VK_NEXT As Short = &H22 ' PageDown
                  End Structure

                  Public Function GetAsyncKeyState(ByVal vKey As Int32) As Short
                  End Function
                  

                  Public Function IsKeyPressed(ByVal KeyToCheck As Short) As Boolean
                  Dim res As Short
                  res = GetAsyncKeyState(KeyToCheck)
                  If res < 0 Then ' -32768 Then
                  IsKeyPressed = True
                  Else
                  IsKeyPressed = False
                  End If
                  End Function

                  Public Structure POINTAPI
                      Dim x As Integer
                      Dim y As Integer
                  End Structure
                  
                      
                  Public Function GetCursorPos(ByRef lpPoint As POINTAPI) As  Boolean
                  End Function
                  
                      
                  Public Function WindowFromPoint(xPoint As Integer, yPoint As Integer) As IntPtr
                  End Function
                  
                      
                  Public Function SetForegroundWindow(hWnd As IntPtr) As Boolean
                  End Fun
                  
                  A Offline
                  A Offline
                  Andre Oosthuizen
                  wrote on last edited by
                  #8

                  As most of the comments suggest above, use a debugger in your code. You have a lot of else statements that will override a normal error, which is why you get no errors but also no warnings/comments in your log file...

                  1 Reply Last reply
                  0
                  • L Lost User

                    You need to use the debugger to see which of those API calls is failing. As it stands your code has no idea whether anything works.

                    E Offline
                    E Offline
                    Emma Stons
                    wrote on last edited by
                    #9

                    Sending text to another window using VB.NET and APIs requires careful coding. Utilizing functions like SendMessage or PostMessage can facilitate interprocess communication effectively.

                    [

                    Finance Fact

                    ](https://financefact.net/)

                    1 Reply Last reply
                    0
                    • S Seek51

                      The problem is, it didn't fail. So i ask, if i use the right api call. Or is there a better way to copy a string to a other textbox on a foreight form/app, then the "SendMessageW" API call?

                      R Offline
                      R Offline
                      Richard Goodwin 2024
                      wrote on last edited by
                      #10

                      When troubleshooting, confirming the correct API call is crucial; if "Go to Parent" didn't fail, exploring alternative methods or refining the existing one might enhance the string-copying process between textboxes in different forms or applications. It's a dynamic journey of finding the most efficient approach in programming 🙂

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups