.NET 模拟 Ctrl+Alt+Del Sendkeys

发布于 2024-10-18 06:33:04 字数 235 浏览 4 评论 0原文

标题中已经说了,如何模拟组合Ctrl+Alt+DEL

我尝试过这个:

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

但没有成功。我正在使用 VB.NET 和 Windows XP SP3

all is said in the title, how can I simulate the combination 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

恍梦境° 2024-10-25 06:33:04

从 Windows Vista 开始,您可以使用 SendSAS函数。


原始答案,现已被上述答案取代

您需要的函数称为SimulateSAS。您需要发送电子邮件至 [email protected] 并索取。 Microsoft 似乎没有记录这一点,但只是对 SimulateSAS 进行网络搜索 你就会明白我的意思了。

其他人已经解释了为什么允许应用程序触发 CTRL+ALT+DEL 实际上不是安全问题,但你肯定不能这样做使用SendKeys

As of Windows Vista, you can use the SendSAS function.


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.

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.

一场信仰旅途 2024-10-25 06:33:04

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

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.

划一舟意中人 2024-10-25 06:33:04

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

Your best bet might be to download the TightVNC source code, and see how they do it.

岁月打碎记忆 2024-10-25 06:33:04

请参阅此线程以获取一些似乎有用的信息:

基本上:

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

    DWORD dwRet = lpfnWmsgSendMessage(dwSessionId,0x208, 0, (LPARAM)&lParam); //取消API文档。
    

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

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.
    

Note, I only distilled the web page I link to, I have no idea if it works, or if there are more gotchas.

风情万种。 2024-10-25 06:33:04

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

As of Windows Vista, the SendSAS function is available.

终止放荡 2024-10-25 06:33:04

我终于找到了 此 C++ 代码,以系统用户身份启动时效果很好。因此,我将代码转换为 dll,并从我的代码中调用该函数。

下面是 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;
}

您还需要将 .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 应用程序。

I finally found this C++ code on CodeProject, which works well when launched as System user. Therefore, I converted the code into a dll, and called the function from my code.

Here is the c++ code (you can use the ErrorExit example function that uses GetLastError from MSDN in case a problem occured):

#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

;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:

C# Code

[DllImport(@"AltCtrlDelCpp.dll")]
static extern bool SimulateAltControlDel();

VB.NET Code

<DllImport("AltCtrlDelCpp.dll")> _
Private Function SimulateAltControlDel() As Boolean

In VB.NET, you also need to add the attribute to the 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文