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

C# 如何在windows服务的锁定/解锁会话处理程序中使用WtsApi32.dll?,c#,.net,visual-studio-2010,wmi,interopservices,C#,.net,Visual Studio 2010,Wmi,Interopservices,我想使用windows服务检测并关闭任何程序(例如:Notepad.exe)。下面的代码是在控制台应用程序中很好的选择 class Program { private static SessionSwitchEventHandler sseh; static void Main(string[] args) { sseh = new SessionSwitchEventHandler(SystemEvents_SessionSwitch);

我想使用windows服务检测并关闭任何程序(例如:Notepad.exe)。下面的代码是在控制台应用程序中很好的选择

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());
    }
}
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 ");
    }
}
但上述代码在windows服务windows 7中不起作用。请查看此链接:

我在windows服务中编写了以下代码,该服务不在Win7上运行,它每次都在windows 7的控制台应用程序中运行

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());
    }
}
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 ");
    }
}
我已经读过这篇文章()但我不能使用

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

:

仅当消息泵正在运行时才会引发此事件。在Windows服务中,除非使用隐藏表单或消息泵已手动启动,否则不会引发此事件。有关显示如何在Windows服务中使用隐藏窗体处理系统事件的代码示例,请参阅SystemEvents类

代码示例已打开,它还注意到:

服务没有消息循环,除非允许它们与桌面交互。如果消息循环不是由隐藏表单提供的(如本例中所示),则服务必须在本地系统帐户下运行,并且需要手动干预以启用与桌面的交互。也就是说,管理员必须在“服务属性”对话框的“登录”选项卡上手动选中“允许服务与桌面交互”复选框。在这种情况下,将自动提供一个消息循环。只有在本地系统帐户下运行服务时,此选项才可用。无法以编程方式启用与桌面的交互

你只说“不行”和“不能用”。如果你想得到更好的答案,请提供确切发生了什么、你期望发生什么以及你尝试过什么的信息。