[RESOLVED] Sending Text To Telnet Without Using Sendkeys
I am trying to send text to a telnet window without using sendkeys.send().
I am new to API but not new to programming and I am using C#.
I figured out how to get the hWnd of the telnet session but I cannot figure out how to send text to the window. Heres what I have so far.
SendMessage(hWndTnet, WM_SETTEXT, 0, "testing");
SendMessage(hWndTnet, WM_CHAR, 13, null);
What am I doing wrong?
Thanks a bunch for any help :thumb:
Re: Sending Text To Telnet Without Using Sendkeys
What type of telnet window is it?
And have you tried WM_KEYDOWN or WM_KEYUP?
Re: Sending Text To Telnet Without Using Sendkeys
thanks for the reply
its the windows xp telnet from C:\WINDOWS\system32\telnet.exe
im really new to api.
how do i find the const values for WM_KEYDOWN and WM_KEYUP?
heres what I have so far
public static int GW_HWNDNEXT = 2;
public static int GW_CHILD = 5;
public static int GW_OWNER = 4;
public static int GWL_HWNDPARENT = -8;
public static int WM_SETTEXT = 12;
public const int HWND_MESSAGE = -3;
public const int WM_CHAR = 258;
Re: Sending Text To Telnet Without Using Sendkeys
I also tried to redirect the standard I/O but telnet will not allow it.
I basically need to be able to send text to a telnet.exe process without using appactivate/sendkeys because I will have 30+ telnet processes open at one time. I am trying to automate a tedious process here at my job.
Re: Sending Text To Telnet Without Using Sendkeys
The quickest way is just to do a Google search "#define " followed by the name of the constant. For example, "#define WM_KEYDOWN". And then grab the most common hex value you see. It's pretty easy, in this case you see the text #define WM_KEYDOWN 0x100 all down the page, so that's the value :)
Code:
const int WM_KEYDOWN = 0x100;
Re: Sending Text To Telnet Without Using Sendkeys
i just tried
public const int WM_KEYDOWN = 0x100;
SendMessage(hWndTnet, WM_KEYDOWN, 0, "testing");
but it did not work...
Re: Sending Text To Telnet Without Using Sendkeys
With KEYDOWN you can only do a character at a time.
http://msdn.microsoft.com/library/de...wm_keydown.asp
Quote:
wParam
Specifies the virtual-key code of the nonsystem key.
so
Code:
SendMessage(hWndTnet, WM_KEYDOWN, Keys.T, 0);
would send a T character, hopefully :)
Re: Sending Text To Telnet Without Using Sendkeys
Thanks for the help.
SendMessage(hWndTnet, WM_KEYDOWN, 0,Keys.T);
I tried and got this error message.
Error 1 The best overloaded method match for 'Send_Window_Test.Form1.SendMessage(int, int, int, string)' has some invalid arguments Form1.cs 70 13 Send Window Test
Error 2 Argument '4': cannot convert from 'System.Windows.Forms.Keys' to 'string' Form1.cs 70 53 Send Window Test
so then i tried using the .ToString() but that did not work either.
SendMessage(hWndTnet, WM_KEYDOWN, 0,Keys.T.ToString());
I also tried this but it did not work either.
SendMessage(hWndTnet, WM_KEYDOWN, Keys.T, null);
and
SendMessage(hWndTnet, WM_KEYDOWN, Convert.ToInt32(Keys.T), null);
Re: Sending Text To Telnet Without Using Sendkeys
oh, you need to change your SendMessage declaration so all the parameters are int's ;)
1 Attachment(s)
Re: Sending Text To Telnet Without Using Sendkeys
Thanks again for helping.
Is there any way I can get you to take a look at the code cause it still doesnt work after makeing all parms type int
Thanks alot.
Re: Sending Text To Telnet Without Using Sendkeys
the telnet window is nothing more than a comand console window so what works is posting WM_CHAR messages. Here is code for VB6, you'll have to make changes for VB.NET
VB Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long _
) As Long
Private Const WM_CHAR = &H102
Private Sub Command1_Click()
Dim hWndCMD As Long
Dim x As String
Dim i As Integer
'Change to your caption below
hWndCMD = FindWindow("ConsoleWindowClass", "C:\WINDOWS\system32\cmd.exe")
If hWndCMD = 0 Then Stop
For i = 1 To Len(Text1.Text)
x = Mid(Text1.Text, i, 1)
PostMessage hWndCMD, WM_CHAR, Asc(x), 0&
Next i
PostMessage hWndCMD, WM_CHAR, Asc(vbCr), 0&
PostMessage hWndCMD, WM_CHAR, Asc(vbLf), 0&
End Sub
Re: Sending Text To Telnet Without Using Sendkeys
See I didn't know what I was doing :)
Appropriately converted!
Code:
[DllImport("user32", EntryPoint = "FindWindowA")]
private static extern IntPtr FindWindow(
[MarshalAs(UnmanagedType.LPStr)] string lpClassName,
[MarshalAs(UnmanagedType.LPStr)] string lpWindowName
);
[DllImport("user32", EntryPoint = "PostMessageA")]
private static extern int PostMessage(
IntPtr hWnd,
int uMsg,
int wParam,
int lParam
);
private const int WM_CHAR = 0x102;
private void Command1_Click()
{
// you will need to change this line to find the telnet window
IntPtr hWndCMD = FindWindow("ConsoleWindowClass", String.Empty);
byte[] chars = (new System.Text.ASCIIEncoding()).GetBytes(Text1.Text);
for (int i = 0; i < chars.Length; i++)
{
PostMessage(hWndCMD, WM_CHAR, (int)chars[i], 0);
}
PostMessage(hWndCMD, WM_CHAR, 10, 0);
PostMessage(hWndCMD, WM_CHAR, 13, 0);
}
Re: Sending Text To Telnet Without Using Sendkeys
I gotta git me some of that .NET stuff. :)
Re: Sending Text To Telnet Without Using Sendkeys
Wow your good. It does work with a ConsoleWindowClass but not with a TelnetClient?
This code works
IntPtr hWndCMD = FindWindow("ConsoleWindowClass", @"C:\WINDOWS\system32\cmd.exe");
but the one for telnet doesnt?
IntPtr hWndCMD = FindWindow("TelnetClient", @"C:\WINDOWS\system32\telnet.exe");
Is there any way to get it to work directly with the telnet window?
Thanks for all the help.
Re: Sending Text To Telnet Without Using Sendkeys
It works for me in VB6
VB Code:
hWndCMD = FindWindow("ConsoleWindowClass", "C:\WINDOWS\system32\telnet.exe")
Re: Sending Text To Telnet Without Using Sendkeys
I was telling it to look for the "TelnetClient" because thats how the program is listed under spy++. I have alot to learn about API.
When you use ConsoleWindowClass it works.
Thanks everyone for getting this issue resolved.
Re: [RESOLVED] Sending Text To Telnet Without Using Sendkeys
No problems,
The ConsoleWindowClass is probably within the TelnetClient window.