1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace WindowsFormsApplication1
{
static class Program
{
[Flags]
public enum AccessRight : uint
{
DESKTOP_READOBJECTS = 0x00000001,
DESKTOP_CREATEWINDOW = 0x00000002,
DESKTOP_CREATEMENU = 0x00000004,
DESKTOP_HOOKCONTROL = 0x00000008,
DESKTOP_JOURNALRECORD = 0x00000010,
DESKTOP_JOURNALPLAYBACK = 0x00000020,
DESKTOP_ENUMERATE = 0x00000040,
DESKTOP_WRITEOBJECTS = 0x00000080,
DESKTOP_SWITCHDESKTOP = 0x00000100,
GENERIC_ALL = (DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU |
DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP)
};
[Flags]
public enum AccountHook
{
Allow = 1,
Disallow = 0
};
public enum HandleInheritance
{
Inherit,
None
};
[StructLayout(LayoutKind.Sequential)]
public struct SecAttrib
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}
[DllImport("user32.dll")]
public static extern IntPtr OpenDesktop(string lpszDesktop,
uint dwFlags,
bool fInherit,
uint dwDesiredAccess);
[DllImport("user32.dll")]
public static extern bool SwitchDesktop(IntPtr hDesktop);
[DllImport("user32.dll")]
public static extern IntPtr CreateDesktop(string lpszDesktop,
IntPtr lpszDevice,
IntPtr pDevmode,
int dwFlags,
AccessRight dwDesiredAccess,
IntPtr lpsa);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr OpenInputDesktop(AccountHook dwFlags, bool fInherit, AccessRight dwDesiredAccess);
[DllImport("user32.dll", EntryPoint = "CloseDesktop", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseDesktop(IntPtr handle);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetThreadDesktop(IntPtr hDesktop); /// <summary>
/// Point d'entrée principal de l'application.
/// </summary>
[STAThread]
static void fenetre() // faut que la fenêtre soit créer dans un thread à part, on arrive sur un bureau vide sinon et la seule facon d'en sortir est de se délogguer
{
IntPtr hdefault = Program.OpenInputDesktop(Program.AccountHook.Disallow, false, Program.AccessRight.GENERIC_ALL);
IntPtr hdesk = Program.CreateDesktop("winformtest", IntPtr.Zero, IntPtr.Zero, 0, Program.AccessRight.GENERIC_ALL, IntPtr.Zero);
if (hdesk != IntPtr.Zero)
{
if (SetThreadDesktop(hdesk)==true)
Program.SwitchDesktop(hdesk);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
System.Threading.Thread.Sleep(1000);
Program.SwitchDesktop(hdefault);
SetThreadDesktop(hdefault);
}
}
static void Main()
{
Thread nthread = new Thread(new ThreadStart(Program.fenetre));
// Start the thread
nthread.Start();
// Spin for a while waiting for the started thread to become
// alive:
while (!nthread.IsAlive) ;
// Put the Main thread to sleep for 1 millisecond to allow oThread
// to do some work:
Thread.Sleep(1);
// Wait until oThread finishes. Join also has overloads
// that take a millisecond interval or a TimeSpan object.
nthread.Join();
}
}
} |
Partager