3

I have this code:

    [PermissionSet(SecurityAction.Assert, Name = "FullTrust")]
    public List<WinInfo> GetWindows()
    {
        try
        {
            var isFullTrust = Assembly.GetExecutingAssembly().IsFullyTrusted;
            if (isFullTrust)
            {
                return Process.GetProcesses().Where(z => !string.IsNullOrEmpty(z.MainWindowTitle))
                    .Select(z => new WinInfo
                        {
                            ProcessID = z.Id,
                            ProcessName = z.ProcessName,
                            WinID = z.MainWindowHandle,
                            WindowTitle = z.MainWindowTitle
                        }).ToList();
            }
            else
                return null;
        }
        catch (Exception ex)
        {
            Trace.Write(ex.Message);
            return null;
        }
    }

When I test in on my local computer under my current user (with admin rights) it works ok, displaying all the processes, that have windows. But when I call this code from a windows service, run under "Local Service" account, then the list is empty. I attached to the process, and through debug I found that "Process.GetProcesses()" returns all the processes, but all of them have MainWindowHandle as 0 and MainWindowTitle as empty, even when they do have windows. So what is wrong with my code?

Edit I edited code, so that it checks the assembly for full trust and have PemmissionSet that should grant the code the neccessary rights. Still the result is the same. When I debug, I can see, that "isFullTrust" is "True" and code executes with no exceptions. Still the list is empty, because none of the processes contains not-empty MainWindowTitle

HellyBlack
  • 33
  • 4

3 Answers3

1

Surely you need to run that under the user account! Why would applications with open windows be running under the local system account? That's for windows services etc

It could also be related to your process requiring full trust

From MSDN: The Process class has a LinkDemand and an InheritenceDemand for FullTrust on it. This means that if your assembly is not fully trusted, it will be unable to kick off new Processes or get information about running processes

Dave Lawrence
  • 3,843
  • 2
  • 21
  • 35
  • Applications are running under the user account. Its only service, that gets information about them, run under Local System account – HellyBlack Jul 16 '12 at 13:18
  • do you need to run service under local system account? Try running under your user account; if that works; you may find something to help here http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/a463efa3-3b4e-427b-86f3-8501728b6f28 and here http://blogs.msdn.com/b/shawnfa/archive/2005/12/14/502826.aspx – Dave Lawrence Jul 16 '12 at 13:30
  • I' ve tried to run it under user account - result is the same. And see my Edit - it's not the issue with pemmisions. – HellyBlack Jul 17 '12 at 06:36
1

According to this thread :

The problem you're seeing is because by default service don't have access to any interactive desktops. I don't recommend interacting with the desktop from a service (#1, there may not be any desktop, #2 there may be multiple desktops, #3 interacting with the desktop from service in Vista is not implemented) but, you can check the "Interace with desktop" in your services properties.

maybe you can try to create an hidden form?

user844541
  • 2,868
  • 5
  • 32
  • 60
0

Maybe this is a question of priviliges. According to this link LocalService has minimum privileges on the local computer.

you should use Local system Account

user844541
  • 2,868
  • 5
  • 32
  • 60