Search code examples
windowsvisual-c++sendinput

Send Ctrl Alt Del through INPUT Structure doesn't work?


How to simulate this ctrl alt del so that it will work ?

My code is as follows:

INPUT Input; /* Generate a "key down" */

Input.type  = INPUT_KEYBOARD;
Input.ki.dwFlags  = KEYEVENTF_SCANCODE; 
Input.ki.wScan=29;
Input.ki.dwExtraInfo=0;
Input.ki.wVk=0;
Input.ki.time=0;
SendInput(1, &Input, sizeof(Input));


Input.type  = INPUT_KEYBOARD;
Input.ki.dwFlags  = KEYEVENTF_SCANCODE; 
Input.ki.wScan=56;
Input.ki.dwExtraInfo=0;
Input.ki.wVk=0;
Input.ki.time=0;
SendInput(1, &Input, sizeof(Input));


Input.type  = INPUT_KEYBOARD;
Input.ki.dwFlags  = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_SCANCODE; 
Input.ki.wScan=83;
Input.ki.dwExtraInfo=0;
Input.ki.wVk=0;
Input.ki.time=0;
SendInput(1, &Input, sizeof(Input));


Input.type  = INPUT_KEYBOARD;
Input.ki.dwFlags  = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; 
Input.ki.wScan=29;
Input.ki.dwExtraInfo=0;
Input.ki.wVk=0;
Input.ki.time=0;
SendInput(1, &Input, sizeof(Input));


Input.type  = INPUT_KEYBOARD;
Input.ki.dwFlags  = KEYEVENTF_SCANCODE  | KEYEVENTF_KEYUP; 
Input.ki.wScan=56;
Input.ki.dwExtraInfo=0;
Input.ki.wVk=0;
Input.ki.time=0;
SendInput(1, &Input, sizeof(Input));


Input.type  = INPUT_KEYBOARD;
Input.ki.dwFlags  = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_SCANCODE  | KEYEVENTF_KEYUP; 
Input.ki.wScan=83;
Input.ki.dwExtraInfo=0;
Input.ki.wVk=0;
Input.ki.time=0;
SendInput(1, &Input, sizeof(Input));

Solution

  • For Windows XP, since SendSAS is not available:

    #define _WIN32_WINNT 0x0500
    
    #include <windows.h>
    #include <winwlx.h>
    
    #include <stdio.h>
    
    BOOL CALLBACK parents(HWND hwnd, LPARAM dummy);
    
    HWND saswindow = NULL;
    
    int main(int argc, char ** argv) {
    
      HDESK h;
    
      HWINSTA hw;
    
      DWORD err;
    
      hw = OpenWindowStation("winsta0", FALSE, GENERIC_ALL);
    
      if (!hw) {
    
        printf("Error %u calling OpenWindowStation.\n", GetLastError());
    
        return 1;
    
      }
    
      if (!SetProcessWindowStation(hw)) {
    
        printf("Error %u calling SetProcessWindowStation.\n", GetLastError());
    
        return 1;
    
      }
    
      h = OpenDesktop("Winlogon", 0, FALSE, GENERIC_ALL);
    
      if (!h) {
    
        printf("Error %u calling OpenDesktop.\n", GetLastError());
    
        return 1;
    
      }
    
      if (!EnumDesktopWindows(h, parents, 0)) {
    
        err = GetLastError();
    
        if (err != 0) {
    
          printf("Error %u enumerating top-level windows.\n", err);
    
          return 1;
    
        }
    
      }
    
      if (saswindow == NULL) {
    
        printf("SAS window not found.\n");
    
        return 1;
    
      }
    
      if (!PostMessage(saswindow, WLX_WM_SAS, WLX_SAS_TYPE_CTRL_ALT_DEL, 0)) {
    
        printf("Error %u posting message.\n", GetLastError());
    
        return 1;
    
      }
    
      return 0;
    
    }
    
    BOOL CALLBACK parents(HWND hwnd, LPARAM dummy) {  
    
      static int n;
    
      static char wintext[16];
    
      n = GetWindowText(hwnd, wintext, sizeof(wintext));
    
      if (n == 0) return TRUE;
    
      if (strcmp(wintext, "SAS window") != 0) return TRUE;
    
      saswindow = hwnd;
    
      SetLastError(0);
    
      return FALSE;
    
    }