Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why can't I use SendKeys on Terminal Server???? 1

Status
Not open for further replies.

hneal98

Programmer
Aug 13, 2002
1,637
US
Any ideas why when running my application through terminal server; I open another application and try to send keystrokes via sendkeys to it, but the application never gets the focus and never receives the keystrokes?

I have tried opening the application with both shell and System.Diagnostics.Process.start. Then for shell, I use the AppWinStyle.NormalFocus parameter. This works fine on my PC, but not on Terminal server. I also tried using the SetForegroundWindow(ipWinHandle) API, but that does not work on my PC or on Terminal Server.

Any ideas as to what I can do?


Thanks.

ps. If you need to see my code, just let me know.
 
Is "Terminal server" some sort of emulator? If it is, I don't expect it to behave like a Windows OS.

Does your code fully work on the PC?


__________________________________________
Try forum1391 for lively discussions
 
Because terminal server is an emulated PC, and not a real computer. Since there are potentially many sessions running, and sendkeys simulates a user pressing a key on the physical keyboard, to which of the many sessions should the keystroke go?

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Terminal Server, to my knowledge is not an emulated PC, but remote access to an actual PC/Server. Since that is the case and since the remore PC/Server also uses Win32, I don't see why Sendkeys should not work. Especially since the application is being run from the remote PC/Server. The only thing Terminal Server is doing, is allowing a way for me to access that remote PC/Server from my PC.

By the way, when I say Terminal Server, it is actually Remote Desktop. My PC is running Windows XP Professional and the remote PC/Server is running Windows 2000.
 
First, any software that allows a simulation of a remote PC is an emulator. The degree to which that emulation is sustained greatly differs between emulators.

>Terminal Server, to my knowledge is not an emulated PC, but remote access to an actual PC/Server. Since that is the case and since the remore PC/Server also uses Win32, I don't see why Sendkeys should not work. Especially since the application is being run from the remote PC/Server. The only thing Terminal Server is doing, is allowing a way for me to access that remote PC/Server from my PC.

You are missing the point that there is an emulator that makes it look like you are directly using the remote computer. By drawing too many such conclusions you may not be able to solve this problem.

When Sendkeys is excuted, which of the two computers is being focused on? This is a sticky point that usually makes some emulators fail miserably.

The fact that Remote Desktop is a Microsoft product does not mean that it'll work they way you hope it should.

And, I'm still waiting for your answer to: "Does your code fully work on a single PC?".


__________________________________________
Try forum1391 for lively discussions
 
If your code already works perfectly on a single PC, post it and show where it is failing.

__________________________________________
Try forum1391 for lively discussions
 
Dimandja, thanks for the insite. Obviously, I am not an expert. I thought about it logically, not from knowledge. I have always thought of programs like dameware, PC Anyware, Terminal Services, etc, to be like a tunnel to my PC, but I guess I thought wrong.

Anyway, to answer you question, yes the application works perfectly on my PC. Also, I tested the application on Remote Desktop, for Notepad and that worked fine. It seems there must be some wierd relationship between Remote Destop and this partular application that keeps the sendkeys from working correctly.

Oh well. Back to the drawing board. If you have any other ideas, let me know. BTW, here is my code:


Code:
    Public Function OpenExeApp(ByVal bCloseWhenDone As Boolean, ByVal strPath As String) As Boolean
        '    Dim strFilePathAndName As String
        Dim ipWinHandle As IntPtr
        Dim i As Integer
        '    'This function opens the splitter application and uses it and then closes it.

' Get a pointer to the current process.
        Dim cp As Process = System.Diagnostics.Process.GetCurrentProcess()
        Dim Prs As Process() = Process.GetProcesses()

        For Each pr As Process In Prs
            Debug.WriteLine(pr.ProcessName.ToString)
            If pr.ProcessName = "Custom820FileSplitter" Then
                Exit Function
            End If
        Next

        ' Start a new program process.
        Try
            Dim p As Process = New System.Diagnostics.Process
            p.StartInfo.FileName = "C:\820Submitter\Testvb\bin\Custom820FileSplitter.exe"
            Dim ProcID As Integer
            p.Start("C:\820Submitter\Testvb\bin\Custom820FileSplitter.exe")
            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal

            'system.Diagnostics.Process.

            Dim hWnd As IntPtr = FindWindow(Nothing, "custom820FileSplitter")

            SetForegroundWindow(hWnd)


        Catch ex As System.InvalidOperationException
            MsgBox(ex.Message)
        End Try

        
        SendKeys.SendWait("{Tab 5}\\Mhcbiztalk1\820import\Done\LOAD\WA_050103_590_8027781_4594_820_HNK_ST0.txt")
        'Tab to spinner to enter # of files to split to.
        SendKeys.SendWait("{Tab 2}")
        'Hi light current data and delete
        SendKeys.SendWait("{End}")
        SendKeys.SendWait("+{Home}")
        SendKeys.SendWait("{Del}")
        'Enter New Data
        SendKeys.SendWait("500")
        'Tab to the Generate button to start process
        SendKeys.SendWait("{Tab 2}")
        'Press the Generate button to start process
        SendKeys.SendWait("{Enter}")

        'Once process is complete, tab to Quit button
        SendKeys.SendWait("{Tab}")
        ' Exit Application when complete.
        SendKeys.SendWait("{Enter}")



        ' Set the focus back to our app.
        SetForegroundWindow(cp.MainWindowHandle)


    End Function


I have also tried this using the shell function. I like it better because it is easier, but I get the same results.
 
A couple of things to take care of:

1. Try adding AppActivate() before each Sendkeys.
2. You need to use System.Threading.Thread.Sleep() (allow for a delay of a couple of seconds) after each Sendkeys: this should give Sendkeys (a slow operation) enough time to do its job.

__________________________________________
Try forum1391 for lively discussions
 
Ok. That worked. I did not need the appactivate because I am already using the SetForegroundWindow(p.MainWindowHandle). I put the System.Threading.Thread.Sleep(1000) after it and put sleep(500) after each of the sendkeys and it works like a charm.

Any explanation as to why I did not need that on my PC, but needed it on Remote Desktop?
 
As I said before Sendkeys is a slow process. Add the lag for communication to the remote PC and you need to pause a little between each Sendkeys. Because of that, I always use delays after each Sendkeys, even on a single PC.

I'm happy you made your application work.

__________________________________________
Try forum1391 for lively discussions
 
Actually, you helped me make my app work. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top