Results 1 to 17 of 17

Thread: [RESOLVED] Sending Text To Telnet Without Using Sendkeys

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Resolved [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

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Sending Text To Telnet Without Using Sendkeys

    What type of telnet window is it?

    And have you tried WM_KEYDOWN or WM_KEYUP?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    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;

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    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.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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;

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    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...

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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

    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    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);
    Last edited by 2MuchRiceMakesMeSick; Jan 10th, 2006 at 05:32 AM.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Sending Text To Telnet Without Using Sendkeys

    oh, you need to change your SendMessage declaration so all the parameters are int's

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    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.
    Attached Files Attached Files

  11. #11
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    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:
    1. Option Explicit
    2.  
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    4.     ByVal lpClassName As String, _
    5.     ByVal lpWindowName As String _
    6. ) As Long
    7.  
    8. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
    9.     ByVal hwnd As Long, _
    10.     ByVal wMsg As Long, _
    11.     ByVal wParam As Long, _
    12.     ByVal lParam As Long _
    13. ) As Long
    14.  
    15. Private Const WM_CHAR = &H102
    16.  
    17. Private Sub Command1_Click()
    18. Dim hWndCMD As Long
    19. Dim x As String
    20. Dim i As Integer
    21.    'Change to your caption below
    22.     hWndCMD = FindWindow("ConsoleWindowClass", "C:\WINDOWS\system32\cmd.exe")
    23.     If hWndCMD = 0 Then Stop
    24.  
    25.     For i = 1 To Len(Text1.Text)
    26.         x = Mid(Text1.Text, i, 1)
    27.         PostMessage hWndCMD, WM_CHAR, Asc(x), 0&
    28.     Next i
    29.         PostMessage hWndCMD, WM_CHAR, Asc(vbCr), 0&
    30.         PostMessage hWndCMD, WM_CHAR, Asc(vbLf), 0&
    31.    
    32. End Sub

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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);
    }

  13. #13
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Sending Text To Telnet Without Using Sendkeys

    I gotta git me some of that .NET stuff.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    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.

  15. #15
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Sending Text To Telnet Without Using Sendkeys

    It works for me in VB6
    VB Code:
    1. hWndCMD = FindWindow("ConsoleWindowClass", "C:\WINDOWS\system32\telnet.exe")

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    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.

  17. #17
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] Sending Text To Telnet Without Using Sendkeys

    No problems,

    The ConsoleWindowClass is probably within the TelnetClient window.

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