2

さて、私はこのクラスをオンラインで見つけました。これは、何も実行しない2番目のデスクトップを「作成」します(つまり、explorer.exeは呼び出されませんなど)。

ただし、この新しく作成されたデスクトップは、シャットダウンして元のデスクトップに戻ることを拒否します。何が起こっているのかわかりません。ですから、誰かが自分のマシンでそれを試すことができれば、それは非常に役に立ちます。

注:すべてのwin apiヘッダーが宣言され、機能していると想定します。

現在のデスクトップを「ロック」するクラス:

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()関数:

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本の赤い波線が表示されます。

4

1 に答える 1

1

非表示のデスクトップ インスタンスで、クリアするためにユーザー入力が必要なメッセージ ボックスを表示しないでください。これは通常、サービスで UI コードをホストすることの失敗でもあります。

また、コードを実行する前に、すべてのアンマネージ API 呼び出しを調査することをお勧めします。

于 2009-08-22T03:23:45.077 に答える