.NET模拟Ctrl + Alt + Del Sendkeys

时间:2011-02-18 22:43:31

标签: c# .net windows vb.net keyboard

所有内容都在标题中说明,如何模拟组合 Ctrl + Alt + DEL

我试过了:

SendKeys.Send("^(%({DEL}))")
SendKeys.Send("^(%{DEL})") 
SendKeys.Send("^%{DEL}")

但都没有效果。我正在研究VB.NET和Windows XP SP3

6 个答案:

答案 0 :(得分:5)

你做不到。这是在设备驱动程序级别完成的,您无法伪造键盘驱动程序的输入。也是你无法禁用它的原因。允许它被伪造当然是一个非常严重的安全漏洞。

答案 1 :(得分:5)

从Windows Vista开始,您可以使用SendSAS功能。


原始答案,现已被上述

取代

您需要的功能称为SimulateSAS。您需要发送电子邮件至saslib@microsoft.com并要求它。微软似乎没有记录这一点,只是为SimulateSAS做一个网络搜索,你会明白我的意思。

其他人已经解释了为什么允许应用触发 CTRL + ALT + DEL 实际上不是安全问题,但你当然可以'用SendKeys来完成。

答案 2 :(得分:4)

您最好的选择可能是下载TightVNC source code,,看看他们是如何做到的。

答案 3 :(得分:3)

请参阅此主题以获取一些看似有用的信息:

基本上:

  • 您的计划必须签名
  • 您的程序必须具有指定所需权限的清单
  • 您的程序必须位于受保护的文件夹中(需要UAC才能写入,如Program Files文件夹)
  • 然后,您的程序可以使用以下未记录的API来调用它:

    DWORD dwRet = lpfnWmsgSendMessage(dwSessionId,0x208, 0, (LPARAM)&lParam); //Undocument API.
    

注意,我只提炼了我链接到的网页,我不知道它是否有效,或者是否有更多的问题。

答案 4 :(得分:1)

我终于在CodeProject上找到了this C++ code,当它作为系统用户启动时效果很好。因此,我将代码转换为dll,并从我的代码中调用该函数。

这是c ++代码(如果出现问题,您可以使用MSDN中使用GetLastError ErrorExit示例函数):

#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应用中无效。

答案 5 :(得分:1)

自Windows Vista起,SendSAS功能可用。