Not all info about process in Process.GetProcesses when run in service by Local Service account

有些话、适合烂在心里 提交于 2019-12-11 13:22:43

问题


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


回答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?




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/11504441/not-all-info-about-process-in-process-getprocesses-when-run-in-service-by-local

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!