【问题标题】:GetLastInputInfo not returning dwTimeGetLastInputInfo 不返回 dwTime
【发布时间】:2021-08-20 03:42:49
【问题描述】:

我有以下代码:

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;
    }
}

结果被设置为假。

【问题讨论】:

  • 我没有看到你在初始化cbSize。你计算它,但我没有看到任何初始化
  • Pinvoke.net 永远是你的朋友:pinvoke.net/default.aspx/user32/GetLastInputInfo.html
  • 谢谢各位。我现在可以正常工作了,我没有正确初始化。 PInvoke 中的代码是正确的

标签: c# winforms winapi interop user32


【解决方案1】:

LASTINPUTINFO 的声明似乎来自PInvoke.net
该结构包含一个静态 int:

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 cbSizeUINT 定义为typedef unsigned int UINT,无符号整数。 这是一个托管类型,它不需要(这里)[MarshalAs(UnmanagedType.U4)]
  • DWORD dwTimeDWORD 定义为 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 += OnTimerElapsed;
    sysIdleTimer.Start();
}

static uint GetLastInputTimeMilliseconds()
{
    var info = new LASTINPUTINFO(0);
       
    if (GetLastInputInfo(ref info)) {
        // Valid within 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
    lblInputIdle.Text = $"Idle Time: {GetLastInputTimeMilliseconds()} milliseconds";
}

【讨论】:

    猜你喜欢
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 2013-09-26
    相关资源
    最近更新 更多