如何以编程方式锁定Windows工作站?

可能重复:
在C#中以编程方式锁定Windows工作站

我目前正在开发一个visual studio windows窗体应用程序,它需要一个锁定工作站的function。 在调用函数时,如何使用user32.dll进行锁定(Windows + L)?

我自己没有尝试过,但我在谷歌上发现了这一点

Process.Start(@"C:\WINDOWS\system32\rundll32.exe", "user32.dll,LockWorkStation");

编辑:我试过了,它的确有效!

edit2:这是一个使用user32.dll但不启动外部进程的解决方案 。

using System.Runtime.InteropServices;

声明一个这样的方法:

 [DllImport("user32.dll")] public static extern bool LockWorkStation(); 

然后调用LockWorkStation();

 using System.Runtime.InteropServices; [DllImport("user32.dll")] public static extern int ExitWindowsEx(int uFlags, int dwReserved); 

只是打电话

  ExitWindowsEx(0, 0);