views:

971

answers:

1

Okay, so I found this class online that "creates" a second desktop which does not run anything (ie, explorer.exe is not called and so forth).

However, this newly created desktop refuses to shutdown and go back to the orignal desktop. I have no idea whats going on. So if someone can try it on their machine, it would be very helpful.

note: assume all win api headers have been declared and work.

The class that "locks" the current dekstop:

namespace Locker
{
    class CLocker
    {
        public static int DesktopHandle;                    // Hold desktop handle.
        public static int oldDesktopHandle;
        public static int DesktopInputID;                   // Hold desktop input id.
        public static int DesktopThreadID;                  // Hold desktop thread id.
        static string DesktopName = "DL.Locker.Desktop";    // Hold the name of new created desktop.
        static FileStream TaskMan;                          // Hold the file stream object to control task manager.
        static string FastSwitching = string.Empty;         // Hold the original value of fast switching i.e. welcome screen
        static string ShutdownWithoutLogin = string.Empty;  // Hold the original value of showinh the shutdown button on welcome screen.

        /// <summary>
        /// Enabled used to enable or disable the locker
        /// </summary>
        public static bool Enabled
        {
            set
            {
                SetProcessPriorityHigh();                       // Set the process priority to high.
                if (value)                                      // Enable or disable the locker?
                {
                    CreateNewDesktop();                         // Creating new desktop.
                    StartProcess(Application.ExecutablePath);   // Starting the locker form, to allow the user to enter login info.
                }
                else
                {
                    DestroyDesktop();                           // Destroy the desktop.
                    ExitProcess(0);                             // Exit the current process, if desktop attached with no process, default desktop will be activated.
                }
            }
        }

        public static bool NeedBootStrapping()
        {
            Console.WriteLine((GetDesktopName() != DesktopName).ToString());
            return (GetDesktopName() != DesktopName);
        }

        static string GetDesktopName()
        {
            int DLength = 0, DHandle = GetThreadDesktop(GetCurrentThreadId());
            StringBuilder DName = new StringBuilder();
            GetUserObjectInformation(DHandle, UOI_NAME, DName, 0, ref DLength);
            if (DLength != 0) GetUserObjectInformation(DHandle, UOI_NAME, DName, DLength, ref DLength);
            Console.WriteLine(DName.ToString());
            return (DName.ToString());
        }

        static void CreateNewDesktop()
        {
            DesktopThreadID = GetThreadDesktop(GetCurrentThreadId());
            DesktopInputID = OpenInputDesktop(0, false, DESKTOP_SWITCHDESKTOP);
            DesktopHandle = CreateDesktop(DesktopName, "", 0, 0, GENERIC_ALL, 0);
            if (DesktopHandle != 0)
            {
                SetThreadDesktop(DesktopHandle);
                SwitchDesktop(DesktopHandle);
            }
        }

        public static void DestroyDesktop()
        {
            SwitchDesktop(DesktopInputID);
            DesktopInputID = 0;
            SetThreadDesktop(DesktopInputID);
            DesktopThreadID = 0;
            CloseDesktop(DesktopHandle);
            DesktopHandle = 0;
        }

        static void StartProcess(string Path)
        {
            MessageBox.Show("Hello from startProcess");
            DestroyDesktop();
        }

        static void SetProcessPriorityHigh()
        {
            SetThreadPriority(GetCurrentThread(), THREAD_BASE_PRIORITY_MAX);
            SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
        }
    }
}

And the main() function:

static void Main()
        {
            if (CLocker.NeedBootStrapping())
                CLocker.Enabled = true;        // Check if we need boot strapping or not, if true then a new desktop will created.
            else  // Run application as usual.
            {
                MessageBox.Show("Hello, this is your new desktop");
                CLocker.Enabled = false;
            }
        }

Update: This code does not compile. It comes up with about 40 red squiggly lines underneath the words saying "Does not exists in current context".

+1  A: 

Dont pop a message box, which requires user input to clear, on a hidden desktop instance. That's usually the down fall of hosting UI code in services as well.

Also, it's a good idea to research any and all unmanaged API calls before just running the code.

Joe Caffeine