CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 16

Hybrid View

  1. #1
    Join Date
    Feb 2005
    Posts
    3

    Talking Ctrl-alt-delete....

    Why doesn't this code bring up the Windows Security Window?????
    =============================================


    ______________________________________________________________
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    Private Const KEYEVENTF_KEYUP = &H2

    ______________________________________________________________

    Private Sub Form_Load()
    keybd_event vbKeyControl, 0, 0, 0
    keybd_event vbKeyAlt, 0, 0, 0
    keybd_event vbKeyDelete, 0, 0, 0
    DoEvents

    keybd_event vbKeyControl, 0, KEYEVENTF_KEYUP, 0
    keybd_event vbKeyAlt, 0, KEYEVENTF_KEYUP, 0
    keybd_event vbKeyDelete, 0, KEYEVENTF_KEYUP, 0
    DoEvents
    End Sub
    _______________________________________________________________

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Ctrl-alt-delete....

    Try using:

    keybd_event VK_CONTROL, 0, 0, 0
    keybd_event VK_ALT, 0, 0, 0
    keybd_event VK_DELETE, 0, 0, 0
    DoEvents

    etc..

    Or try using SendKey

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Ctrl-alt-delete....

    Do believe it is not that easy: the following applied to sendKeys, but I am
    quite sure same principles are under your code...
    http://techsupt.winbatch.com/TS/T000001024F12.html

    By the way: the const for those keys are:
    Code:
    Const VK_MENU As Long = &H12    'This is Alt
    Const VK_CONTROL As Long = &H11
    Const VK_DELETE As Long = &H2E
    And also Cjard and VMA said something on this - you could see if Vma link to
    C code can help you :
    http://www.codeguru.com/forum/showth...t=ctrl+alt+del
    Last edited by Cimperiali; March 1st, 2005 at 04:37 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646

    Re: Ctrl-alt-delete....

    I've spent the entire evening porting VNC code to VB, and searching for resources in the internet, but seems that I stuck on OpenDesktop API.
    In VNC they simulate the CTRL ALT DEL combination by posting WM_HOTKEY message in the "Winlogon" desktop.

    This desktop is a special secure desktop, I kept getting access denied error when trying to open this desktop (perhaps it's been being restricted in WinXP SP1).

    Furthermore to successfully call this function the thread calling this function must not have a GUI opened, so we must call the function in a new different thread.

    Anyway here's the code to VNC way of sending CTRL ALT DEL (not that they work, but just in case anyone interested)

    Code:
    Option Explicit
    
    Private Const HWND_BROADCAST As Long = &HFFFF&
    Private Const WM_HOTKEY As Long = &H312
    Private Const MOD_ALT As Long = &H1
    Private Const MOD_CONTROL As Long = &H2
    Private Const VK_DELETE As Long = &H2E
    
    Private Const DESKTOP_CREATEMENU As Long = &H4&
    Private Const DESKTOP_CREATEWINDOW As Long = &H2&
    Private Const DESKTOP_ENUMERATE As Long = &H40&
    Private Const DESKTOP_HOOKCONTROL As Long = &H8&
    Private Const DESKTOP_WRITEOBJECTS As Long = &H80&
    Private Const DESKTOP_READOBJECTS As Long = &H1&
    Private Const DESKTOP_SWITCHDESKTOP As Long = &H100&
    Private Const DESKTOP_JOURNALPLAYBACK As Long = &H20&
    Private Const DESKTOP_JOURNALRECORD As Long = &H10&
    
    Private Const GENERIC_WRITE As Long = &H40000000
    
    Private Const WINSTA_ACCESSCLIPBOARD As Long = &H4&
    Private Const WINSTA_ACCESSGLOBALATOMS As Long = &H20&
    Private Const WINSTA_ACCESSPUBLICATOMS As Long = &H20&
    Private Const WINSTA_CREATEDESKTOP As Long = &H8&
    Private Const WINSTA_ENUMDESKTOPS As Long = &H1&
    Private Const WINSTA_ENUMERATE As Long = &H100&
    Private Const WINSTA_EXITWINDOWS As Long = &H40&
    Private Const WINSTA_READATTRIBUTES As Long = &H2&
    Private Const WINSTA_READSCREEN As Long = &H200&
    Private Const WINSTA_WRITEATTRIBUTES As Long = &H10&
    
    Private Const MAXIMUM_ALLOWED As Long = &H2000000
    
    Private Const UOI_NAME As Long = 2
    
    Private Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Long
    Private Declare Function GetThreadDesktop Lib "user32.dll" (ByVal dwThread As Long) As Long
    Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Declare Function OpenDesktop Lib "user32.dll" Alias "OpenDesktopA" (ByVal lpszDesktop As String, ByVal dwFlags As Long, ByVal fInherit As Boolean, ByVal dwDesiredAccess As Long) As Long
    Private Declare Function OpenInputDesktop Lib "user32.dll" (ByVal dwFlags As Long, ByVal fInherit As Boolean, ByVal dwDesiredAccess As Long) As Long
    Private Declare Function CloseDesktop Lib "user32.dll" (ByVal hDesktop As Long) As Long
    Private Declare Function GetUserObjectInformation Lib "user32.dll" Alias "GetUserObjectInformationA" (ByVal hObj As Long, ByVal nIndex As Long, ByVal pvInfo As String, ByVal nLength As Long, ByRef lpnLengthNeeded As Long) As Long
    Private Declare Function SetThreadDesktop Lib "user32.dll" (ByVal hDesktop As Long) As Long
    Private Declare Function OpenWindowStation Lib "user32.dll" Alias "OpenWindowStationA" (ByVal lpszWinSta As String, ByVal fInherit As Boolean, ByVal dwDesiredAccess As Long) As Long
    Private Declare Function SetProcessWindowStation Lib "user32.dll" (ByVal hWinSta As Long) As Long
    
    
    Public Sub SimulateCtrlAltDelThreadFn()
      Dim old_desktop As Long
      old_desktop = GetThreadDesktop(GetCurrentThreadId())
    
      ' Switch into the Winlogon desktop
      If SelectDesktop("Winlogon") = False Then
        'vnclog.Print(LL_INTERR, VNCLOG("failed to select logon desktop\n"));
        MsgBox "Open Desktop Failed"
        Exit Sub
      End If
    
      'vnclog.Print(LL_ALL, VNCLOG("generating ctrl-alt-del\n"));
    
      ' Fake a hotkey event to any windows we find there.... :(
      ' Winlogon uses hotkeys to trap Ctrl-Alt-Del...
      PostMessage HWND_BROADCAST, WM_HOTKEY, 0, MakeLong(MOD_ALT Or MOD_CONTROL, VK_DELETE)
    
      ' Switch back to our original desktop
      If old_desktop <> 0 Then SelectHDESK old_desktop
    
      
    End Sub
    
    Private Function SelectDesktop(ByVal Name As String) As Boolean
    
      Dim desktop As Long
          
        If Len(Name) > 0 Then
        
          ' Attempt to open the named desktop
          desktop = OpenDesktop(Name, 0, False, _
            DESKTOP_CREATEMENU Or DESKTOP_CREATEWINDOW Or _
            DESKTOP_ENUMERATE Or DESKTOP_HOOKCONTROL Or _
            DESKTOP_WRITEOBJECTS Or DESKTOP_READOBJECTS Or _
            DESKTOP_SWITCHDESKTOP Or GENERIC_WRITE)
          
            Debug.Print Err.LastDllError 'Always gets 5 here (access denied)
        Else
        
          ' No, so open the input desktop
          desktop = OpenInputDesktop(0, False, _
            DESKTOP_CREATEMENU Or DESKTOP_CREATEWINDOW Or _
            DESKTOP_ENUMERATE Or DESKTOP_HOOKCONTROL Or _
            DESKTOP_WRITEOBJECTS Or DESKTOP_READOBJECTS Or _
            DESKTOP_SWITCHDESKTOP Or GENERIC_WRITE)
        
        End If
    
        ' Did we succeed?
        
        If (desktop = 0) Then Exit Function
          'vnclog.Print(LL_INTERR, VNCLOG("unable to open desktop:%d\n"), GetLastError());
          
        ' Switch to the new desktop
        If (SelectHDESK(desktop) = 0) Then
          ' Failed to enter the new desktop, so free it!
          Call CloseDesktop(desktop)
            'vnclog.Print(LL_INTERR, VNCLOG("SelectDesktop failed to close desktop:%d\n"), GetLastError());
          Exit Function
        End If
    
        ' We successfully switched desktops!
        SelectDesktop = True
      
    End Function
    
    Private Function MakeLong(ByVal LoWord As Integer, _
      ByVal HiWord As Integer) As Long
    
      MakeLong = ((HiWord * &H10000) + LoWord)
    
    End Function
    
    Private Function SelectHDESK(new_desktop As Long) As Boolean
        Dim old_desktop As Long
        
        old_desktop = GetThreadDesktop(GetCurrentThreadId())
    
        Dim dummy As Long
        Dim new_name As String '[256];
        
        new_name = String$(256, vbNullChar)
        If (Not GetUserObjectInformation(new_desktop, UOI_NAME, new_name, 256, dummy)) Then
          'Exit Function
        End If
    
        'vnclog.Print(LL_INTERR, VNCLOG("SelectHDESK to %s (%x) from %x\n"), new_name, new_desktop, old_desktop);
    
        ' Switch the desktop
        If (Not SetThreadDesktop(new_desktop)) Then
          'vnclog.Print(LL_INTERR, VNCLOG("unable to SetThreadDesktop\n"), GetLastError());
          'Exit Function
        End If
    
        ' Switched successfully - destroy the old desktop
        If (Not CloseDesktop(old_desktop)) Then
          'vnclog.Print(LL_INTERR, VNCLOG("SelectHDESK failed to close old desktop %x (Err=%d)\n"), old_desktop, GetLastError());
        End If
    
        SelectHDESK = True
    
    End Function

  5. #5
    Join Date
    Oct 2003
    Location
    Timisoara, Romania
    Posts
    460

    Re: Ctrl-alt-delete....

    Heh Hi everyone!
    I miss from here for more than 2 months and when I come back I see that my name is not forgotten This makes me feel very good BTW the task of sending Ctrl+Alt+Del took a lot of my free time but still I got to no result. Maybe some day, ...some day indeed
    You liked it? Please show your gratitude and rate it!

    There is no 'patch' for stupidity.

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Ctrl-alt-delete....

    Sorry son, but it aint going to happen. You cannot send Ctrl + Alt + Del any more than you can write a program that will short together pins 7 and 25 of the parallel port..

    WHy not?

    Well, that's effectively what Ctrl+Alt+Del is. Its a hard wired circuit into the motherboard, you cant replicate it in software. WHen you press these 3 keys, they create a special electrical circuit that generates what is called an interrupt. THis is a signal to the CPU that something wants attention. The windows kernel drivers intercept this interrupt and go through the motions of whatever chunk of code is run, that shows the ctrl+alt+del screen. It cannot be replicated in software without writing your own kernel level keyboard driver or something.

    Find another way of doing what you want (whatever it is), this way is too much like hard work
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured