SendKeys from a Windows Service

Hello All,

I am having a problem sending keystrokes from a Windows Service Application.

I added the <Assembly: AllowPartiallyTrustedCallers()> to the assembly file and referenced the System.Windows.Forms and imported in my service file (Imports System.Windows.Forms).

There is a password field that pops up on the screen and is the only thing in focus that the following block writes to. I even tried this in a forms app and it worked like a charm, but for some reason the service is not accepting the Sendkeys.

I narrowed down the block with the error by use of a logged entry:

For i = 1 To passLenght
skey = Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(pass, i), 1)

'WriteToLogFile("Sending keys to screen: " & skey.ToString, Prod)

'here is where the problem seem to start

SendKeys.SendWait(skey)

Next

SendKeys.SendWait("{enter}")

The error on the log is as follows:

System.Security.SecurityException: Hook cannot be created.
at System.Windows.Forms.SendKeys.InstallHook()
at System.Windows.Forms.SendKeys.Send(String keys, Control control, Boolean wait)
at System.Windows.Forms.SendKeys.SendWait(String keys)
at <Application Name>.<Form Name>.<Sub Proc Name>(Object username, Object environment, Object PROD, Object pass)
The Zone of the assembly that failed was:
MyComputer <PROD as in above>

Can someone please advise.

Thanks
Michael





Answer this question

SendKeys from a Windows Service

  • Andy Ho

    Keep in mind that SendKeys from a Windows Service will no longer work in Windows Vista due to Session 0 isolation. See this link for more information:

    http://msdn.microsoft.com/windowsvista/reference/appcompat/default.aspx pull=/library/en-us/dnlong/html/AppComp.asp#appcomp_topic10

    Chris

  • FannwongCindy

    Process.Start() has some overloads that take an "arguments" parameter. So you can just use:

    System.Diagnostics.Process.Start("c:\apppath\app.exe", "some.ini")



  • Josh Mackey

    It looks like you're trying to go from NY to LA by crossing the ocean! (in other words, you're taking the LONG way around).

    If you just want to manipulate a text file there shouldn't be a need to do all that with a command prompt. Just use System.IO.File methods to open the file, parse what you need using standard String methods, then move the file. System.IO.File can open, read, copy, and delete files with ease.



  • thekaran

    Hi,

    it's not about manipulating a text file, the crunch is to raise a batch file as a process and punch some commands automatically when a file is created inside a specifc directory.

    the process is somewhat like this

    1. a user copies a file in a shared dierctory from somewhere.

    2. another user "Y" on the other side will open this file, and raise a batch file which is supposed to talk to some machine

    3. "Y" then opens and copy paste some contents from the file and pass this content with a command to this batch file.

    step 2 and 3 needs to be automated, i am actually now done with both steps, but this sendkeys api is not perfect, it's not good with time synch. sometimes it completes the whole command sometimes it doesn't.

    so i am used to put the thread for sleep for about 500 milliseconds but this is somewhat hardcoding.

    is there any workaround.

    Thanks and Regards,

    brij


  • Alex Levin

    Hi Kimble,

    Thanks for your input.

    I will try and update the forum


    Thanks again

    Michael

  • SQL Newb

    Chris Dunaway wrote:
    Keep in mind that SendKeys from a Windows Service will no longer work in Windows Vista due to Session 0 isolation. See this link for more information:

    http://msdn.microsoft.com/windowsvista/reference/appcompat/default.aspx pull=/library/en-us/dnlong/html/AppComp.asp#appcomp_topic10

    Chris

    Vista is going to break a lot of apps where the developer ignored good practice... We're going to see SO many posts in here after its released



  • iGary

    Hi Thanks for the mention. I had tried that but it only works with the local system account and it messes with the "assumed" user that I need. Anyhow I have changed this temporary by hard coding it. (tested and working)

    BUT have some problems here:

    One: open the link to the actual exe sitting in a folder
    Dim psInfo As New System.Diagnostics.ProcessStartInfo _
    ("PATH HERE\application.lnk")

    psInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
    Dim myProcess As Process = System.Diagnostics.Process.Start(psInfo)


    two: send the password to the dialog box which is "modal"
    <this should be ok based on some testing and log entries>


    I am having some trouble running the link is there any way that you can run an exe with a parameter e.g.

    c:\apppath\app.exe some.ini

    Any suggestions appreciated

    Thanks

    Michael

  • JEAN BAPTISTE LOTY

    Hi,

    I am also trying to do the same.

    My service is a kind of directorywatcher service, which looks for creating of files in a given folder, and the file thus created recently must be picked up, then i start a .bat file, and extract a particular field from this text file, and write up a command with the text of this given file. then i am supposed to delete this file as well.

    now if i do the same using a service it'll be Quiet and good for me i tried something like this

    Process myProcess = new Process();

    myProcess.StartInfo.FileName = executionInstance.StartInfo.FileName; //the command name, in this case it's c:\windows\system32\cmd.exe

    //read the command from the stream

    myProcess.StartInfo.RedirectStandardInput = true;

    myProcess.StartInfo.UseShellExecute = false;

    myProcess.Start();

    StreamWriter _cmdWithText = myProcess.StandardInput;

    _cmdWithText.WriteLine("type " + executionInstance.FilePath + @" > C:\TestWithCommand.txt"); \\if the file is created, here i am just trying to use a command like type, and redirect the contents

    _cmdWithText.WriteLine();

    _cmdWithText.Close();

    myProcess.WaitForExit();

    myProcess.Close();

    the program doesn't throw any kind of error but doesn't work as well,

    Thanks and Regards

    Brij


  • DJBurkey

    Hi fellas,

    Thanks for your input however I am still having the problem with having the application run correctly. If i double click on the link or on the actual app it works but when I run as a process it doesnt work as it should.

    Thanks for your contributions. I will find some more information and update this post later

    Michael

  • Thomaschr

    Keep us posted if you get anywhere with this.

    We're trying to use sendkeys to automate testing of a particular keyboard handler, and we use CruiseControl as a service to provide continuous integration testing.

    I've tried explicitly asserting UnmanagedCode, and the service is running under an administor's account, but always get the same exception.


  • rekoms

    You'll probably have to set the service to allow "Interact with Desktop" (don't remember exactly where that option is but a quick search of the forums should reveal it for you).

    But all in all, this is a bad idea. Services are not meant to interact directly with a user session. That kind of interaction is to be accomplished by a service controller application. If the program must interact with a user session then it probably should not be a service. It should be an application that runs from the system tray.



  • SendKeys from a Windows Service