Avatar of Sheldon Livingston
Sheldon Livingston
Flag for United States of America asked on

Obtaining idle time via VB.NET.

Happy new year all...

I am trying to get the system idle time via version 4 of the .NET framework using VB.NET.

Public Class idleTime
    Public Structure LASTINPUTINFO
        Public cbSize As UInteger
        Public dwTime As UInteger
    End Structure

    <DllImport("user32.dll")>
    Private Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
    End Function

    Public Function GetInactiveTime() As String ' Nullable(Of TimeSpan)
        Dim info As LASTINPUTINFO = New LASTINPUTINFO
        info.cbSize = CUInt(Marshal.SizeOf(info))

        If (GetLastInputInfo(info)) Then
            Return TimeSpan.FromMilliseconds(Environment.TickCount - info.dwTime).ToString '  
        Else
            Return "N/A" 'Nothing
        End If
    End Function
End Class

Open in new window

Through testing I see that info.dwTime always returns zero.

Thoughts?
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Sheldon Livingston

8/22/2022 - Mon
DevAdmin

Read this https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getlastinputinfo

If return 0 the function fails.

Try change the definitio of LASTINPUTINFO

Private Structure LASTINPUTINFO
      Dim cbSize As Int32
      Dim dwTime As Int32
End Structure
Ron Malmstead

This link claims to have a working example.

https://stackoverflow.com/questions/8280799/vb-net-how-to-know-time-for-which-system-is-idle

Looks a lot like yours except this > 

Private Declare Function GetTickCount Lib "kernel32" () As Long
Sheldon Livingston

ASKER
I think the issue is with LASTINPUTINFO... it always returns zero while tick count is good.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
DevAdmin

With these definition work for me:

Imports System.Runtime.InteropServices

    <StructLayout(LayoutKind.Sequential)> Structure LASTINPUTINFO
        <MarshalAs(UnmanagedType.U4)> Public cbSize As Integer
        <MarshalAs(UnmanagedType.U4)> Public dwTime As Integer
    End Structure

    <DllImport("user32.dll")>
    Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
    End Function

And I test idle time with this code:

        lastInputInf.cbSize = Marshal.SizeOf(lastInputInf)
        lastInputInf.dwTime = 0
        GetLastInputInfo(lastInputInf)
Sheldon Livingston

ASKER
It appears that this cannot be done from a service... only an app.
DevAdmin

Look at the help of the function

"This function is useful for input idle detection. However, GetLastInputInfo does not provide system-wide user input information across all running sessions. Rather, GetLastInputInfo provides session-specific user input information for only the session that invoked the function."

https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getlastinputinfo
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
DevAdmin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Sheldon Livingston

ASKER
Thank you Ermanno... I will be looking into this.