Tell if lock screen is up!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RAJSPY
    New Member
    • Jan 2008
    • 18

    Tell if lock screen is up!

    Hi all,

    I want to start a program through a SMS job. Only, the program must wait until the user is logged on. so far so good realizing this with SMS. The problem is that our company has a screen lock within 10 minutes (company policy) if the program starts with the lock screen on I want to wait until the user unlocks the desktop. Is there some variable that records if the screen is locked?

    Thanks in advanced,
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I wish I were more help on this, but yes I think there was a way to get this information. I saw it on here a long time ago. Don't remember how or where though, sorry.

    Comment

    • RAJSPY
      New Member
      • Jan 2008
      • 18

      #3
      Yeah i have seen one with session switch hooks. But that doesn't give a answer back if the screen is already locked.

      Anybody else has a clue??

      Thanx

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I am thinking somewhere burried in WMI you can get the current user state, which should include if screen is locked or not.

        Comment

        • KodeKrazy
          New Member
          • Mar 2008
          • 32

          #5
          Try this. You may have to tweak it a bit as I converted it to .NET from some old VB6 snippet I used previously in an app that required it. Also, you may get an error on the "Switch Screen" part, if you do, it means the desktop is locked.

          KK

          Code:
          Public Class Form1
          Inherits System.Windows.Forms.Form
          Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
          Call Test()
          End Sub
          End Class
          
          Module Module1
          Private Declare Function OpenDesktop Lib "user32" Alias "OpenDesktopA" (ByVal lpszDesktop As String, ByVal dwFlags As Integer, ByVal fInherit As Integer, ByVal dwDesiredAccess As Integer) As Integer
          Private Declare Function SwitchDesktop Lib "user32" (ByVal hDesktop As Integer) As Integer
          Private Declare Function CloseDesktop Lib "user32" (ByVal hDesktop As Integer) As Integer
          Private Const DESKTOP_SWITCHDESKTOP As Integer = &H100
          
          Public Sub Test()
          Dim p_lngHwnd As Integer
          Dim p_lngRtn As Integer
          Dim p_lngErr As Integer
          
          p_lngHwnd = OpenDesktop(lpszDesktop:="Default", dwFlags:=0, fInherit:=False, dwDesiredAccess:=DESKTOP_SWITCHDESKTOP)
          If p_lngHwnd = 0 Then
          Debug.WriteLine("Err with OpenDesktop: " & Err.LastDllError)
          Else
          p_lngRtn = SwitchDesktop(hDesktop:=p_lngHwnd)
          p_lngErr = Err.LastDllError
          
          If p_lngRtn = 0 Then
          If p_lngErr = 0 Then
          Debug.WriteLine("Desktop is locked: " & Err.LastDllError)
          Else
          Debug.WriteLine("Error with SwitchDesktop: " & Err.LastDllError)
          End If
          Else
          Debug.WriteLine("Not Locked")
          End If
          p_lngHwnd = CloseDesktop(p_lngHwnd)
          End If
          End Sub
          End Module

          Comment

          • RAJSPY
            New Member
            • Jan 2008
            • 18

            #6
            Perfect

            Thanx it works perfect.

            I had some code like this but the syntaxes where not good. And I was just searching for some answers and you dropped them here Thanx again.

            I've turned it to this C# code:
            Code:
            [DllImport("user32", EntryPoint ="OpenDesktopA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            private static extern int OpenDesktopA(String lpszDesktop, int dwFlags, int fInherit, int dwDesiredAccess);
                    
            public int OpenDesktop(String lpszDesktop,int dwFlags,int fInherit,int dwDesiredAccess)
            {
                return OpenDesktopA(lpszDesktop,dwFlags,fInherit,dwDesiredAccess);
            }
                    
            [DllImport("user32", EntryPoint ="SwitchDesktop", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            private static extern int SwitchDesktop(int hDesktop);
            
            public int Switch_Desktop(int hDesktop)
            {
              return SwitchDesktop(hDesktop);
            }
            
            [DllImport("user32", EntryPoint = "CloseDesktop", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            private static extern int CloseDesktop(int hDesktop);
            
            public int Close_Desktop(int hDesktop)
            {
               return CloseDesktop(hDesktop);
            }
            
            And in your application:
            private const int DESKTOP_SWITCHDESKTOP = 0x100;
            
            int p_lngHwnd;
            int p_lngRtn;
            try
            {	        
              p_lngHwnd = _ModalDesktop.OpenDesktop("default", 0, 0, DESKTOP_SWITCHDESKTOP);
             p_lngRtn = _ModalDesktop.Switch_Desktop(p_lngHwnd);
             if (p_lngRtn == 0) 
            {
              //Do somthing here .....
            }
             p_lngHwnd = _ModalDesktop.Close_Desktop(p_lngHwnd); 
            }
            catch(Exception ex)
            {
              //Some code here .....
            }
            Last edited by Curtis Rutland; Aug 18 '08, 01:36 PM. Reason: Added Code Tags - Please use the # button

            Comment

            Working...