1

i am trying to run a process but want it to be hidden - so i guess, i want to run it in a different desktop.

public static void LaunchCommand2(string strCommand)
        {
            // Variables
            PROCESS_INFORMATION processInfo = new PROCESS_INFORMATION();
            STARTUPINFO startInfo = new STARTUPINFO();
            Boolean bResult = false;
            IntPtr hToken = IntPtr.Zero;
            UInt32 uiResultWait = WAIT_FAILED;
            IntPtr desktop = CreateDesktop("temp", IntPtr.Zero, IntPtr.Zero, 0, (long)DESKTOP_ACCESS_MASK.GENERIC_ALL, IntPtr.Zero);
            startInfo.lpDesktop = "temp";
            try
            {
                startInfo.cb = Marshal.SizeOf(startInfo);
                startInfo.dwFlags = STARTF_USESHOWWINDOW;
                startInfo.wShowWindow = 0;
                unsafe
                {
                    WTSQueryUserToken(WTSGetActiveConsoleSessionId(),out hToken);
                }

                bResult = Win32.CreateProcessAsUser(
                    hToken,
                    null,
                    strCommand,
                    IntPtr.Zero,
                    IntPtr.Zero,
                    false,
                    0,
                    IntPtr.Zero,
                    null,
                    ref startInfo,
                    out processInfo
                );
                if (!bResult) { throw new Exception("CreateProcessAsUser error #" + Marshal.GetLastWin32Error()); }

                // Wait for process to end
                uiResultWait = WaitForSingleObject(processInfo.hProcess, INFINITE);
                if (uiResultWait == WAIT_FAILED) { throw new Exception("WaitForSingleObject error #" + Marshal.GetLastWin32Error()); }
            }
            finally
            {
                // Close all handles
                CloseHandle(hToken);
                CloseHandle(processInfo.hProcess);
                CloseHandle(processInfo.hThread);
            }
        }

but it does not work. i dont know if i am setting new desktop to a startInfo correctly. Thanks in advance.

Alexander Capone
  • 528
  • 3
  • 16
  • 1
    Why not use a windows service? – Matthijs Jun 25 '14 at 13:08
  • Also, if you set "ShowInTaskbar" on your form to false it will not show. – Gusman Jun 25 '14 at 13:10
  • 2
    if you want to hide a process just use the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden; the_StartInfo.UseShellExecute = false; – MRebai Jun 25 '14 at 13:11
  • @Gusman what do you mean? how to set it to true? – Alexander Capone Jun 25 '14 at 13:13
  • Each form has a property named "ShowInTaskbar", if you set it to false and after form_load you do a Hide(), the window will disappear and will not have a taskbar icon, so it will be hidden. – Gusman Jun 25 '14 at 13:37
  • @Gusman did you notice it is not C# code? – Alexander Capone Jun 25 '14 at 15:51
  • Oh, so you mean the app you want to start is not your app or is not in c#? then moez gave you the right solution... and also this is a possible duplicate of http://stackoverflow.com/questions/5377423/hide-console-window-from-process-start-c-sharp – Gusman Jun 25 '14 at 16:00

0 Answers0