.NET模拟按Ctrl + Alt + Del键的SendKeys
所有的标题是说,我怎么可以模拟组合按Ctrl + Alt键+ DEL ?.NET模拟按Ctrl + Alt + Del键的SendKeys
我尝试这样做:
SendKeys.Send("^(%({DEL}))")
SendKeys.Send("^(%{DEL})")
SendKeys.Send("^%{DEL}")
但没有奏效。我正在使用VB.NET和Windows XP SP3
我终于在CodeProject上找到了this C++ code,它在系统用户上启动时效果很好。因此,我将代码转换为一个dll,并从我的代码中调用该函数。
这里是C++代码(可以使用ErrorExit例如功能,在案件的问题发生使用GetLastError
从MSDN):
#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;
}
你还需要一个DEF文件添加到项目要正确导出函数(该项目被命名为AltCtrlDelCpp),并告诉链接该模块的定义文件,这个文件
;altctrldel.def
LIBRARY AltCtrlDelCpp
;CODE PRELOAD MOVEABLE DISCARDABLE
;DATA PRELOAD MOVEABLE
EXPORTS
SimulateAltControlDel
然后,在.NET解决方案,您将DLL添加到项目中,对其进行配置,使其在输出目录会复制,你只需要使用的DllImport导入功能:
C#代码
[DllImport(@"AltCtrlDelCpp.dll")]
static extern bool SimulateAltControlDel();
VB.NET代码
<DllImport("AltCtrlDelCpp.dll")> _
Private Function SimulateAltControlDel() As Boolean
在VB.NET中,您还需要将属性添加到Sub Main中:
<MTAThread()> _
Sub Main()
然后,你只需要拨打SimulateAltControlDel
函数,你去那里。请注意,我仅在控制台应用程序上有此项工作,但它在winform应用程序中不起作用。
你不能。这是在设备驱动程序级完成的,不能为键盘驱动程序伪造输入。也是你无法禁用它的原因。允许它伪造当然是一个非常严重的安全漏洞。
从Windows Vista开始,您可以使用SendSAS
函数。
原来的答复,现在你需要以上
功能取代被称为SimulateSAS
。您需要通过电子邮件[email protected]询问。微软似乎没有记录这一点,但只是做一个SimulateSAS
的网络搜索,你会明白我的意思。
其他人解释为什么它实际上不是一个安全问题,使应用程序触发CTRL + ALT + DEL ,但你肯定不能SendKeys
做到这一点。
看到这个线程的一些信息似乎有用:
基本上是:
- 你的程序必须签署
- 你的程序必须有一个清单指定所需特权
- 你的程序必须位于一个受保护的文件夹(需要UAC进行写入,像Program Files文件夹)
-
你的程序然后可以使用下面的未公开的API来调用它:
DWORD dwRet = lpfnWmsgSendMessage(dwSessionId,0x208, 0, (LPARAM)&lParam); //Undocument API.
请注意,我只提取了我链接到的网页,我不知道它是否有效,或者有更多的疑难问题。
@mdmullinax:从技术上讲,没有。他问的是如何模拟键,而不是陷入困境。 – mellamokb 2011-02-18 22:56:44
我的坏,评论删除 – MikeM 2011-02-18 23:37:45
你想实际模拟的关键,或者你只是想调用标准的安全对话框?如果是后者,则可以调用[WindowsSecurity`方法](http://msdn.microsoft.com/zh-cn/library/windows/desktop/bb774126%28v=vs.85%29.aspx) shell调度对象。 – 2012-03-06 14:18:23