在 C# 中以编程方式锁定 Windows 工作站

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1263047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:58:27  来源:igfitidea点击:

Lock Windows workstation programmatically in C#

c#windowspinvokepure-managed

提问by Ron Klein

I ran into this example for locking Windows workstation:

我遇到了这个锁定 Windows 工作站的例子:

using System.Runtime.InteropServices;
...
[DllImport("user32.dll", SetLastError = true)]
static extern bool LockWorkStation();

...
if (!LockWorkStation())
    throw new Win32Exception(Marshal.GetLastWin32Error()); // or any other thing

Is there a pure managed alternative to this snippet? Namely, without P-Invoke.

是否有此代码段的纯托管替代方案?即,没有 P-Invoke。

回答by JaredPar

No there is not. This is the best way to achieve this action.

不,那里没有。这是实现此操作的最佳方式。

Even if it was provided in the BCL, its implementation would almost certainly be identical to your sample. It's not something the CLR would natively implement.

即使它在 BCL 中提供,它的实现也几乎肯定与您的示例相同。这不是 CLR 本身会实现的。