2011-02-18 206 views
5

所有的標題是說,我怎麼可以模擬組合按Ctrl + Alt鍵+ DEL .NET模擬按Ctrl + Alt + Del鍵的SendKeys

我嘗試這樣做:

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

但沒有奏效。我正在使用VB.NET和Windows XP SP3

+2

@mdmullinax:從技術上講,沒有。他問的是如何模擬鍵,而不是陷入困境。 – mellamokb 2011-02-18 22:56:44

+0

我的壞,評論刪除 – MikeM 2011-02-18 23:37:45

+0

你想實際模擬的關鍵,或者你只是想調用標準的安全對話框?如果是後者,則可以調用[WindowsSecurity`方法](http://msdn.microsoft.com/zh-cn/library/windows/desktop/bb774126%28v=vs.85%29.aspx) shell調度對象。 – 2012-03-06 14:18:23

回答

1

我終於在CodeProject上找到了this C++ code,它在系統用戶上啓動時效果很好。因此,我將代碼轉換爲一個dll,並從我的代碼中調用該函數。

這裏是C++代碼(可以使用ErrorExit例如功能,在案件的問題發生使用GetLastError從MSDN):

#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

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

5

從Windows Vista開始,您可以使用SendSAS函數。


原來的答覆,現在你需要以上

功能取代被稱爲SimulateSAS。您需要通過電子郵件[email protected]詢問。微軟似乎沒有記錄這一點,但只是做一個SimulateSAS的網絡搜索,你會明白我的意思。

其他人解釋爲什麼它實際上不是一個安全問題,使應用程序觸發CTRL + ALT + DEL ,但你肯定不能SendKeys做到這一點。

2

看到這個線程的一些信息似乎有用:

基本上是:

  • 你的程序必須簽署
  • 你的程序必須有一個清單指定所需特權
  • 你的程序必須位於一個受保護的文件夾(需要UAC進行寫入,像Program Files文件夾)
  • 你的程序然後可以使用下面的未公開的API來調用它:

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

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

1

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