2009-02-04 27 views
2

好吧,所以我在網上發現這個類「創建」了第二個不運行任何東西的桌面(即explorer.exe沒有被調用等等)。win api和c# - 桌面

但是,這個新創建的桌面拒絕關閉並返回到原始桌面。我不知道發生了什麼事。所以如果有人可以在他們的機器上試用它,這將是非常有幫助的。

注意:假設所有win api標頭已被聲明並正常工作。

類是 「鎖定」 當前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); 
     } 
    } 
} 

而且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條紅色波浪線,在「當前語境中不存在」的詞語之下。

+1

它搞砸了你的機器,所以現在你想讓我們其中一個人嘗試它,並搞砸我們的機器呢? – 2009-02-04 01:55:41

回答

1

不要在隱藏的桌面實例上彈出消息框,它需要用戶輸入以清除。這通常是服務中託管UI代碼的下降。

此外,在運行代碼之前研究任何和所有非託管API調用是個好主意。