简体   繁体   中英

Start a process in a new desktop

I am trying to start a process on a new desktop.

This is the code I have so far but it opens the process in the background.

The new desktop is created perfectly. So all this works fine.

However the new process does not open in the new desktop.

I need to bring the external process to the front or open it in the new desktop

Where am I doing wrong with regards to starting the process in the new desktop?

Can you help please?

Thanks for any input

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace AntiKeylogger
{
  //  public partial class Program : NetkioskVDesktop.Form1
 //   public partial class Program :

static class Program
{

    [DllImport("user32.dll")]
    public static extern IntPtr CreateDesktop(string lpszDesktop, IntPtr lpszDevice, IntPtr pDevmode, int dwFlags, uint dwDesiredAccess, IntPtr lpsa);

    [DllImport("user32.dll")]
    private static extern bool SwitchDesktop(IntPtr hDesktop);

    [DllImport("user32.dll")]
    public static extern bool CloseDesktop(IntPtr handle);

    [DllImport("user32.dll")]
    public static extern bool SetThreadDesktop(IntPtr hDesktop);

    [DllImport("user32.dll")]
    public static extern IntPtr GetThreadDesktop(int dwThreadId);

    [DllImport("kernel32.dll")]
    public static extern int GetCurrentThreadId();

   enum DESKTOP_ACCESS : uint

   {
        DESKTOP_NONE = 0,
        DESKTOP_READOBJECTS = 0x0001,
        DESKTOP_CREATEWINDOW = 0x0002,
        DESKTOP_CREATEMENU = 0x0004,
        DESKTOP_HOOKCONTROL = 0x0008,
        DESKTOP_JOURNALRECORD = 0x0010,
        DESKTOP_JOURNALPLAYBACK = 0x0020,
        DESKTOP_ENUMERATE = 0x0040,
        DESKTOP_WRITEOBJECTS = 0x0080,
        DESKTOP_SWITCHDESKTOP = 0x0100,

        GENERIC_ALL = (DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU |
                        DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
                        DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP),
    }


     [DllImport("kernel32.dll")]
     private static extern bool CreateProcess(
       string lpApplicationName,
       string lpCommandLine,
       IntPtr lpProcessAttributes,
       IntPtr lpThreadAttributes,
       bool bInheritHandles,
       int dwCreationFlags,
       IntPtr lpEnvironment,
       string lpCurrentDirectory);


    static void Main(string[] args)
    {

      IntPtr hOldDesktop = GetThreadDesktop(GetCurrentThreadId());
      IntPtr hNewDesktop = CreateDesktop("RandomDesktopName", IntPtr.Zero, IntPtr.Zero, 0, (uint)DESKTOP_ACCESS.GENERIC_ALL, IntPtr.Zero);

        SwitchDesktop(hNewDesktop);     

        Task.Factory.StartNew(() =>

        {
          SetThreadDesktop(hNewDesktop);

          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new NetkioskVDesktop.Form1());

         Process.Start("C:\\Windows\\System32\\Notepad.exe");


        }).Wait(); 
        SwitchDesktop(hOldDesktop);    
        CloseDesktop(hNewDesktop);

    }
}

}

    [DllImport("user32.dll")]
    public static extern IntPtr CreateDesktop(string lpszDesktop, IntPtr lpszDevice, IntPtr pDevmode, int dwFlags, uint dwDesiredAccess, IntPtr lpsa);

    [DllImport("user32.dll")]
    private static extern bool SwitchDesktop(IntPtr hDesktop);

    [DllImport("user32.dll")]
    public static extern bool CloseDesktop(IntPtr handle);

    [DllImport("user32.dll")]
    public static extern bool SetThreadDesktop(IntPtr hDesktop);

    [DllImport("user32.dll")]
    public static extern IntPtr GetThreadDesktop(int dwThreadId);

    [DllImport("kernel32.dll")]
    public static extern int GetCurrentThreadId();

    enum DesktopAccess : uint
    {
        DesktopReadobjects = 0x0001,
        DesktopCreatewindow = 0x0002,
        DesktopCreatemenu = 0x0004,
        DesktopHookcontrol = 0x0008,
        DesktopJournalrecord = 0x0010,
        DesktopJournalplayback = 0x0020,
        DesktopEnumerate = 0x0040,
        DesktopWriteobjects = 0x0080,
        DesktopSwitchdesktop = 0x0100,

        GenericAll = DesktopReadobjects | DesktopCreatewindow | DesktopCreatemenu |
                     DesktopHookcontrol | DesktopJournalrecord | DesktopJournalplayback |
                     DesktopEnumerate | DesktopWriteobjects | DesktopSwitchdesktop
    }

    public Form1()
    {
        InitializeComponent();
    }

    //You must press this button to start
    private void btnInit_Click(object sender, EventArgs e)
    {
        Init();
    }

    private static void Init()
    {
        // old desktop's handle, obtained by getting the current desktop assigned for this thread
        var hOldDesktop = GetThreadDesktop(GetCurrentThreadId());

        // new desktop's handle, assigned automatically by CreateDesktop
        var hNewDesktop = CreateDesktop(GetRandomName(), IntPtr.Zero, IntPtr.Zero, 0, (uint)DesktopAccess.GenericAll, IntPtr.Zero);

        // switching to the new desktop
        SwitchDesktop(hNewDesktop);

        // Random login form: used for testing / not required
        var passwd = "";

        // running on a different thread, this way SetThreadDesktop won't fail
        Task.Factory.StartNew(() =>
        {
            // assigning the new desktop to this thread - so the Form will be shown in the new desktop)
            SetThreadDesktop(hNewDesktop);

            var inbox = new CustomInbox();
            inbox.FormClosing += (sender, e) => { passwd = inbox.Value; };

            Application.Run(inbox);


        }).Wait();  // waits for the task to finish
        // end of login form

        // if got here, the form is closed => switch back to the old desktop
        SwitchDesktop(hOldDesktop);

        // disposing the secure desktop since it's no longer needed
        CloseDesktop(hNewDesktop);

        MessageBox.Show(@"Password, typed inside secure desktop: " + passwd, @"Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

    //This get a random name for the new desktop
    private static string GetRandomName()
    {
        var value = DateTime.Now.ToLongTimeString();
        value = value.GetHashCode().ToString().Replace("-", "").ToLowerInvariant();
        return value;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM