CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  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

  7. #7
    Join Date
    Mar 2005
    Posts
    17

    Re: Ctrl-alt-delete....

    What's the point of emulating the keys? Just shell run the program you want.
    For example, if you wanted to run Task Manager (default Ctrl-Alt-Del program in XP), you can just do the following:

    Code:
    Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    
    Dim tString as String, ret as Long, tPath as String
    tString = String(200, Chr$(0))
    tPath = Left$(tString, GetWindowsDirectory(tString, Len(tString))) + "\taskmgr.exe"
    Shell tPath, vbNormalFocus
    Simple really!

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

    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..
    Yes you can, RealVNC and Radmin surely can. But yes, perhaps this is the only type of application that need this kind of feature (Remote workstation access), because all of the feature in the Windows Security dialog can be accessed by some other way.

    But I find it rather challenging to do it in VB

    Some additional information, my posted code above seems only work for LocalSystem account, so that means posting CTRL ALT DEL can only be done by a Service running on that WorkStation (I have verified it with RealVNC)

    to VMA, here are a summarize of problems before we can simulate the keystroke :
    - OpenDesktop to Winlogon must be called from a thread with no GUI opened
    - the thread must be created by a service run as LocalSystem account

    So as a conclusion, no pure VB code can do this. Perhaps some day someone can proof otherwise

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

    Re: Ctrl-alt-delete....

    The only way to do it, I heard but never seen is to rewrite your own keyboard driver as cjard mentioned (not in VB ofcourse :P). Still I have something msgina.dll. It helped me disable ctrl+alt+del so maybe it will help me trigger it I still have more to study.
    You liked it? Please show your gratitude and rate it!

    There is no 'patch' for stupidity.

  10. #10
    Join Date
    Mar 2005
    Posts
    17

    Re: Ctrl-alt-delete....

    Holy **** that's extreme!
    Re-writing drivers and stuff it totally pointless!
    I just read on a site that you can actually get the program that Windows runs when it is sent the Ctrl-Alt-Del message from the motherboard in the registry... but I don't know the key, it didn't specify.

    Try searching your registry for taskmgr or whatever your version of XP runs, you might find the key

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

    Re: Ctrl-alt-delete....

    Quote Originally Posted by Burningmace
    I just read on a site that you can actually get the program that Windows runs when it is sent the Ctrl-Alt-Del message from the motherboard in the registry... but I don't know the key, it didn't specify.
    That's msgina.dll and it is called when you press Ctrl+Alt+Del. What we want here is do the exact oposite thing. Simulate the key pressing.
    Last edited by vma; March 2nd, 2005 at 03:05 AM.
    You liked it? Please show your gratitude and rate it!

    There is no 'patch' for stupidity.

  12. #12
    Join Date
    Mar 2005
    Posts
    17

    Re: Ctrl-alt-delete....

    But why?
    You can just run TaskMgr.exe or whatever.
    I think you misunderstood what I wrote above.
    Here's some pseudo code to explain what I mean:
    Code:
    Get path of program that is ran when you press Ctrl-Alt-Del (e.g TaskMgr.exe)
    Execute it via the use of the Shell function.
    msgina.dll has nothing to do with that above...

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

    Re: Ctrl-alt-delete....

    Task manager is one of the option of logon menu. Task is to bring up the
    logon menu. I agree you can always write your formLogon with buttons to
    invoke Task Manager or ShutDown dialog or Logoff one. But it is not the same
    thing as sending Ctrl+Alt+Del...
    ...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.

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

    Re: Ctrl-alt-delete....

    Quote Originally Posted by Luthv
    Yes you can, RealVNC and Radmin surely can. But yes, perhaps this is the only type of application that need this kind of feature (Remote workstation access), because all of the feature in the Windows Security dialog can be accessed by some other way.
    I'd have thought that the VNC boys really would have written some drivers.. Im dimly aware that people like UltraVNC have written their own graphics drivers for more optimized remote networking..
    I must admit, i did always wonder how they did it...

    BUt ultimately the answer (not for LuthV because if i tell him this im telling my grandma how to suck eggs) is "you cannot 'send' Ctrl + Alt + Delete because the key combination does not exist"

    when those keys are pressed, the keyboard doesnt send the ascii code for a Ctrl, the ascii code for an Alt and the ascii Ctrl for a delete through to the higher level functions of the os.. it actually causes an IRQ interrupt on one of the interrupt lines on the motherboard, just like a PCI device does when it wants attention.

    Windows is programmed to respond to that interrupt, but because interrupt lines are physical electrical wires that a voltage appears on, you cant make software do this; software cannot generate voltage in a switched circuit that is not accessible by a software-controlled voltage device..

    So basically, forget about sending Ctrl + Alt + Delete and tell us WHY you want to send it, and we will advise you what other thing you can do to access the same feature..
    "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

  15. #15
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: Ctrl-alt-delete....

    Quote Originally Posted by cjard
    when those keys are pressed, the keyboard doesnt send the ascii code for a Ctrl, the ascii code for an Alt and the ascii Ctrl for a delete through to the higher level functions of the os.. it actually causes an IRQ interrupt on one of the interrupt lines on the motherboard, just like a PCI device does when it wants attention.

    Windows is programmed to respond to that interrupt, but because interrupt lines are physical electrical wires that a voltage appears on, you cant make software do this; software cannot generate voltage in a switched circuit that is not accessible by a software-controlled voltage device..
    I'm not sure that this is correct. I'm almost positive that anything coming through the keyboard would have to go through interrupt 60h. The book the Art of Assembly gives a pretty good (OK, the best I've seen) description of the keyboard hardware. That chapter is available here:

    http://maven.smith.edu/~thiebaut/Art...20/CH20-1.html

    According to AoA, it's possible to write instructions directly to the microprocessor inside the keyboard using interrupt 64h, and the author even gives an example of how one would disable Ctrl+Alt+Del by discarding the Del scancodes. If it is possible to hook these scan codes at a machine language level, it should be possible to write them as well. There is even an instruction for the keyboard processor that tells it to echo back the next input, so it would seem to me that this would be fairly straight-forward in assembly. If you used the same trick for running precompilied ASM strings that some subclassers use, this should be possible do entirely from VB. My ASM is still a bit shakey, but I might try to hammer this out over the weekend.

Page 1 of 2 12 LastLast

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