0

I want to start an application from console, using a specific user account, but without a window being shown. I have gone through multiple Stack Overflow articles on this subject, but none of the suggested solutions seem to work for me. I can start the application, but I cannot prevent a window from popping up when passing username/password.

I managed to get the following code to run on my computer (Windows 10), but it fails to work on the target machine (Windows 7).

Target framework: .NET 4.5.2

Code is executed in a windowless WPF application using output type Console Application, as normal console applications failed to work.

private void StartProcess(string appPath, string user, SecureString password, string domain)
{
    var processStartInfo = new ProcessStartInfo
    {
        FileName = appPath,
        WorkingDirectory = Path.GetDirectoryName(appPath),
        CreateNoWindow = true,
        UseShellExecute = false,
        WindowStyle = ProcessWindowStyle.Hidden,
        Password = password,
        UserName = user,
        Domain = domain,
        RedirectStandardError = false,
        RedirectStandardOutput = false
    };

    Process.Start(processStartInfo);
}

My application will be started from console and the target is also a console app.

How can I ensure that the process is run in the background without a window appearing under Windows 7?

Some of the articles:

Christopher
  • 9,634
  • 2
  • 17
  • 31
Greg
  • 1,076
  • 1
  • 9
  • 17
  • Why is this tagged as WPF ? – Peregrine Oct 22 '18 at 15:40
  • Please include links to some of the articles you've tried that have not worked. For instance, [Running an application as different user](https://stackoverflow.com/q/38053199/215552) mentions the use of PsExec, and [Run Code as a different user](https://stackoverflow.com/q/1168571/215552) mentions the use of P/Invoke. – Heretic Monkey Oct 22 '18 at 15:41
  • @Peregrine From the OP "Code is executed in a windowless WPF application" – Heretic Monkey Oct 22 '18 at 15:41
  • @HereticMonkey: Calling it WPF is at least questionable. It is good to include the information about the environment, but this seems to be mostly about Process wiht `CreateNoWindow = true` not working as expected. – Christopher Oct 22 '18 at 16:05
  • @Christopher... so edit the question and remove the tag. Not sure why no one's done this... – Heretic Monkey Oct 22 '18 at 16:07
  • @HereticMonkey: Because nobody but yourself felt it relevant. And even I only did it because this discussion is annoying and distracting. And it was the best way to stop it :) – Christopher Oct 22 '18 at 16:09
  • Just to clarify the code you posted is extracted from a correctly working app on windows 10 and capable to launch an application without prompting for a password but on windows 7 you are getting a prompt ? – A. Lion Oct 23 '18 at 09:12
  • @P.Lion: No, the application works in both cases, but in Windows 7 the started process is visible and running in the foreground. I want the process to run in the background, which works in Windows 10. I must add this may have something to do with user rights, but I'm unsure how this could influence the way a process is displayed. – Greg Oct 23 '18 at 10:06

2 Answers2

1

You can try this:

private void StartProcess(string appPath, string user, SecureString password, string domain)
{
    var processStartInfo = new ProcessStartInfo
    {
        FileName = appPath,
        WorkingDirectory = Path.GetDirectoryName(appPath),
        CreateNoWindow = true,
        UseShellExecute = false,
        WindowStyle = ProcessWindowStyle.Hidden,
        RedirectStandardOutput = false,
        RedirectStandardError = false,
        Verb = string.Format("runas /user:{0}\\{1} /password:{2}", domain, user, password)
    };
    Process.Start(processStartInfo);
}
  • That should be more likely to cause such an issue. As now you are a running "runas" with `CreateNoWindow = true`. A property that should not be inherented by whatever runas starts. You get similar issues when you try to use elevation on a batchfile: What you elevate is the Interpreter. – Christopher Oct 22 '18 at 16:07
  • That seems to work, cheers! Will have to test it a bit more though. – Greg Oct 22 '18 at 16:08
  • @Greg: If that works, that means all my knowledge about how Process and Runas work/interact is wrong. And I hope Dudesville Hurynnx writes an explanation. – Christopher Oct 22 '18 at 16:12
  • @Christopher: yeah, I would have expected ProcessStartInfo with credentials to have the same effect as runas. It seems to work, but I'm not quite happy with it yet. – Greg Oct 22 '18 at 16:18
  • Sorry, spoke too soon. The process is started with my current user and ignores Verb. – Greg Oct 22 '18 at 16:39
0

Wierd. CreateNoWindow = true should definitely do that (prevent a Window from spawning). Aside from the obvious cases (this is not the code you actually run), I got a few ideas. A lot of times programms need to get around not being able to use a Window. So they start a seperate process/a new instance of themself with those rights.

Services do not get interactive Sessions by default as of Vista (and you should not overeride that). So they either have a helper process started by the Task Sheduler. Or just force start it via Process. And for some old code like the (t)rusty COM Officer interop, Interactive sessiosn are explicitly needed.

If you have such a programm, you are basically playing the topmost game by another name: https://blogs.msdn.microsoft.com/oldnewthing/20110310-00/?p=11253

Christopher
  • 9,634
  • 2
  • 17
  • 31