Hey all, i am trying to send some text to a console box (dos box) from my vb program but i can not seem to get it working.
Here is my current code:
Dim blah As Long
Private Const WM_GETTEXT As Integer = &HD
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
blah = FindWindow1(vbNullString, "Form1")
blah = FindWindowEx(blah, vbNullString, "ConsoleWindowClass", vbNullString)
Debug.Print(blah)
SendMessage(blah, WM_SETTEXT, 200, "A")
Though that does work, it only puts a A for the title bar and not within the console.
Any help would be great! :o)
David
I've not tried it but I think you might want to look at AttachConsole to attach your process to the console of the command line process. Then you should be able to use the Console.WriteLine and similar methods I'd assume.
You can find a sample (in C#, but should be easy to convert to VB using one of the online converters) on the PInvoke page.
Related
From VB .Net, I'm trying to send a string to a textbox of another application but I cannot make it work. I'm able to get the handle and even set the focus to the textbox but my SendMessage function doesn't seem to be correct as I get the error message "SendMessage' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."
Here is my code:
Module Module1
Private Const WM_SETTEXT As Int32 = &HC
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Long
Private Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Auto Function FindWindowEx Lib "user32" (ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
Private Declare Auto Function SendMessage Lib "user32" (ByVal hwnd As IntPtr, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As IntPtr
Sub Main()
Dim AppHwnd As IntPtr = FindWindow(vbNullString, "Test Application"
Dim WinHwnd1 As IntPtr = FindWindowEx(AppHwnd, 0&, "SWT_Window0", vbNullString)
Dim WinHwnd2 As IntPtr = FindWindowEx(WinHwnd1, 0&, "SWT_Window0", vbNullString)
Dim WinHwnd3 As IntPtr = FindWindowEx(WinHwnd2, 0&, "SWT_Window0", vbNullString)
Dim TextBoxHwnd1 As IntPtr = FindWindowEx(WinHwnd3, 0&, "Edit", vbNullString)
Dim TextBoxHwnd2 As IntPtr = FindWindowEx(WinHwnd3, TextBoxHwnd1, "Edit", vbNullString)
MsgBox(TextBoxHwnd2)
SetForegroundWindow(TextBoxHwnd2)
SendMessage(TextBoxHwnd2, WM_SETTEXT, 0&, "text")
End Sub
End Module
The line "MsgBox(TextBoxHwnd2)" returns the handle number I found using Window Detective so I'm assuming the code is correct up to this point. Also, I tested "SetForegroundWindow(TextBoxHwnd1)" and the cursor is on the first textbox whereas "SetForegroundWindow(TextBoxHwnd1) sets the cursor on the second textbox.
Your declaration looks like it might have been migrated from legacy VB code, where Long was a 32-bit integer and Integer was only 16-bit (dating to legacy VB's origination in 16-bit Windows). The length of the data types changed in .NET which was created after the shift to universal 32-bit Windows, so Integer is 32-bit and Long is 64-bit.
Moreover, the Windows API itself needed to be updated for 64-bit Windows. SendMessage is one of the functions that has some parameters that are longer in 64-bit because they are expected to hold pointers.
I checked on the native header file declaration of SendMessage and found that:
The parameter Msg is declared as UINT which would correspond to Integer in VB. This needs to be changed regardless of whether you have a 32-bit or 64-bit build.
The parameter wParam is declared as UINT_PTR so it should in fact be Long for a 64-bit build, but Integer for a 32-bit build; if it's possible to declare it as IntPtr and then pass 0 to it, that would probably be best, because it should automatically adjust the length depending on whether you're building for 32-bit or 64-bit.
i was programming an hack for a game in Visual basic, i want simulate a keypress with the code:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
SendKeys.Send("a")
End Sub
In my window game didn't work, but in other windows such as chrome or a test textbox work, why?
if this is not a flash game , you can get the process of the game by:
Dim TheGame as Process = Process.GetProcessesByName("YourGame")(0)
get the handle of the game window:
Dim Handle As IntPtr = TheGame.MainWindowHandle
and then use sendmessage function to send the keys to the window:
SendMessage(Handle, 258, PUT KEY HERE, 0)
for example - instead of the "PUT KEY HERE" type:
&H41 (is "A")
you can find the keys list here:
http://www.pinvoke.net/default.aspx/Enums/VirtualKeys.html
the user32.dll sendmessage function:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
Edit:
if the code is not working change the 258 (wm_char) in the sendmessage to 256(wm_keydown)
note:sendkeys sends keys to the current active window, and you should try to use pinvoke functions first and if you don't have any choice then use sendkeys.
Recommendation:
explore the functions: postmessage,sendmessage,setforegroundwindow and also explore spy++ tool :).
I have tried automating a few external window's Combo box controls using the API functions, SendMessage and PostMessage and using the Window Messages WM_SETTEXT WM_GETTEXTLENGTH and WM_GETTEXT.
I can get this to work when I run through the code for example pressing F11 through each line or putting a breakpoint in and running it at a certain point, but when I run it as normal, the WM_SETTEXT message doesn't update the Combo box.
It seems to be an issue with the speed that the code is running so my first solution was to put a piece of validation that ensures the text is in the combo box, but for some reason the WM_GETTEXT retrieves the text that should be in the text box even though its not visible.
The code below is a snippet of the kind of validation I've tried, my question here would be is there any sort of wait until method I could use to ensure that the text actually gets entered, I know about the Sleep API but I would ideally like something that actually makes sure that text is there and doesn't load f x seconds and hopes that the text is there.
Any ideas are appriciated.
Many Thanks
VB.net Code Example:
Private Enum WindowMessages
WM_SETTEXT = &HC
WM_GETTEXT = &HD
WM_GETTEXTLENGTH = &HE
End Enum
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Int32, ByVal wMsg As Int32, ByVal _
wParam As Int32, lParam As String) As Int32
Public Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hWnd
As Int32, _
ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Int32
Private Sub TestExample(ByVal hWndTextBox as Int32, ByVal TextString as String)
'####Using the hWnd of a Combo box control and a string to enter in the combo box.####
Dim TextLength as Int32
Dim ItemText as string
Do until ItemText = TextString
SendMessage(hWndTextBox, WM_SETTEXT, 0, TextString) > 0
TextLength = SendMessage(hWndTextBox, WM_GETTEXTLENGTH, 0, 0)
ItemText = Space(TextLength)
SendMessage(hWndTextBox, WM_GETTEXT, TextLen + 1, ItemText)
Loop
End Sub
I'm trying to make a program with vb.net (VS2008) for studio shooting with my Nikon D600.
I'm using a program called ControlMyNikon for tethered shooting and it's working perfectly.
It has this external control-feature with following instructions: http://i.jjj.fi/a9dAQ7z.png
Could someone give me hint what does 'send string to 'ControlMyNikon v4.1' window with windows messages' actually mean?
I tried with SendMessages.
I was able to change window title with WM_SETTEXT but that's all.
I'm able to get the window handle but don't know how to send any string to it.
Help? :)
Ugh, Nikon is pretty infamous for writing truly crappy shovelware. This does not disappoint. Here are some declarations that ought to work. Try the Unicode version first. If that produces Chinese text then use the Ansi version:
Imports System.Runtime.InteropServices
Class NativeMethods
Friend Const WM_SETTEXT As Integer = 12
<DllImport("user32.dll", EntryPoint:="SendMessageW", CharSet:=CharSet.Unicode)> _
Friend Shared Function SetWindowTextUnicode(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="SendMessageA", CharSet:=CharSet.Ansi)> _
Friend Shared Function SetWindowTextAnsi(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
End Function
End Class
Usage:
NativeMethods.SetWindowTextUnicode(handleYouFound, WM_SETTTEXT, IntPtr.Zero, "shoot")
If neither works then you are probably using the wrong window handle. Use Spy++ to double check that you located the window properly.
im a complet noob in vb . i have been trying to figure out how to send keystrokes back to my own application , while minimized/ Or focused on an other window.
i think i need to use PostMessageA . i read about it on forums. But its like chinese for me.
my goal is to run these little programs by the 100's on 1 pc. and they just press a key in their own application , over and over.
can someone help me out please.
thanks
i was thinking something like this
Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CHAR As Long = &H102
PostMessage("notepad", WM_CHAR, "T", 1)
as a test. what am i doing wrong
A few things, for one you have found your code in a VB6 Forum. The size of integers has changed since then. A Long in VB6 is equivalant to an Integer in VB.Net(see this Msdn link). The second issue is that you are supplying a String to PostMessage where it is expecting the handle to your window. I would suggest that you look at this CodeProject article about how to Send strings to another application by using Windows messages
Your PostMessage declaration should look like this.
Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer