简体   繁体   English

.NET模拟Ctrl + Alt + Del Sendkeys

[英].NET Simulate Ctrl+Alt+Del Sendkeys

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

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. 允许它被伪造当然是一个非常严重的安全漏洞。

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 . 您需要的功能称为SimulateSAS You need to e-mail saslib@microsoft.com and ask for it. 您需要发送电子邮件至saslib@microsoft.com并要求它。 Microsoft don't appear to document this, but just do a websearch for SimulateSAS and you'll see what I mean. 微软似乎没有记录这个,但只是为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来做。

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

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) 您的程序必须位于受保护的文件夹中(需要UAC才能写入,如Program Files文件夹)
  • Your program can then use the following undocumented API to invoke it: 然后,您的程序可以使用以下未记录的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. 请注意,我只提炼了我链接到的网页,我不知道它是否有效,或者是否有更多的问题。

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

I finally found this C++ code on CodeProject, which works well when launched as System user . 我终于在CodeProject上找到了这个C ++代码 ,它在以System用户身份启动时运行良好。 Therefore, I converted the code into a dll, and called the function from my code. 因此,我将代码转换为dll,并从我的代码中调用该函数。

Here is the c++ code (you can use the ErrorExit example function that uses GetLastError from MSDN in case a problem occured): 这是c ++代码(您可以使用ErrorExit示例函数 ,在发生问题时使用来自MSDN的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. 然后你只需要调用SimulateAltControlDel函数就可以了。 Please note that I had this work only for a Console Apps , it didn't work in winform apps. 请注意,我的这项工作仅适用于控制台应用程序 ,但它在winform应用程序中无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM