Gia*_*971 7 .net c# vb.net windows keyboard
所有都在标题中说,我怎样才能模拟组合Ctrl+ Alt+ DEL?
我试过这个:
SendKeys.Send("^(%({DEL}))")
SendKeys.Send("^(%{DEL})")
SendKeys.Send("^%{DEL}")
Run Code Online (Sandbox Code Playgroud)
但都没有效果.我正在研究VB.NET和Windows XP SP3
你不能.这是在设备驱动程序级别完成的,您无法伪造键盘驱动程序的输入.也是你无法禁用它的原因.允许它被伪造当然是一个非常严重的安全漏洞.
从Windows Vista开始,您可以使用该SendSAS功能.
原始答案,现在已被上述所取代
您需要的功能被调用SimulateSAS.您需要发送电子邮件至saslib@microsoft.com并要求它.微软似乎没有记录这个,但只是做一个网络搜索SimulateSAS,你会明白我的意思.
其他人已经解释了为什么允许应用程序触发CTRL+ ALT+ 实际上不是一个安全问题DEL,但你肯定无法做到这一点SendKeys.
我终于在 CodeProject 上找到了这段 C++ 代码,当以System user身份启动时效果很好。因此,我将代码转换为 dll,并从我的代码中调用该函数。
下面是 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;
}
Run Code Online (Sandbox Code Playgroud)
还需要在工程中添加一个.def文件才能正确导出函数(工程名为AltCtrlDelCpp)并告诉链接器该模块的定义文件就是这个文件
;altctrldel.def
LIBRARY AltCtrlDelCpp
;CODE PRELOAD MOVEABLE DISCARDABLE
;DATA PRELOAD MOVEABLE
EXPORTS
SimulateAltControlDel
Run Code Online (Sandbox Code Playgroud)
然后,在 .NET 解决方案中,将 dll 添加到项目中,对其进行配置,以便始终将其复制到输出目录中,然后只需使用 DllImport 导入该函数:
C# 代码
[DllImport(@"AltCtrlDelCpp.dll")]
static extern bool SimulateAltControlDel();
Run Code Online (Sandbox Code Playgroud)
VB.NET代码
<DllImport("AltCtrlDelCpp.dll")> _
Private Function SimulateAltControlDel() As Boolean
Run Code Online (Sandbox Code Playgroud)
在 VB.NET 中,您还需要将该属性添加到 Sub Main :
<MTAThread()> _
Sub Main()
Run Code Online (Sandbox Code Playgroud)
然后你只需要调用该SimulateAltControlDel函数就可以了。请注意,我仅适用于Console Apps,它不适用于 winform 应用程序。
| 归档时间: |
|
| 查看次数: |
11812 次 |
| 最近记录: |