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 FunctionEnd Class
<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
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."
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