首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >win api和c# -桌面

win api和c# -桌面
EN

Stack Overflow用户
提问于 2009-02-04 01:17:04
回答 1查看 2.3K关注 0票数 2

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

然而,这个新创建的桌面拒绝关闭并返回原始桌面。我不知道怎么回事。因此,如果有人可以在他们的机器上试用,这将是非常有帮助的。

注意:假设所有win api头都已声明并工作。

“锁定”当前dekstop的类:

代码语言:javascript
复制
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);
        }
    }
}

和main()函数:

代码语言:javascript
复制
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条红色的线条。

EN

回答 1

Stack Overflow用户

发布于 2009-08-22 03:23:45

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

此外,在运行代码之前研究任何和所有非托管API调用都是一个好主意。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/509733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档