gpt4 book ai didi

c# - win api 和 c# - 桌面

转载 作者:太空宇宙 更新时间:2023-11-03 14:37:17 27 4
gpt4 key购买 nike

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

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

注意:假设所有 win api header 都已声明并有效。

“锁定”当前桌面的类:

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 条红色波浪线。

最佳答案

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

此外,最好在运行代码之前研究所有非托管 API 调用。

关于c# - win api 和 c# - 桌面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/509733/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com