Getting window titles from a windows service

后端 未结 3 2101
庸人自扰
庸人自扰 2020-12-21 11:55

My company is using an unfortunate third party product that requires a user to be left logged on to an XP workstation running two instances of it\'s product.

I was t

相关标签:
3条回答
  • 2020-12-21 12:34

    I don't think you can do that with the .net framework...

    but you can with WinAPI: http://msdn.microsoft.com/en-us/library/ms633497%28v=vs.85%29.aspx

    and you can call it from C# like so: http://www.pinvoke.net/default.aspx/user32/enumwindows.html

    An important note: your process need to have permissions for this...


    you'll also probably need this: http://msdn.microsoft.com/en-us/library/ms633520(v=vs.85).aspx http://www.pinvoke.net/default.aspx/user32.getwindowtext

    0 讨论(0)
  • 2020-12-21 12:37

    Services run in session 0, the non-interactive session for services. Desktop applications run in different sessions. Session 1 for the first interactive user, session 2 for the second and so on. Inside the sessions are window stations, and inside window stations are desktops.

    Window stations are secured and isolated. That means that a process in one window station cannot access the objects of another. This means that your service cannot use GetWindowText or indeed any function or message to interact with a window in a different window station.

    If you need to transmit this information between the interactive desktops and your service, then you need:

    1. A process running in the interactive desktop, and
    2. An IPC channel to communicate between that desktop process and the service.

    In short, you are going to need to build a bit more scaffolding to get this to work.

    0 讨论(0)
  • 2020-12-21 12:44
    using System.Runtime.InteropServices;
    using System.Text;
    

    ...

    [DllImport("user32.dll")]
    static extern int GetWindowText(int hWnd, StringBuilder text, int count); 
    

    ...

    Process[] processList = Process.GetProcesses();
    
    List<string> windowTitles = new List<string>();
    
    foreach (Process proc in processList)
    {
        StringBuilder Buff = new StringBuilder(256);
    
        if (GetWindowText(proc.MainWindowHandle.ToInt32(), Buff, 256) > 0 )
        {
            windowTitles.Add(Buff.ToString());
        }
    }
    
    0 讨论(0)
提交回复
热议问题