请问 GetLastInputInfo 返回最后一次输入时间一直是 0 怎么解决
vb.net吧
全部回复
仅看楼主
level 8
fjy206 楼主
Private Declare Function GetLastInputInfo Lib "user32" (ByRef plii As IntPtr) As Long '把结构 重新定义为 指针 IntPtr
Private Declare Function GetTickCount Lib "kernel32" () As Long
''' <summary>
''' 结构体声明
''' </summary>
<Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)>
Public Structure LASTINPUTINFO
<MarshalAsAttribute(UnmanagedType.U4)>
Dim cbSize As UInt32
<MarshalAsAttribute(UnmanagedType.U4)>
Dim dwTime As Int32 End Structure Private Function aa() As Integer Dim lastInputInfo As New LASTINPUTINFO lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo) Dim thObject2 As Runtime.InteropServices.GCHandle = Runtime.InteropServices.GCHandle.Alloc(lastInputInfo, Runtime.InteropServices.GCHandleType.Pinned)
Dim tpObject2 As IntPtr = thObject2.AddrOfPinnedObject() '取得指向结构的指针
Dim f As Integer Try Dim lostTime As String = "" Dim ret As Long = GetLastInputInfo(tpObject2) If ret <> 0 Then f = lastInputInfo.dwTime 'f = lostTime'/ 1000 / 60 ' MsgBox(lostTime / 1000 / 60) End If Catch ex As Exception MsgBox(ex.ToString) End Try '在使用完毕后一定要释放指针指向的内存块,让垃圾回收器可对这个内存块回收处理
If thObject2.IsAllocated Then thObject2.Free() End If
Return f
End Function
2017年12月29日 01点12分 1
level 13
你这是从VB的声明复制的吧,恰好我不久前也在弄与这个函数有关的程序,首先函数返回值是64位整数也就是long是不行的,其次你这个有点乱,我直接贴给你我写的吧。
Declare Function GetTickCount Lib "kernel32" () As Integer
Declare Function GetLastInputInfo Lib "user32" (ByRef pLASTINPUTINFO As LastInputInfo) As Boolean
Structure LastInputInfo
Dim cbSize As Integer
Dim dwTime As Integer
End Structure
Function GetIdle() As Integer
Dim Info As LastInputInfo
Info.cbSize = Len(Info)
GetLastInputInfo(Info)
Return (GetTickCount - Info.dwTime)
End Function
2018年03月02日 12点03分 2
1