问题描述
using System;
using System.Runtime.InteropServices;
public class WindowsFunctions
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
public static int TicksSinceLastinput()
{
var info = new LASTINPUTINFO();
GetLastInputInfo(ref info);
var lastInputTickCount = info.dwTime;
return (int)lastInputTickCount;
}
}
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
[MarshalAs(UnmanagedType.U4)]
public UInt32 cbSize;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dwTime;
}
但是,在运行时,info.dwTime
为零。
在 VS2019 IDE 中运行
更新:
我尝试使 TicksSinceLastInput
不是静态的,但无论如何都失败了。
我失败的单元测试现在是:
[TestMethod]
public void TestTicksSinceLastinput()
{
var funcs = new WindowsFunctions();
var ticks = funcs.TicksSinceLastinput();
Assert.IsTrue( ticks > 0);
}
更新:
我的代码现在是:
public class WindowsFunctions
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
public int TicksSinceLastinput()
{
var info = new LASTINPUTINFO();
var result = GetLastInputInfo(ref info);
var lastInputTickCount = info.dwTime;
return (int)lastInputTickCount;
}
}
result 被设置为 false。
解决方法
LASTINPUTINFO
的声明,看起来来自于 PInvoke.net。
结构体包含一个静态整数:
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
它可能看起来像是在定义结构体的大小,但实际上并没有实际用途。
在你的原始代码中,结构体的 cbSize
成员没有被初始化,它必须是:它指定了结构体本身的大小;分配它是强制性的。
GetLastInputInfo 函数声明是正确的:
[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO info);
它的返回类型是BOOL
(定义为typedef int BOOL
),而不是BOOLEAN
(定义为typedef BYTE BOOLEAN
)。BOOL
是托管的,不需要:
[return: MarshalAs(UnmanagedType.Bool)]
可以简化 LASTINPUTINFO 结构的声明和初始化,包括一个初始化其成员的非空构造函数:
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
public LASTINPUTINFO(uint init) {
cbSize = (uint)Marshal.SizeOf<LASTINPUTINFO>();
dwTime = init;
}
}
其成员类型为:
-
UINT cbSize
:UINT
定义为typedef unsigned int UINT
,无符号整数。 它是托管类型,不需要(此处)[MarshalAs(UnmanagedType.U4)]
-
DWORD dwTime
:DWORD
定义为typedef unsigned long DWORD
,它是一个 32 位无符号整数。仍然可以管理,不需要[MarshalAs(UnmanagedType.U4)]
。
当然,指定编组类型不会有什么坏处(当它正确时,就是这样)。
示例用法,使用 Timer 显示自上次输入以来经过的毫秒数:
// Better resolution than System.Windows.Forms.Timer
System.Timers.Timer sysIdleTimer = null;
// [...]
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
sysIdleTimer = new System.Timers.Timer() {
Interval = 100,SynchronizingObject = this // Marshal events to the UI Thread
};
sysIdleTimer.Elapsed += this.OnTimerElapsed;
sysIdleTimer.Start();
}
// [...]
static uint GetLastInputTimeMilliseconds()
{
var info = new LASTINPUTINFO(0);
if (GetLastInputInfo(ref info)) {
// Valid wihin 24.9 days of machine activity
uint idleTime = (uint)Environment.TickCount - info.dwTime;
return idleTime;
}
return 0;
}
// [...]
protected void OnTimerElapsed(object sender,System.Timers.ElapsedEventArgs e)
{
// Shows the Input Idle time in a Label
this.lblInputIdle.Text = $"Idle Time: {GetLastInputTimeMilliseconds()} milliseconds";
}