在C#中获取用户空闲时间?

11

我找到了这个有关获取用户空闲时间的教程 Idle Time

问题是只有应用程序在用户上运行时才能起作用。

而我的应用程序运行在系统上。

我该如何获取空闲时间?或者如果PC闲置怎么办?


4
由于在 SYSTEM 下运行的服务种类繁多,因此它很少实际处于“空闲”状态,所以这个数字几乎肯定为零。你能否给我们更好的想法,您希望如何使用这个数字?你的更大目标是什么? - Cos Callis
2
我猜更大的目标是看到连接的鼠标和键盘设备的闲置。 - Akku
1
如果您想获取已登录用户的空闲时间,您应该知道在现代Windows中,使用快速用户切换或终端服务(可能有多个已登录用户)是没有这样的功能的。 - Damien_The_Unbeliever
3个回答

6

我知道这个答案已经被接受了,但我想补充一下,以防有人想在终端服务环境中做同样的事情。

Cassia 是一个开源库,它在 Windows 终端服务 API 周围放置了 .NET 包装器。我已经用它来管理我的服务器,它工作得非常好。您可以通过调用 ITerminalServicesSession.LastInputTime 来获取任何会话的空闲时间。


3

据我所了解,您对GetLastInputInfo函数的结果感到满意。因此,我建议如下操作:

假设您有一个在本地系统帐户下运行的可执行文件A。创建可执行文件B,利用GetLastInputInfo函数收集相关的系统信息。接下来,使用CreateProcessAsUser函数以已登录用户令牌的方式从可执行文件A中运行可执行文件B。这个类可以帮助您完成此操作(不幸的是,我无法找到其中发布的stackoverflow问题):

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Helpers
{
    [StructLayout(LayoutKind.Sequential)]
    internal struct PROCESS_INFORMATION
    {
        public IntPtr hProcess;
        public IntPtr hThread;
        public uint dwProcessId;
        public uint dwThreadId;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct SECURITY_ATTRIBUTES
    {
        public uint nLength;
        public IntPtr lpSecurityDescriptor;
        public bool bInheritHandle;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct STARTUPINFO
    {
        public uint cb;
        public string lpReserved;
        public string lpDesktop;
        public string lpTitle;
        public uint dwX;
        public uint dwY;
        public uint dwXSize;
        public uint dwYSize;
        public uint dwXCountChars;
        public uint dwYCountChars;
        public uint dwFillAttribute;
        public uint dwFlags;
        public short wShowWindow;
        public short cbReserved2;
        public IntPtr lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }

    internal enum SECURITY_IMPERSONATION_LEVEL
    {
        SecurityAnonymous,
        SecurityIdentification,
        SecurityImpersonation,
        SecurityDelegation
    }

    internal enum TOKEN_TYPE
    {
        TokenPrimary = 1,
        TokenImpersonation
    }

    public class ImpersonateProcessAsLoggedUser
    {
        private const short SW_SHOW = 5;
        private const uint TOKEN_QUERY = 0x0008;
        private const uint TOKEN_DUPLICATE = 0x0002;
        private const uint TOKEN_ASSIGN_PRIMARY = 0x0001;
        private const int GENERIC_ALL_ACCESS = 0x10000000;
        private const int STARTF_USESHOWWINDOW = 0x00000001;
        private const int STARTF_FORCEONFEEDBACK = 0x00000040;
        private const uint CREATE_UNICODE_ENVIRONMENT = 0x00000400;

        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern bool CreateProcessAsUser(
            IntPtr hToken,
            string lpApplicationName,
            string lpCommandLine,
            ref SECURITY_ATTRIBUTES lpProcessAttributes,
            ref SECURITY_ATTRIBUTES lpThreadAttributes,
            bool bInheritHandles,
            uint dwCreationFlags,
            IntPtr lpEnvironment,
            string lpCurrentDirectory,
            ref STARTUPINFO lpStartupInfo,
            out PROCESS_INFORMATION lpProcessInformation);


        [DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx", SetLastError = true)]
        private static extern bool DuplicateTokenEx(
            IntPtr hExistingToken,
            uint dwDesiredAccess,
            ref SECURITY_ATTRIBUTES lpThreadAttributes,
            Int32 ImpersonationLevel,
            Int32 dwTokenType,
            ref IntPtr phNewToken);


        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern bool OpenProcessToken(
            IntPtr ProcessHandle,
            UInt32 DesiredAccess,
            ref IntPtr TokenHandle);

        [DllImport("userenv.dll", SetLastError = true)]
        private static extern bool CreateEnvironmentBlock(
            ref IntPtr lpEnvironment,
            IntPtr hToken,
            bool bInherit);


        [DllImport("userenv.dll", SetLastError = true)]
        private static extern bool DestroyEnvironmentBlock(
            IntPtr lpEnvironment);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool CloseHandle(
            IntPtr hObject);


        private static bool LaunchProcessAsUser(string cmdLine, IntPtr token, IntPtr envBlock)
        {
            bool result = false;


            var pi = new PROCESS_INFORMATION();
            var saProcess = new SECURITY_ATTRIBUTES();
            var saThread = new SECURITY_ATTRIBUTES();
            saProcess.nLength = (uint) Marshal.SizeOf(saProcess);
            saThread.nLength = (uint) Marshal.SizeOf(saThread);

            var si = new STARTUPINFO();
            si.cb = (uint) Marshal.SizeOf(si);


            //if this member is NULL, the new process inherits the desktop
            //and window station of its parent process. If this member is
            //an empty string, the process does not inherit the desktop and
            //window station of its parent process; instead, the system
            //determines if a new desktop and window station need to be created.
            //If the impersonated user already has a desktop, the system uses the
            //existing desktop.

            si.lpDesktop = @"WinSta0\Default"; //Modify as needed
            si.dwFlags = STARTF_USESHOWWINDOW | STARTF_FORCEONFEEDBACK;
            si.wShowWindow = SW_SHOW;
            //Set other si properties as required.

            result = CreateProcessAsUser(
                token,
                null,
                cmdLine,
                ref saProcess,
                ref saThread,
                false,
                CREATE_UNICODE_ENVIRONMENT,
                envBlock,
                null,
                ref si,
                out pi);


            if (result == false)
            {
                int error = Marshal.GetLastWin32Error();
                string message = String.Format("CreateProcessAsUser Error: {0}", error);
                Debug.WriteLine(message);
            }

            return result;
        }


        private static IntPtr GetPrimaryToken(int processId)
        {
            IntPtr token = IntPtr.Zero;
            IntPtr primaryToken = IntPtr.Zero;
            bool retVal = false;
            Process p = null;

            try
            {
                p = Process.GetProcessById(processId);
            }

            catch (ArgumentException)
            {
                string details = String.Format("ProcessID {0} Not Available", processId);
                Debug.WriteLine(details);
                throw;
            }


            //Gets impersonation token
            retVal = OpenProcessToken(p.Handle, TOKEN_DUPLICATE, ref token);
            if (retVal)
            {
                var sa = new SECURITY_ATTRIBUTES();
                sa.nLength = (uint) Marshal.SizeOf(sa);

                //Convert the impersonation token into Primary token
                retVal = DuplicateTokenEx(
                    token,
                    TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_QUERY,
                    ref sa,
                    (int) SECURITY_IMPERSONATION_LEVEL.SecurityIdentification,
                    (int) TOKEN_TYPE.TokenPrimary,
                    ref primaryToken);

                //Close the Token that was previously opened.
                CloseHandle(token);
                if (retVal == false)
                {
                    string message = String.Format("DuplicateTokenEx Error: {0}", Marshal.GetLastWin32Error());
                    Debug.WriteLine(message);
                }
            }

            else
            {
                string message = String.Format("OpenProcessToken Error: {0}", Marshal.GetLastWin32Error());
                Debug.WriteLine(message);
            }

            //We'll Close this token after it is used.
            return primaryToken;
        }

        private static IntPtr GetEnvironmentBlock(IntPtr token)
        {
            IntPtr envBlock = IntPtr.Zero;
            bool retVal = CreateEnvironmentBlock(ref envBlock, token, false);
            if (retVal == false)
            {
                //Environment Block, things like common paths to My Documents etc.
                //Will not be created if "false"
                //It should not adversley affect CreateProcessAsUser.

                string message = String.Format("CreateEnvironmentBlock Error: {0}", Marshal.GetLastWin32Error());
                Debug.WriteLine(message);
            }
            return envBlock;
        }

        public static bool Launch(string appCmdLine /*,int processId*/)
        {
            bool ret = false;

            //Either specify the processID explicitly
            //Or try to get it from a process owned by the user.
            //In this case assuming there is only one explorer.exe

            Process[] ps = Process.GetProcessesByName("explorer");
            int processId = -1; //=processId
            if (ps.Length > 0)
            {
                processId = ps[0].Id;
            }

            if (processId > 1)
            {
                IntPtr token = GetPrimaryToken(processId);

                if (token != IntPtr.Zero)
                {
                    IntPtr envBlock = GetEnvironmentBlock(token);
                    ret = LaunchProcessAsUser(appCmdLine, token, envBlock);
                    if (envBlock != IntPtr.Zero)
                        DestroyEnvironmentBlock(envBlock);

                    CloseHandle(token);
                }
            }
            return ret;
        }
    }
}

接下来,您需要找到一种方式将从可执行文件B收集到的信息发送给可执行文件A。有许多方法可以实现这一点。其中之一是.Net Remoting。但是,您也可以创建一个中间的XML甚至文本文件。
也许这不是解决您问题的最佳方式,但如果您需要更多的本地系统<->已登录用户交互,您可以遵循这个模式。

重点是可执行文件B将在当前用户上下文中运行。这就是为什么GetLastInputInfo应该返回该特定用户的统计信息。这个类现在在我的系统中工作,它包括在Local System帐户下运行的Windows服务和从该服务注入到已登录用户上下文中的可执行文件。通信渠道是.Net Remoting。看一下Launch方法和这一行: IntPtr token = GetPrimaryToken(processId);更新:哇,我在回复什么? :-) - ReVolly
抱歉,我仔细查看了代码并意识到你在做什么,但是你的描述没有清楚地表明解决方案的这个方面。然而,我怀疑在终端服务环境中这种方法不会非常有效,因为你无法确定你实际上正在与哪个会话通信,当然,这可能是一个可接受的限制,具体取决于使用情况,所以我只是提醒一下。 - Chris Taylor
谢谢,Chris。抱歉我的描述不太好,我英语不是很好。我没有在终端服务环境中尝试过这段代码,但在我的情况下它非常有效。实际上,建议的解决方案在你的情况下可能无法工作的唯一原因是错误的资源管理器令牌。这种情况需要进行一些额外的调查。 - ReVolly
@Virtuality 你好,你认为在Windows Service(运行在SYSTEM上)和Normal Process(运行在CURRENT USER上)之间进行通信的最佳方式是什么? 顺便说一下,你的代码真棒!它能在所有平台上运行吗?(XP/Vista/7)? - Danpe
@Danpe 嗯,谢谢,但这不是我的代码,我在StackOverflow上找到的。据我所知,在XP / Vista / 7上运行没有问题。当我开始提到的项目时,我正在使用 .Net Remoting,但现在不确定这是否是最佳选择。如果您正在 .NET 4 上开发,根据您的要求,我建议查看 1) WCF(tcp或命名管道端点),2)NamedPipes(据我所知,.NET 4具有原生支持)。当然,这些只是一些选项。 - ReVolly

2
做类似这样的事情,推荐的方法是运行一个独立的应用程序,将用户会话数据传递给服务。您可以通过将其添加到注册表中的HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run来在用户会话开始时配置应用程序。您可以在以下 KB 文章中阅读有关此方法的更多详细信息:http://support.microsoft.com/kb/308403

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接