Skip to content

Why System.Diagnostics.Process.MainWindowTitle returns empty string on Linux? #31658

Closed

Description

@Symbai

System.Diagnostics.Process.MainWindowTitle is empty for all processes on Linux, even when the application has root rights. Tested on Ubuntu 19.10 64bit with .NET Core 3.1 console application.

Repro

sudo ./ConsoleApp2

class Program
    {
        static void Main(string[] args)
        {
            var procs = Process.GetProcesses();
            foreach (var proc in procs)
            {
                Console.WriteLine($"Process: {proc.ProcessName}, MainWindowTitle: {(string.IsNullOrEmpty(proc.MainWindowTitle) ? "<EMPTY>" : proc.MainWindowTitle)}");
            }
        }
    }

Result:

image

Activity

  1. stephentoub commented on Feb 3, 2020

    @stephentoub
    Member

    ?

    From where do you recommend Process get the MainWindowTitle?

    By design it is currently hardcoded to return string.Empty for lack of something better and general enough:

    public string MainWindowTitle => string.Empty;

    If you can suggest something better, it can be changed accordingly.

  2. added
    question
    on Feb 3, 2020
  3. stephentoub commented on Feb 3, 2020

    @stephentoub
    Member

    Would this be helpful?

    Thank you, but no, that's about Windows, and we already have a solution for Windows:

    int length = Interop.User32.GetWindowTextLengthW(handle);

  4. Entomy commented on Feb 3, 2020

    @Entomy

    That's what I get for trying to read C++ code 😅

  5. Entomy commented on Feb 3, 2020

    @Entomy

    I think you should be able to get it using XFetchName() with the caveat that it requires the WM be EWMH compliant and support _NET_WM_VISIBLE_NAME and _NET_WM_NAME. Most WM's are, and the ones that aren't are unlikely to be used by the same crowd that'd be using .NET.

  6. Symbai commented on Feb 3, 2020

    @Symbai
    Author

    By design it is currently hardcoded to return string.Empty for lack of something better and general enough:

    Then you should update the docs to reflect that? https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.mainwindowtitle?view=netcore-3.1

  7. stephentoub commented on Feb 3, 2020

    @stephentoub
    Member

    I think you should be able to get it using XFetchName()

    That depends on X11. If we could do it without taking an additional statically-linked dependency / light-up only if it's available at run-time, and if it would address the issue for enough consumers, it's an option.

    Then you should update the docs to reflect that?

    This is following suit with the docs stating If the associated process does not have a main window (so that MainWindowHandle is zero), MainWindowTitle is an empty string (""). But they could certainly be augmented to say that on some platforms we may not be able to determine this and so similarly return an empty string. The docs are open source at https://github.com/dotnet/dotnet-api-docs. You are welcome and encouraged to submit an issue and/or PR. Thanks.

  8. Entomy commented on Feb 3, 2020

    @Entomy

    If the associated process does not have a main window (so that MainWindowHandle is zero), MainWindowTitle is an empty string ("").

    My conclusion from this is that if you get an empty string, there shouldn't be a main window. I don't believe this is "following suit with the docs". I put in a PR for the docs.

  9. Entomy commented on Feb 3, 2020

    @Entomy

    That depends on X11. If we could do it without taking an additional statically-linked dependency / light-up only if it's available at run-time, and if it would address the issue for enough consumers, it's an option.

    Maybe there's something available in /proc? I've never done a deep dive into that though.

  10. stephentoub commented on Feb 3, 2020

    @stephentoub
    Member

    My conclusion from this is that if you get an empty string, there shouldn't be a main window. I don't believe this is "following suit with the docs".

    Now we're arguing over semantics, but my point was that it's currently saying string.Empty is returned if there isn't a main window, and the difference between platforms is how it determines that. Note that MainWindowHandle is returning zero, as is called out in that sentence.

    Maybe there's something available in /proc?

    Windowing information is not surfaced in procfs.

  11. Entomy commented on Feb 3, 2020

    @Entomy

    That depends on X11. If we could do it without taking an additional statically-linked dependency / light-up only if it's available at run-time, and if it would address the issue for enough consumers, it's an option.

    And now thinking about this, it only solves the problem for a subset of Unix users, and an ever decreasing subset at that. Between whatever macOS is calling their windowing system now, Wayland, Mir, and TWIN, there's a lot of competing systems in that space, and no clear way to universally get the window title.

    I don't envy you.

  12. stephentoub commented on Feb 3, 2020

    @stephentoub
    Member

    it only solves the problem for a subset of Unix users, and an ever decreasing subset at that.

    Right :-(

  13. wfurt commented on Feb 3, 2020

    @wfurt
    Member

    cc: @tmds

  14. added
    os-linux
    on Feb 3, 2020
  15. Symbai commented on Feb 3, 2020

    @Symbai
    Author

    This is following suit with the docs stating If the associated process does not have a main window (so that MainWindowHandle is zero), MainWindowTitle is an empty string ("").

    If I have two windows with two window titles on Windows and Linux and in one it returns an empty string and in the other the window title, then my first assumption is that it is a bug. Then I go check the docs and the docs say, you quoted it yourself does not have a main window [...] my assumption stays. Because the Linux process clearly has a main window, just like the Windows process.

    In order to know it is not a bug but a design choice I would either:

    • Be a Linux expert knowing there is no such native API to retreive the window title
    • Be a C# runtime expert knowing that the source simply returns string.empty for all processes in all cases when running on linux.

    But most people are not and it is not clear for us at first place and it doesn't hurt to add a single additional line to the docs, thank you.

  16. stephentoub commented on Feb 3, 2020

    @stephentoub
    Member

    it doesn't hurt to add a single additional line to the docs

    That's why I suggested a PR could be submitted to the docs repo, which @Entomy kindly did.

  17. Entomy commented on Feb 3, 2020

    @Entomy

    Yup, Stephen and I put it together here.

  18. added a commit that references this issue on Mar 2, 2020
  19. changed the title [-]System.Diagnostics.Process.MainWindowTitle does not work on Linux[/-] [+]Why System.Diagnostics.Process.MainWindowTitle returns empty string on Linux?[/+] on Jun 25, 2020
  20. adamsitnik commented on Jun 25, 2020

    @adamsitnik
    Member

    The docs have been updated in dotnet/dotnet-api-docs#3844, closing.

  21. ghost locked as resolved and limited conversation to collaborators on Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Diagnostics.Processos-linuxquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions