C#窗口服务不要更改变量值

时间:2017-04-25 19:34:11

标签: c# service remote-desktop

我正在尝试制作一项小型服务,检查远程桌面会话上的窗口是否存在,如果是,则打开程序

public partial class Service1 : ServiceBase
{
    [DllImport("user32.dll")]
    static extern int GetSystemMetrics(SystemMetric smIndex);
    public enum SystemMetric
    {
        SM_CXSCREEN = 0,  // 0x00
        SM_CYSCREEN = 1,  // 0x01
        SM_CXVSCROLL = 2,  // 0x02
        SM_CYHSCROLL = 3,  // 0x03
        SM_CYCAPTION = 4,  // 0x04
        SM_CXBORDER = 5,  // 0x05
        SM_CYBORDER = 6,  // 0x06
        SM_CXDLGFRAME = 7,  // 0x07
        //SM_CXFIXEDFRAME = 7,  // 0x07
        SM_CYDLGFRAME = 8,  // 0x08
        //SM_CYFIXEDFRAME = 8,  // 0x08
        SM_CYVTHUMB = 9,  // 0x09
        SM_CXHTHUMB = 10, // 0x0A
        SM_CXICON = 11, // 0x0B
        SM_CYICON = 12, // 0x0C
        SM_CXCURSOR = 13, // 0x0D
        SM_CYCURSOR = 14, // 0x0E
        SM_CYMENU = 15, // 0x0F
        SM_CXFULLSCREEN = 16, // 0x10
        SM_CYFULLSCREEN = 17, // 0x11
        SM_CYKANJIWINDOW = 18, // 0x12
        SM_MOUSEPRESENT = 19, // 0x13
        SM_CYVSCROLL = 20, // 0x14
        SM_CXHSCROLL = 21, // 0x15
        SM_DEBUG = 22, // 0x16
        SM_SWAPBUTTON = 23, // 0x17
        SM_CXMIN = 28, // 0x1C
        SM_CYMIN = 29, // 0x1D
        SM_CXSIZE = 30, // 0x1E
        SM_CYSIZE = 31, // 0x1F
        //SM_CXSIZEFRAME = 32, // 0x20
        SM_CXFRAME = 32, // 0x20
        //SM_CYSIZEFRAME = 33, // 0x21
        SM_CYFRAME = 33, // 0x21
        SM_CXMINTRACK = 34, // 0x22
        SM_CYMINTRACK = 35, // 0x23
        SM_CXDOUBLECLK = 36, // 0x24
        SM_CYDOUBLECLK = 37, // 0x25
        SM_CXICONSPACING = 38, // 0x26
        SM_CYICONSPACING = 39, // 0x27
        SM_MENUDROPALIGNMENT = 40, // 0x28
        SM_PENWINDOWS = 41, // 0x29
        SM_DBCSENABLED = 42, // 0x2A
        SM_CMOUSEBUTTONS = 43, // 0x2B
        SM_SECURE = 44, // 0x2C
        SM_CXEDGE = 45, // 0x2D
        SM_CYEDGE = 46, // 0x2E
        SM_CXMINSPACING = 47, // 0x2F
        SM_CYMINSPACING = 48, // 0x30
        SM_CXSMICON = 49, // 0x31
        SM_CYSMICON = 50, // 0x32
        SM_CYSMCAPTION = 51, // 0x33
        SM_CXSMSIZE = 52, // 0x34
        SM_CYSMSIZE = 53, // 0x35
        SM_CXMENUSIZE = 54, // 0x36
        SM_CYMENUSIZE = 55, // 0x37
        SM_ARRANGE = 56, // 0x38
        SM_CXMINIMIZED = 57, // 0x39
        SM_CYMINIMIZED = 58, // 0x3A
        SM_CXMAXTRACK = 59, // 0x3B
        SM_CYMAXTRACK = 60, // 0x3C
        SM_CXMAXIMIZED = 61, // 0x3D
        SM_CYMAXIMIZED = 62, // 0x3E
        SM_NETWORK = 63, // 0x3F
        SM_CLEANBOOT = 67, // 0x43
        SM_CXDRAG = 68, // 0x44
        SM_CYDRAG = 69, // 0x45
        SM_SHOWSOUNDS = 70, // 0x46
        SM_CXMENUCHECK = 71, // 0x47
        SM_CYMENUCHECK = 72, // 0x48
        SM_SLOWMACHINE = 73, // 0x49
        SM_MIDEASTENABLED = 74, // 0x4A
        SM_MOUSEWHEELPRESENT = 75, // 0x4B
        SM_XVIRTUALSCREEN = 76, // 0x4C
        SM_YVIRTUALSCREEN = 77, // 0x4D
        SM_CXVIRTUALSCREEN = 78, // 0x4E
        SM_CYVIRTUALSCREEN = 79, // 0x4F
        SM_CMONITORS = 80, // 0x50
        SM_SAMEDISPLAYFORMAT = 81, // 0x51
        SM_IMMENABLED = 82, // 0x52
        SM_CXFOCUSBORDER = 83, // 0x53
        SM_CYFOCUSBORDER = 84, // 0x54
        SM_TABLETPC = 86, // 0x56
        SM_MEDIACENTER = 87, // 0x57
        SM_STARTER = 88, // 0x58
        SM_SERVERR2 = 89, // 0x59
        SM_MOUSEHORIZONTALWHEELPRESENT = 91, // 0x5B
        SM_CXPADDEDBORDER = 92, // 0x5C
        SM_DIGITIZER = 94, // 0x5E
        SM_MAXIMUMTOUCHES = 95, // 0x5F

        SM_REMOTESESSION = 0x1000, // 0x1000
        SM_SHUTTINGDOWN = 0x2000, // 0x2000
        SM_REMOTECONTROL = 0x2001, // 0x2001
    }
    private Timer timer = null;
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {


        timer = new Timer();
        this.timer.Interval = 10000;
        this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_tick);
        timer.Enabled = true;
        Library.writeErrorLog("Service Started !");

    }
    private void timer_tick(object sender , ElapsedEventArgs e)
    {
        int result = GetSystemMetrics(SystemMetric.SM_REMOTESESSION);
        bool isRemoteSession = (result != 0);
        Library.writeErrorLog("System IsOn Remote Session : "+isRemoteSession.ToString()+" !");
        if (isRemoteSession)
        {
            Process yourProcess = new Process();
            yourProcess.StartInfo.FileName = @"C:\Remote.txt";
            yourProcess.Start();
        }

    }

    protected override void OnStop()
    {
        timer.Enabled = false;
        Library.writeErrorLog("Service Stopped !");

    }
}

现在的问题是“isRemoteSession”始终为False, 这是远程会话之前和之后的日志文件内容

4/25/2017 9:13:02 PM : Service Stopped !
4/25/2017 9:13:54 PM : Service Started !
4/25/2017 9:13:57 PM : System IsOn Remote Session : False !
4/25/2017 9:14:00 PM : System IsOn Remote Session : False !
4/25/2017 9:14:03 PM : System IsOn Remote Session : False !
4/25/2017 9:14:06 PM : System IsOn Remote Session : False !
4/25/2017 9:14:09 PM : System IsOn Remote Session : False !
4/25/2017 9:14:12 PM : System IsOn Remote Session : False !
4/25/2017 9:14:16 PM : System IsOn Remote Session : False !
4/25/2017 9:14:19 PM : System IsOn Remote Session : False !
4/25/2017 9:14:22 PM : System IsOn Remote Session : False !
4/25/2017 9:14:25 PM : System IsOn Remote Session : False !
4/25/2017 9:14:28 PM : System IsOn Remote Session : False !
4/25/2017 9:14:31 PM : System IsOn Remote Session : False !
4/25/2017 9:14:34 PM : System IsOn Remote Session : False !
4/25/2017 9:14:37 PM : System IsOn Remote Session : False !
4/25/2017 9:14:40 PM : System IsOn Remote Session : False !
4/25/2017 9:14:43 PM : System IsOn Remote Session : False !
4/25/2017 9:14:46 PM : System IsOn Remote Session : False !
4/25/2017 9:14:49 PM : System IsOn Remote Session : False !
4/25/2017 9:14:52 PM : System IsOn Remote Session : False !
4/25/2017 9:14:55 PM : System IsOn Remote Session : False !
4/25/2017 9:14:58 PM : System IsOn Remote Session : False !
4/25/2017 9:15:01 PM : System IsOn Remote Session : False !
4/25/2017 9:15:04 PM : System IsOn Remote Session : False !
4/25/2017 9:15:07 PM : System IsOn Remote Session : False !
4/25/2017 9:15:10 PM : System IsOn Remote Session : False !
4/25/2017 9:15:13 PM : System IsOn Remote Session : False !
4/25/2017 9:15:16 PM : System IsOn Remote Session : False !
4/25/2017 9:15:19 PM : System IsOn Remote Session : False !
4/25/2017 9:15:22 PM : System IsOn Remote Session : False !
4/25/2017 9:15:25 PM : System IsOn Remote Session : False !
4/25/2017 9:15:27 PM : Service Stopped !
4/25/2017 9:15:38 PM : Service Started !
4/25/2017 9:15:41 PM : System IsOn Remote Session : False !
4/25/2017 9:15:44 PM : System IsOn Remote Session : False !
4/25/2017 9:15:47 PM : System IsOn Remote Session : False !
4/25/2017 9:15:50 PM : System IsOn Remote Session : False !
4/25/2017 9:15:53 PM : System IsOn Remote Session : False !
4/25/2017 9:15:56 PM : System IsOn Remote Session : False !
4/25/2017 9:15:59 PM : System IsOn Remote Session : False !
4/25/2017 9:16:02 PM : System IsOn Remote Session : False !
4/25/2017 9:16:05 PM : System IsOn Remote Session : False !
4/25/2017 9:16:08 PM : System IsOn Remote Session : False !
4/25/2017 9:16:11 PM : System IsOn Remote Session : False !
4/25/2017 9:16:14 PM : System IsOn Remote Session : False !
4/25/2017 9:16:17 PM : System IsOn Remote Session : False !
4/25/2017 9:16:20 PM : System IsOn Remote Session : False !
4/25/2017 9:16:23 PM : System IsOn Remote Session : False !
4/25/2017 9:16:26 PM : System IsOn Remote Session : False !
4/25/2017 9:16:29 PM : System IsOn Remote Session : False !
4/25/2017 9:16:32 PM : System IsOn Remote Session : False !
4/25/2017 9:16:35 PM : System IsOn Remote Session : False !
4/25/2017 9:16:38 PM : System IsOn Remote Session : False !
4/25/2017 9:16:41 PM : System IsOn Remote Session : False !
4/25/2017 9:16:44 PM : System IsOn Remote Session : False !
4/25/2017 9:16:47 PM : System IsOn Remote Session : False !

我尝试测试与Windows窗体应用程序相同的代码,当我进入远程桌面会话时它运行良好

2 个答案:

答案 0 :(得分:1)

如果您事先将其初始化为true,请确保您的变量将更改为false。 请看一下这个问题,特别是Martin Schlott回答。

  

出于安全原因,不允许服务与用户桌面交互。因此,它们在虚拟桌面上运行,与物理桌面无关。

When running as a windows service, how do i get the number of active monitors? C++

答案 1 :(得分:1)

Windows服务在"会话0"。

下运行

在Windows Vista / 7/8/10之前的几天内,第一个登录用户被分配到会话0,第二个被分配到会话1,等等。这意味着允许Windows应用程序在会话0下运行,这是否定的更长的时间。

您可以详细了解会话0隔离here

的影响

您曾经能够使用远程桌面和/admin切换到远程会话0,但我不认为交换机在经过Windows Vista SP 1的操作系统上做任何事情。这表示运行服务的会话0永远不会通过远程桌面访问。

正如@scartag所提到的,您可以have the service interact with the desktop并以这种方式轮询信息(如果您想在桌面会话中启动应用,则无论如何都必须这样做。)

您需要找到一种方法来轮询所有活动Windows会话(因为可以同时登录多个用户)并查看其中是否有任何远程并做出相应反应。

以下是可能在这方面为您提供指导的其他SO帖子的一些链接:

How to get list of all logged in users using

How to get a list of Windows sessions?

How do I get a list of local Windows users who are logged in?