简体   繁体   English

如何在Windows服务的锁定/解锁会话处理程序中使用WtsApi32.dll?

[英]how to use WtsApi32.dll in lock/Unlock session handler in windows service?

I want to detect and close any program (for example: Notepad.exe) by using a windows service. 我想使用Windows服务检测并关闭任何程序(例如:Notepad.exe)。 Below code is good choose in a console application. 下面的代码是在控制台应用程序中的不错选择。

class Program
{
    private static SessionSwitchEventHandler sseh;
    static void Main(string[] args)
    {
        sseh = new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
        SystemEvents.SessionSwitch += sseh;
        while (true) { }
    }

    static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
    {
        Console.WriteLine(e.Reason.ToString());
    }
}

But Above code is not working in a windows service windows 7. look this link : 但以上代码在Windows Service Windows 7中不起作用。请看此链接:

http://social.msdn.microsoft.com/Forums/eu/netfxcompact/thread/04b16fac-043a-41c3-add9-482c912e95be http://social.msdn.microsoft.com/Forums/eu/netfxcompact/thread/04b16fac-043a-41c3-add9-482c912e95be

I have written below code in the windows service which does not run on win 7, it is working every time on windows 7 in console application. 我已经在无法在win 7上运行的Windows服务中编写了以下代码,每次在控制台应用程序中的Windows 7上都可以正常工作。

protected override void OnStart(string[] args)
{ 
    SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
    Console.ReadLine();
    SystemEvents.SessionSwitch -= SystemEvents_SessionSwitch;
}


static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
    WriteToLogFile( e.Reason.ToString());
    if (e.Reason == SessionSwitchReason.SessionLock)
    {
         WriteToLogFile("SessionLock ");
    }
    if (e.Reason == SessionSwitchReason.SessionUnlock)
    {
         WriteToLogFile("SessionUnlock ");
    }
    if (e.Reason == SessionSwitchReason.SessionLogon)
    {
         WriteToLogFile("SessionLogon ");
    }
}

I have been read this article ( http://rhauert.wordpress.com/category/ucc/ ) but I can not use 我已经阅读了这篇文章( http://rhauert.wordpress.com/category/ucc/ ),但是我不能使用

protected override void OnStart(string[] args)
{
     WriteToText("Windows Service is started");
     SessionChangeHandler x = new SessionChangeHandler();
}

MSDN: MSDN:

SystemEvents.SessionSwitch Event : SystemEvents.SessionSwitch事件

This event is only raised if the message pump is running. 仅在消息泵正在运行时才引发此事件。 In a Windows service, unless a hidden form is used or the message pump has been started manually, this event will not be raised. 在Windows服务中,除非使用隐藏表单或手动启动消息泵,否则不会引发此事件。 For a code example that shows how to handle system events by using a hidden form in a Windows service, see the SystemEvents class. 有关显示如何通过使用Windows服务中的隐藏表单处理系统事件的代码示例,请参见SystemEvents类。

The code sample is on this page , which also notes: 此页面上的代码示例,还指出:

Services do not have message loops, unless they are allowed to interact with the desktop. 服务没有消息循环,除非允许它们与桌面交互。 If the message loop is not provided by a hidden form, as in this example, the service must be run under the local system account, and manual intervention is required to enable interaction with the desktop. 如果此隐藏示例未提供消息循环,则该服务必须在本地系统帐户下运行,并且需要手动干预才能与桌面进行交互。 That is, the administrator must manually check the Allow service to interact with desktop check box on the Log On tab of the service properties dialog box. 即,管理员必须手动选中服务属性对话框的“登录”选项卡上的“允许服务与桌面交互”复选框。 In that case, a message loop is automatically provided. 在这种情况下,将自动提供消息循环。 This option is available only when the service is run under the local system account. 仅当服务在本地系统帐户下运行时,此选项才可用。 Interaction with the desktop cannot be enabled programmatically. 无法通过编程方式启用与桌面的交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM