0

Pretty new to C#, I'm trying to create a WinForms Application that will run ScanState and capture the output from it. I've got this code as a test...

private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            process.StartInfo.FileName = $"amd64\\scanstate.exe"; ;
            process.StartInfo.Arguments = "";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.RedirectStandardInput = true;
            process.Start();
            string q = "";
            string err = "";
            while (!process.HasExited)
            {
                q += process.StandardOutput.ReadToEnd();
                err += process.StandardError.ReadToEnd();
            }
            //label1.text = q;
            MessageBox.Show(q.ToString());
            MessageBox.Show(err);
        }

But all I get is two blank message boxes. Inspecting q shows that apparently it says something. The inspector says this: \r\0\n\0s\0t\0r\0a\0t\0o\0r\0 \0p\0r\0i\0v\0i\0l\0e\0g\0e\0s\0 \0a\0r\0e\0 \0r\0e\0q\0u\0i\0r\0e\0d\0 \0t\0o\0 \0r\0u\0n\0 \0t\0h\0i\0s\0 \0t\0o\0o\0l\0.\0\r\0\n\0\r\r\nScanState return code: 34\r\r\n"

I'm not sure what this is or if it can even be converted to plain text.

  • The following may be helpful: https://stackoverflow.com/questions/72817251/trying-to-use-process-start-to-automate-executing-an-exe-file/72818271#72818271, https://stackoverflow.com/questions/70451991/query-user-called-via-cmd-exe-results-0-output/70452809#70452809, and https://stackoverflow.com/questions/70924974/qwinsta-server-in-vb-net-display-result-in-datagridview/70927338#70927338 – Tu deschizi eu inchid Sep 28 '22 at 00:02
  • It's not clear why you've redirected `StandardInput` when you aren't providing any input. – Tu deschizi eu inchid Sep 28 '22 at 00:06
  • For testing purposes, I was trying something else with cmd.exe. Returns fine with cmd.exe so I'm not sure why ScanState won't return. – Matthew Malone Sep 28 '22 at 00:11
  • 1
    The message appears to state that *administrator privileges are required to run this tool* – Jimi Sep 28 '22 at 01:59
  • Yes, it wants administrator access but how do I get the output to send properly to my app? – Matthew Malone Sep 28 '22 at 16:29
  • You'll need to execute it as administrator. Set `process.StartInfo.verb = "runas"` (see [here](https://stackoverflow.com/a/72678720/10024425)). Also add an application manifest to your project as shown [here](https://stackoverflow.com/a/65014450/10024425). – Tu deschizi eu inchid Sep 28 '22 at 17:05
  • It's not that. I know it needs admin access but it won't even return the output. Regardless, tried that already. – Matthew Malone Sep 28 '22 at 22:51

0 Answers0