在新的桌面上开始一个过程

时间:2018-06-19 16:31:24

标签: c# process desktop

我正在尝试在新桌面上启动一个过程。

这是我到目前为止的代码,但是它在后台打开了该过程。

完美创建了新桌面。所以这一切都很好。

但是新过程不会在新桌面上打开。

我需要将外部过程放在最前面或在新桌面中打开

在新桌面上启动该程序时,我在哪里做错了?

可以帮忙吗?

感谢您的任何输入

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);

    }
}

}

1 个答案:

答案 0 :(得分:0)

add_action()