【问题标题】:Capturing windows event with java windows services使用 java windows 服务捕获 windows 事件
【发布时间】:2011-04-10 15:16:32
【问题描述】:

我想通过 Windows 服务捕获锁定/解锁/启动/关闭/注销和登录事件,然后我想为每个事件触发一个函数,以便我可以捕获事件发生的时间。

我想通过 Windows 服务来执行此操作,这样我就不需要手动运行程序。我想通过java语言运行这个程序。

【问题讨论】:

    标签: java windows-services event-log


    【解决方案1】:

    看起来您需要使用 JNA 并使用本机 Windows 调用编写捕获代码。

    有一个类java.awt.Robot 做相反的事情——模拟操作系统事件,但我不知道在纯Java 中捕获事件的方法。

    【讨论】:

      【解决方案2】:

      在 C# 中它非常简单。我可以向您展示 C# 中的代码,然后如果您想将 Java 用作一门语言,您可以将其转换为 Ja.Net。 (如果你真的想使用 JVM,这不会有多大帮助)。

      1. 创建空的 C# 服务。
      2. 在您的程序 Main 方法中将 CanHandleSessionChangeEvent 属性设置为 true:

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            LogService logService = new LogService();
            logService.CanHandleSessionChangeEvent = true;
            ServicesToRun = new ServiceBase[] 
            { 
                logService 
            };
            ServiceBase.Run(ServicesToRun);
        }
        
      3. 在服务实现中覆盖 OnSessionChange 事件,您可以在其中转储有关用户登录/注销和会话连接/断开连接的信息

        protected override void OnSessionChange(SessionChangeDescription changeDescription)
        {
            EventLog.WriteEvent(
                new EventInstance(100, 0, EventLogEntryType.Information), 
                String.Format("Reason: {0}, SessionId:{1}", changeDescription.Reason, changeDescription.SessionId));
            base.OnSessionChange(changeDescription);
        }
        
      4. 注册服务,启动它并查看事件日志中的记录。

      【讨论】:

        猜你喜欢
        • 2012-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多