SHARE
TWEET

Create new desctop C#

a guest Oct 10th, 2013 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Administrator
  4.  * Date: 23.09.2013
  5.  * Time: 12:32
  6.  *
  7.  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8.  */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Runtime.InteropServices;
  14. using System.ComponentModel;
  15. using System.Threading;
  16.  
  17. namespace ScreenBlock
  18. {
  19.         /// <summary>
  20.         /// Description of Block.
  21.         /// </summary>
  22.  
  23.        
  24.        
  25.         internal class Block
  26.         {
  27.                
  28.                 // Fields
  29.         private const uint GENERIC_ALL = 0x1ff;
  30.  
  31.         [DllImport("user32.dll", SetLastError = true)]
  32.         [return: MarshalAs(UnmanagedType.Bool)]
  33.         private static extern bool CloseDesktop ( IntPtr hDesktop );
  34.  
  35.         [DllImport("user32.dll", SetLastError = true)]
  36.         [return: MarshalAs(UnmanagedType.Bool)]
  37.         private static extern bool CloseWindowStation ( IntPtr hWinSta );
  38.  
  39.         [DllImport("user32.dll", SetLastError = true)]
  40.         private static extern IntPtr CreateDesktop ( string lpszDesktop, string lpszDevice, IntPtr pDevmode, uint dwFlags, uint dwDesiredAccess, IntPtr Zero );
  41.  
  42.         [DllImport("kernel32.dll", SetLastError = true)]
  43.         [return: MarshalAs(UnmanagedType.Bool)]
  44.         private static extern bool CreateProcess ( string lpApplicationName, string lpCommandLine, IntPtr pcZero, IntPtr thZero, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation );
  45.  
  46.         [DllImport("user32.dll", SetLastError = true)]
  47.         private static extern IntPtr CreateWindowStation ( string lpWinSta, uint dwFlags, uint acessMask, IntPtr zero );
  48.  
  49.         [DllImport("user32.dll", SetLastError = true)]
  50.         [return: MarshalAs(UnmanagedType.Bool)]
  51.         private static extern bool EnumWindows ( EnumProc lpEnumProc, IntPtr lParam );
  52.  
  53.         [DllImport("user32.dll", SetLastError = true)]
  54.         private static extern IntPtr FindWindow ( string cap, string cls );
  55.  
  56.         [DllImport("user32.dll", SetLastError = true)]
  57.         private static extern IntPtr GetProcessWindowStation ();
  58.  
  59.         [DllImport("user32.dll", SetLastError = true)]
  60.         private static extern IntPtr GetThreadDesktop ( int thId );
  61.  
  62.         [DllImport("user32.dll", SetLastError = true)]
  63.         private static extern int GetWindowText ( IntPtr hWnd, StringBuilder sb, int max );
  64.  
  65.         [DllImport("user32.dll", SetLastError = true)]
  66.         [return: MarshalAs(UnmanagedType.Bool)]
  67.         private static extern bool SwitchDesktop ( IntPtr hDesktop );
  68.  
  69.         [DllImport("user32.dll", SetLastError = true)]
  70.         private static extern IntPtr OpenInputDesktop ( uint dwFlags, bool Inherit, uint dwDesiredAccess );
  71.  
  72.         [DllImport("user32.dll", SetLastError = true)]
  73.         [return: MarshalAs(UnmanagedType.Bool)]
  74.         private static extern bool SetProcessWindowStation ( IntPtr hWinSta );
  75.  
  76.         [DllImport("user32.dll", SetLastError = true)]
  77.         [return: MarshalAs(UnmanagedType.Bool)]
  78.         private static extern bool SetThreadDesktop ( IntPtr hDesktop );
  79.  
  80.  
  81.         // Nested Types
  82.         private delegate bool EnumProc ( IntPtr hWnd, IntPtr lParam );
  83.  
  84.         [StructLayout(LayoutKind.Sequential)]
  85.         internal struct PROCESS_INFORMATION
  86.         {
  87.             public IntPtr hProcess;
  88.             public IntPtr hThread;
  89.             public int dwProcessId;
  90.             public int dwThreadId;
  91.         }
  92.  
  93.         [StructLayout(LayoutKind.Sequential)]
  94.         private struct STARTUPINFO
  95.         {
  96.             public int cb;
  97.             public string lpReserved;
  98.             public string lpDesktop;
  99.             public string lpTitle;
  100.             public int dwX;
  101.             public int dwY;
  102.             public int dwXSize;
  103.             public int dwYSize;
  104.             public int dwXCountChars;
  105.             public int dwYCountChars;
  106.             public int dwFillAttribute;
  107.             public int dwFlags;
  108.             public short wShowWindow;
  109.             public short cbReserved2;
  110.             public IntPtr lpReserved2;
  111.             public IntPtr hStdInput;
  112.             public IntPtr hStdOutput;
  113.             public IntPtr hStdError;
  114.                
  115.                 public static void Block()
  116.                 {
  117.                         int num;
  118.             string temp;
  119.             IntPtr hObject = CreateDesktop("NewDesktop", null, IntPtr.Zero, 1, 0x1ff, IntPtr.Zero);
  120.             if (!SUCCESS(hObject)) {
  121.                 throw new Win32Exception(_geterr());
  122.             }
  123.             if (!SetThreadDesktop(hObject) && ((num = _geterr()) != 0)) {
  124.                 throw new Win32Exception(num);
  125.             }
  126.             IntPtr hDesktop = OpenInputDesktop(1, false, 0x1ff);
  127.             if (!SwitchDesktop(hObject) && ((num = _geterr()) != 0)) {
  128.                 throw new Win32Exception(num);
  129.             }
  130.             _shell(Environment.GetEnvironmentVariable("windir") + @"\notepad.exe", "NewDesktop");
  131.             Thread.Sleep(0x1388);
  132.  
  133.             EnumWindows(delegate( IntPtr hWnd, IntPtr lParam ) {
  134.                 if (!string.IsNullOrEmpty(temp = _getwincap(hWnd))) {
  135.                     Console.WriteLine(temp);
  136.                 }
  137.                 return true;
  138.             }, IntPtr.Zero);
  139.  
  140.             Thread.Sleep(0x3e8);
  141.             if (!SwitchDesktop(hDesktop) && ((num = _geterr()) != 0)) {
  142.                 throw new Win32Exception(num);
  143.             }
  144.             if (!SetThreadDesktop(hDesktop) && ((num = _geterr()) != 0)) {
  145.                 throw new Win32Exception(num);
  146.             }
  147.             if (!CloseDesktop(hObject) && ((num = _geterr()) != 0))
  148.                 throw new Win32Exception(num);
  149.             Console.Read();
  150.         }
  151.  
  152.         private static int _geterr () {
  153.             return Marshal.GetLastWin32Error();
  154.         }
  155.  
  156.         private static string _getwincap ( IntPtr hWnd ) {
  157.             StringBuilder sb = new StringBuilder(0xff);
  158.             int length = GetWindowText(hWnd, sb, sb.Capacity);
  159.             return sb.ToString(0, length);
  160.         }
  161.  
  162.         private static void _shell ( string file, string desktop ) {
  163.             STARTUPINFO startupinfo;
  164.             PROCESS_INFORMATION process_information;
  165.             int num;
  166.             startupinfo = new STARTUPINFO {
  167.                 cb = Marshal.SizeOf(typeof(STARTUPINFO)),
  168.                 lpDesktop = desktop
  169.             };
  170.             if (!CreateProcess(file, null, IntPtr.Zero, IntPtr.Zero, false, 0x20, IntPtr.Zero, null, ref startupinfo, out process_information) && ((num = _geterr()) != 0)) {
  171.                 throw new Win32Exception(num);
  172.             }
  173.         }
  174.  
  175.         private static bool SUCCESS ( IntPtr hObject ) {
  176.             return (hObject != IntPtr.Zero);
  177.         }
  178.                
  179.                 }
  180.         }
  181. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top