我正在尝试编写一个小程序,该程序作为服务运行并监视用户是否处于活动状态。如果用户空闲(没有鼠标/键盘)一个小时,则某些进程将被终止。如果用户使用 user32.dll 中的 LASTINPUTINFO 运行它,它就可以工作,但它不能作为服务工作。进一步看,我遇到有人说用 SystemPowerInformation 调用 CallNtPowerInformation 并检查 TimeRemaining 成员。我想这样做,但在互操作方面经验很少,希望得到一些帮助/示例:
在 C# 中我会导入:
[DllImport("powrprof.dll", SetLastError = true)]
private static extern UInt32 CallNtPowerInformation(
Int32 InformationLevel,
IntPtr lpInputBuffer,
UInt32 nInputBufferSize,
IntPtr lpOutputBuffer,
UInt32 nOutputBufferSize
);
我相信那么我需要创建一个结构系统电源信息 http://msdn.microsoft.com/en-us/library/windows/desktop/aa373217%28v=vs.85%29.aspx处理结果?
为n00bness道歉
您可以这样获取您需要的信息:
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
const int SystemPowerInformation = 12;
const uint STATUS_SUCCESS = 0;
struct SYSTEM_POWER_INFORMATION
{
public uint MaxIdlenessAllowed;
public uint Idleness;
public uint TimeRemaining;
public byte CoolingMode;
}
[DllImport("powrprof.dll")]
static extern uint CallNtPowerInformation(
int InformationLevel,
IntPtr lpInputBuffer,
int nInputBufferSize,
out SYSTEM_POWER_INFORMATION spi,
int nOutputBufferSize
);
static void Main(string[] args)
{
SYSTEM_POWER_INFORMATION spi;
uint retval = CallNtPowerInformation(
SystemPowerInformation,
IntPtr.Zero,
0,
out spi,
Marshal.SizeOf(typeof(SYSTEM_POWER_INFORMATION))
);
if (retval == STATUS_SUCCESS)
Console.WriteLine(spi.TimeRemaining);
Console.ReadLine();
}
}
}
我无法告诉您此方法在从服务运行时是否会为您提供所需的信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)