?
Solved

SendKeys not working

Posted on 2009-07-01
5
Medium Priority
?
1,890 Views
Last Modified: 2012-05-07
Hi all,
I am using the following script to reboot my DSL router. It is working perfectly fine when a user account is signed in. But when the computer is locked(Windows + L key) the script fails. I have investigated and found that SendKeys will not work when the computer is locked. So what are my options? What should I use instead of "Sendkeys"?
Note: This script is run from a VB.NET application whenever internet stays disconnected for more than 2 minutes....
Can the script be eliminated and done form inside the VB.NET code?
OS: Windows XP SP3
VB.NET 2008
On Error Resume Next
strComputer = "."
 
Dim  WshShell, Windir, i, objWMIService,SeverName
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set WshShell = CreateObject("WScript.Shell")
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")
ServerName = " 192.168.1.1"
 
 
        'Launch telnet session from the command line
        WshShell.Run "telnet " & ServerName
        'Wait until the application has loaded
        WScript.Sleep 300    
        Set ColProcesses = objWMIService.ExecQuery("Select * from Win32_Process",,48)
        For Each Process in ColProcesses
           If Process.Name = "telnet.exe"  Then
              WshShell.AppActivate Process.Name
           End If
        Next
            wscript.sleep 100
            WshShell.SendKeys "root"
            wscript.sleep 200
            WshShell.SendKeys "{ENTER}"
            wscript.sleep 200
            WshShell.SendKeys "admin"
            wscript.sleep 200
            WshShell.SendKeys "{ENTER}"
            wscript.sleep 200
            WshShell.SendKeys "reboot"
            wscript.sleep 200
            WshShell.SendKeys "{ENTER}"
            wscript.sleep 200
Set ColProcesses = objWMIService.ExecQuery("Select * from Win32_Process",,48)
        For Each Process in ColProcesses
           If Process.Name = "telnet.exe" Then
              Process.Terminate()
           End If
        Next

Open in new window

0
Comment
Question by:imarshad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24753761
SendKeys is used to send the message that a keyboard's key has been pressed to a window. So, of course, somebody must be logged on the machine.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 24755653
"Can the script be eliminated and done form inside the VB.NET code?"

Unproven code...I tested by sending simple commands like "help" and "display" while the computer was NOT locked:
(just FYI...I have zero experience using telnet)
Public Class Form1
 
    Private Const WM_CHAR As Integer = &H102
 
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
        ByVal hwnd As Integer, ByVal wMsg As Integer, _
        ByVal wParam As Integer, ByVal lParam As Integer) As Integer
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ServerName As String = "192.168.1.1"
        Dim script() As String = {"root", "admin", "reboot"}
        Dim telnet As Process = Process.Start("telnet", ServerName)
        System.Threading.Thread.Sleep(2000) ' delay to make sure it's loaded
 
        Dim c As String
        For Each cmd As String In script
            For i As Integer = 0 To cmd.Length - 1
                c = cmd.Substring(i, 1)
                PostMessage(telnet.MainWindowHandle, WM_CHAR, Asc(c), 0&)
            Next i
            PostMessage(telnet.MainWindowHandle, WM_CHAR, Asc(vbCr), 0&)
            PostMessage(telnet.MainWindowHandle, WM_CHAR, Asc(vbLf), 0&)
            System.Threading.Thread.Sleep(200)
        Next
 
        telnet.CloseMainWindow()
    End Sub
 
End Class

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24755816
I just did another simple test and was able to open a telnet window and send several commands while the computer was LOCKED.  =)
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24761627
0
 
LVL 13

Author Comment

by:imarshad
ID: 24766606
Thanks Idle_mind..... This was exactly what I was looking for....
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question