0

I am trying to run a bunch of processes in the background using the CMD prompt from my support application.

I have successfully done this for standard commands, but when trying to run a command as an administrator (to retrieve and end processes), the console window will briefly appear on the screen.

Code:

public static bool checkRunning(string procName)
    {
        var ss = new SecureString();
        ss.AppendChar('T');
        ss.AppendChar('a');
        ss.AppendChar('k');
        ss.AppendChar('e');
        ss.AppendChar('c');
        ss.AppendChar('a');
        ss.AppendChar('r');
        ss.AppendChar('e');
        ss.AppendChar('9');
        ss.AppendChar('9');
        ss.MakeReadOnly();

        //ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/C tasklist /S " + Program.servName + " /FI \"SESSION eq " + Program.sessID + "\" /FO CSV /NH")

        ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/C tasklist /S " + Program.servName + " /FI \"SESSION eq " + Program.sessID + "\" /FO CSV /NH";


            startInfo.WorkingDirectory = @"C:\windows\system32";
            startInfo.Verb = "runas";
            startInfo.Domain = "mydomain";
            startInfo.UserName = "ADMINuser";
            startInfo.Password = ss;

            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.CreateNoWindow = true;


        string strCheck = " ";

        Process proc = Process.Start(startInfo);
        proc.OutputDataReceived += (x, y) => strCheck += y.Data;

        proc.BeginOutputReadLine();
        proc.WaitForExit();

        if (strCheck.Contains(procName))
        {
            return true;              
        }
        else
        {
            return false;
        }
    }

From what I understand, setting shellExecute=false, createNoWindow=true and ProcessWindowStyle.Hidden should resolve the issue, but I believe it is not working due to the required admin log in.

Does anyone know of a solution or suitable workaround to this problem? Any help is much appreciated, Many thanks

Bassie
  • 9,529
  • 8
  • 68
  • 159

2 Answers2

0

You can use following code :

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

const int SW_HIDE = 0;
const int SW_SHOW = 5;  

static void Main(string[] args)
        {
            var handle = GetConsoleWindow();
            ShowWindow(handle, SW_HIDE);
        }
Jean
  • 524
  • 4
  • 21
  • Hi Jean, thanks for your quick response. I tried adding the above to my Program.cs file, but Visual Studio seems to think I am missing an directive or assembly reference (with ref to [DllImport("kernel32.dll")]) - am I putting this in the wrong place? – Bassie Nov 11 '15 at 14:41
  • Sorry basijan, I forgot to add. You need to reference to : System.Runtime.InteropServices; – Jean Nov 11 '15 at 14:51
  • Thanks Jean - that removed the error I was seeing. I have added the code exactly how you put it above, but I am still seeing the CMD windows appearing when executing. Do I need to somehow attach the SW_HIDE property to the method which sends the CMD process? – Bassie Nov 11 '15 at 14:57
  • Thats really strange that I tried to run cmd.exe as you do in my console application, but it didnt give me window output. – Jean Nov 11 '15 at 23:01
  • Hi Jean, that is strange - could it be that I am running a Windows Forms Application rather than a Console App? – Bassie Nov 12 '15 at 13:00
0

From MSDN site on ProcessStartInfo.CreateNoWindow Property :

Remarks

If the UseShellExecute property is true or the UserName and Password properties are not null, the CreateNoWindow property value is ignored and a new window is created.

There is no workaround or resolution mentioned, and I have been unable to find one anywhere.

I have had to resort to me application briefly displaying CMD windows when running certain processes (The CreateNoWindow property works when not using UserName and Password).

Bassie
  • 9,529
  • 8
  • 68
  • 159