windows .NET 模拟 Ctrl+Alt+Del 发送键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5047344/
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
.NET Simulate Ctrl+Alt+Del Sendkeys
提问by GianT971
all is said in the title, how can I simulate the combination Ctrl+Alt+DEL?
所有在标题说,我怎么可以模拟组合Ctrl+ Alt+ DEL?
I tried this:
我试过这个:
SendKeys.Send("^(%({DEL}))")
SendKeys.Send("^(%{DEL})")
SendKeys.Send("^%{DEL}")
But none worked. I am working on VB.NET and Windows XP SP3
但没有一个奏效。我正在使用 VB.NET 和 Windows XP SP3
采纳答案by GianT971
I finally found this C++ codeon CodeProject, which works well when launched as System user. Therefore, I converted the code into a dll, and called the function from my code.
我终于在 CodeProject 上找到了这个 C++ 代码,它在以System user启动时运行良好。因此,我将代码转换为 dll,并从我的代码中调用该函数。
Here is the c++ code (you can use the ErrorExit example functionthat uses GetLastError
from MSDN in case a problem occured):
这是 C++ 代码(如果出现问题,您可以使用MSDN 中使用的ErrorExit 示例函数GetLastError
):
#include "windows.h"
#include <strsafe.h>
__declspec(dllexport) BOOL SimulateAltControlDel()
{
HDESK hdeskCurrent;
HDESK hdesk;
HWINSTA hwinstaCurrent;
HWINSTA hwinsta;
//
// Save the current Window station
//
hwinstaCurrent = GetProcessWindowStation();
if (hwinstaCurrent == NULL)
return FALSE;
//
// Save the current desktop
//
hdeskCurrent = GetThreadDesktop(GetCurrentThreadId());
if (hdeskCurrent == NULL)
return FALSE;
//
// Obtain a handle to WinSta0 - service must be running
// in the LocalSystem account
//
hwinsta = OpenWindowStation("winsta0", FALSE,
WINSTA_ACCESSCLIPBOARD |
WINSTA_ACCESSGLOBALATOMS |
WINSTA_CREATEDESKTOP |
WINSTA_ENUMDESKTOPS |
WINSTA_ENUMERATE |
WINSTA_EXITWINDOWS |
WINSTA_READATTRIBUTES |
WINSTA_READSCREEN |
WINSTA_WRITEATTRIBUTES);
if (hwinsta == NULL)
return FALSE;
//
// Set the windowstation to be winsta0
//
if (!SetProcessWindowStation(hwinsta))
return FALSE;
//
// Get the default desktop on winsta0
//
hdesk = OpenDesktop("Winlogon", 0, FALSE,
DESKTOP_CREATEMENU |
DESKTOP_CREATEWINDOW |
DESKTOP_ENUMERATE |
DESKTOP_HOOKCONTROL |
DESKTOP_JOURNALPLAYBACK |
DESKTOP_JOURNALRECORD |
DESKTOP_READOBJECTS |
DESKTOP_SWITCHDESKTOP |
DESKTOP_WRITEOBJECTS);
if (hdesk == NULL)
return FALSE;
//
// Set the desktop to be "default"
//
if (!SetThreadDesktop(hdesk))
return FALSE;
PostMessage(HWND_BROADCAST,WM_HOTKEY,0,MAKELPARAM(MOD_ALT|MOD_CONTROL,VK_DELETE));
//
// Reset the Window station and desktop
//
if (!SetProcessWindowStation(hwinstaCurrent))
return FALSE;
if (!SetThreadDesktop(hdeskCurrent))
return FALSE;
//
// Close the windowstation and desktop handles
//
if (!CloseWindowStation(hwinsta))
return FALSE;
if (!CloseDesktop(hdesk))
return FALSE;
return TRUE;
}
You also need to add a .def file to the project to export the function correctly (the project is named AltCtrlDelCpp) and tell the linker that the definition file of the module is this file
还需要在项目中添加一个.def文件才能正确导出函数(项目名为AltCtrlDelCpp)并告诉链接器模块的定义文件就是这个文件
;altctrldel.def
LIBRARY AltCtrlDelCpp
;CODE PRELOAD MOVEABLE DISCARDABLE
;DATA PRELOAD MOVEABLE
EXPORTS
SimulateAltControlDel
Then, in the .NET solution, you add the dll to the project, configures it so that it is always copied in the output directory, and you just use DllImport to import the function:
然后,在 .NET 解决方案中,您将 dll 添加到项目中,对其进行配置,使其始终复制到输出目录中,您只需使用 DllImport 导入函数:
C# Code
C# 代码
[DllImport(@"AltCtrlDelCpp.dll")]
static extern bool SimulateAltControlDel();
VB.NET Code
VB.NET 代码
<DllImport("AltCtrlDelCpp.dll")> _
Private Function SimulateAltControlDel() As Boolean
In VB.NET, you also need to add the attribute to the Sub Main :
在 VB.NET 中,您还需要将属性添加到 Sub Main :
<MTAThread()> _
Sub Main()
Then you just have to call the SimulateAltControlDel
function and there you go. Please note that I had this work only for a Console Apps, it didn't work in winform apps.
然后你只需要调用这个SimulateAltControlDel
函数就可以了。请注意,我只对Console Apps进行了这项工作,它在 winform 应用程序中不起作用。
回答by Hans Passant
You can't. This is done at the device driver level, you can't fake input for the keyboard driver. Also the reason you cannot disable it. Allowing it to be faked would of course be a very serious security flaw.
你不能。这是在设备驱动程序级别完成的,您不能伪造键盘驱动程序的输入。这也是您无法禁用它的原因。允许它被伪造当然是一个非常严重的安全漏洞。
回答by David Heffernan
As of Windows Vista, you can use the SendSAS
function.
从 Windows Vista 开始,您可以使用该SendSAS
功能。
Original answer, now superseded by the above
原来的答案,现在被上面的取代
The function you need is called SimulateSAS
. You need to e-mail [email protected] and ask for it. Microsoft don't appear to document this, but just do a websearch for SimulateSAS
and you'll see what I mean.
您需要的函数称为SimulateSAS
。您需要发送电子邮件至 [email protected] 并索取。微软似乎没有记录这一点,但只要进行网络搜索SimulateSAS
,你就会明白我的意思。
Others have explained why it's actually not a security issue to allow apps to trigger CTRL+ALT+DEL, but you certainly can't do it with SendKeys
.
其他人则解释了为什么它实际上不是一个安全问题,使应用程序来触发CTRL+ ALT+ DEL,但你肯定无法做到这一点SendKeys
。
回答by Robert Harvey
Your best bet might be to download the TightVNC source code,and see how they do it.
最好的办法可能是下载TightVNC 源代码,看看他们是怎么做的。
回答by Lasse V. Karlsen
See this thread for some information that seems useful:
请参阅此线程以获取一些似乎有用的信息:
Basically:
基本上:
- Your program must be signed
- Your program must have a manifest specifying the privileges needed
- Your program must be located in a protected folder (one that requires UAC for writing to, like the Program Files folder)
Your program can then use the following undocumented API to invoke it:
DWORD dwRet = lpfnWmsgSendMessage(dwSessionId,0x208, 0, (LPARAM)&lParam); //Undocument API.
- 您的程序必须签名
- 您的程序必须有一个清单,指定所需的权限
- 您的程序必须位于受保护的文件夹中(需要 UAC 写入的文件夹,例如 Program Files 文件夹)
然后,您的程序可以使用以下未公开的 API 来调用它:
DWORD dwRet = lpfnWmsgSendMessage(dwSessionId,0x208, 0, (LPARAM)&lParam); //Undocument API.
Note, I only distilled the web page I link to, I have no idea if it works, or if there are more gotchas.
请注意,我只提炼了我链接到的网页,我不知道它是否有效,或者是否有更多问题。