簡體   English   中英

.NET模擬Ctrl + Alt + Del Sendkeys

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

所有都在標題中說,我怎樣才能模擬組合Ctrl + Alt + DEL

我試過這個:

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

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

你不能。 這是在設備驅動程序級別完成的,您無法偽造鍵盤驅動程序的輸入。 也是你無法禁用它的原因。 允許它被偽造當然是一個非常嚴重的安全漏洞。

從Windows Vista開始,您可以使用SendSAS功能。


原始答案,現在已被上述所取代

您需要的功能稱為SimulateSAS 您需要發送電子郵件至saslib@microsoft.com並要求它。 微軟似乎沒有記錄這個,但只是為SimulateSAS做一個網絡搜索,你會明白我的意思。

其他人已經解釋了為什么允許應用程序觸發CTRL + ALT + DEL實際上不是安全問題,但你當然不能用SendKeys來做。

您最好的選擇可能是下載TightVNC源代碼,看看他們是如何做到的。

請參閱此主題以獲取一些看似有用的信息:

基本上:

  • 您的計划必須簽名
  • 您的程序必須具有指定所需權限的清單
  • 您的程序必須位於受保護的文件夾中(需要UAC才能寫入,如Program Files文件夾)
  • 然后,您的程序可以使用以下未記錄的API來調用它:

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

請注意,我只提煉了我鏈接到的網頁,我不知道它是否有效,或者是否有更多的問題。

從Windows Vista開始, SendSAS功能可用。

我終於在CodeProject上找到了這個C ++代碼 ,它在以System用戶身份啟動時運行良好。 因此,我將代碼轉換為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應用程序中無效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM