locked
Supply a value to the prompt from an unattended process RRS feed

  • Question

  • Hello
    I have a windows service written in c#
    it calls an exe with some arguments. However one of the arguments is a prompt.

    I cant prompt a user from a windows service but can I send in "prompt values" with code?

    I have tried this:

               Process myProcess = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo();           
                startInfo.FileName = "C:\\Windows\\System32\\tpmvscmgr.exe";
                startInfo.Arguments = "create /name tpmvsc /pin prompt /adminkey random /generate";            
                startInfo.RedirectStandardInput = true;
           
                startInfo.UseShellExecute = false;          
                myProcess.StartInfo = startInfo;
                myProcess.Start(); 
          
                System.IO.StreamWriter myStreamWriter = myProcess.StandardInput;

                System.Threading.Thread.Sleep(5700); //or while(true)

                myStreamWriter.WriteLine("12345678");

    It does not work however. there is no input added.
    Any advice?


    • Edited by Johannes HC Monday, January 2, 2017 2:46 PM
    Monday, January 2, 2017 2:45 PM

All replies

  • It does not work however. there is no input added.
    Any advice?

    There is nothing you can do other than rewrite the other program to not prompt the user.

    Monday, January 2, 2017 5:39 PM
  • No? How can you be sure? It should be possible to use the RedirectStandardInput from what I understand?

    Just that I  can't get it to work properly for some reason, I have managed to redirect errors but not input or output.  Very confusing..

    Anyway I cant rewrite it, it is a standard windows program.

    Tuesday, January 3, 2017 8:23 AM
  • So you are trying to start a 3rd-party executable from your own Windows service?

    But it sounds like the executable is a visual program, i.e. requires a user-interface. What you are trying to do may not be possible, but have you tried contacting whoever wrote that executable? They are the ones who would know if what you are trying to do is possible or not.


    • Edited by RJP1973 Tuesday, January 3, 2017 8:36 AM
    Tuesday, January 3, 2017 8:35 AM
  • Hmm this is confusing.
    Just to clarify I want something like this.

    http://stackoverflow.com/questions/19552012/c-sharp-process-standard-input

    I want to use the functionality of 
    RedirectStandardInput in c# 

    But cant get it to work for some reason.
    Tuesday, January 3, 2017 9:11 AM
  • Hi Johannes,

    The whole purpose of Windows Service is not to have any UI. If you want to pass value to your code, you could use a notepad. Write that value into noted and tell your code to read that notepad value. Hope this helps you.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Tuesday, January 3, 2017 1:49 PM
  • Hello 

    No this does not help. I have access to the pin I want to use but I cant use it from the service.
    I need to call the exe from a service and then enter the pin somehow.

    If I use the code above but change 
     startInfo.Arguments = "create /name tpmvsc /pin prompt /adminkey random /generate"; 

    to 

     startInfo.Arguments = "create /name tpmvsc /pin default /adminkey random /generate"; 

    It works fine but then it is created with default pin. If only the tpmvscmgr.exe had an option for pin where you enter the pin to use directly on creation all would be fine but the only allowed options are default or prompt.

    And I cant get prompt to work since for some reason I cant get hold of the input / output streams.

    If I change the arguments so it returns an error I can pick the error-stream up with RedirectStandardError so not sure what is going on and why I cant use RedirectStandardInput.

    Tuesday, January 3, 2017 1:59 PM
  • You can get the input and output streams, but there's no guarantee that the target program is actually reading the input stream.  It could be using the console APIs directly to read the key presses.

    Normally console apps don't do this, and they just read the streams, which is easier.  But where the commandline was obviously designed to _not allow_ the pin to be specified, it is more likely that that console application will not just be reading stdin.

    You can see an indication that this is actually occurring by running

    tpmvscmgr.exe create /name foo /pin prompt /adminkey random /generate

    and then hitting Ctrl-C.  Ctrl-C signals the end of stdin, which will normally cause a console app to terminate.  But tpmvscmgr.exe just echos an '*' when you hit Ctrl-C, and then evaluates the input when you hit Enter.

    It may be possible to do this with the COM APIs

    Use Virtual Smart Cards

    https://technet.microsoft.com/en-us/library/dn579265.aspx

    But those are intended for Desktop Applications, not services.

    David


    Microsoft Technology Center - Dallas

    My Blog



    Tuesday, January 3, 2017 4:33 PM
  • I see, yes I am struggling with the code but was hoping I could make it easy on myself by "faking a prompt input" but I guess I need to give that up.

    Just frustrating to have been so close. If only the tpmvscmgr had an option which allowed a pin directly instead of prompting for it I would have been done days ago.

    But thank you for clarifying it. Now at least I know why this doesn't work.

    Wednesday, January 4, 2017 9:20 AM