Send String/Text to a other window [vb.net / API]
-
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 SubMy 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 StructurePublic 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 FunctionPublic 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
-
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 SubMy 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 StructurePublic 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 FunctionPublic 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
-
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.
-
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?
-
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?
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.
-
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 SubMy 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 StructurePublic 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 FunctionPublic 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
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.
-
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 SubMy 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 StructurePublic 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 FunctionPublic 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
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
-
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 SubMy 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 StructurePublic 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 FunctionPublic 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
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...
-
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.
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
-
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?
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 🙂