English 中文(简体)
Win API和C# - 桌面应用程序
原标题:
  • 时间:2009-02-04 01:17:04
  •  标签:

好的,我在网上找到了这个课程,它可以“创建”一个第二个桌面,不运行任何东西(即,不调用explorer.exe等)。

然而,这个新创建的桌面拒绝关闭并返回到原始桌面。我不知道发生了什么。因此,如果有人能在他们的机器上尝试一下,那将非常有帮助。

请注意:假设所有咏叹调 winapi 已被声明并且可用。

“锁定”当前桌面的班级:

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

主函数:

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

更新: 这段代码不能编译。在那些单词下面出现了大约40条红色的波浪线,表示“不存在于当前的上下文中”。

问题回答

不要在隐藏的桌面实例上弹出需要用户输入才能清除的消息框。这通常也是将UI代码托管在服务中的失败之处。

此外,运行代码之前,研究所有未经管理的 API 调用是一个好主意。





相关问题
热门标签