You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Why System.Diagnostics.Process.MainWindowTitle returns empty string on Linux? #31658
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)}");
}
}
}
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.
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.
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.
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.
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.
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.
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.
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
Activity
stephentoub commentedon Feb 3, 2020
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:
runtime/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Unix.cs
Line 1001 in 5b30529
If you can suggest something better, it can be changed accordingly.
Entomy commentedon Feb 3, 2020
Would this be helpful?
https://stackoverflow.com/questions/45625222/c-get-windows-title-using-process-name
stephentoub commentedon Feb 3, 2020
Thank you, but no, that's about Windows, and we already have a solution for Windows:
runtime/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Win32.cs
Line 205 in 5b30529
Entomy commentedon Feb 3, 2020
That's what I get for trying to read C++ code 😅
Entomy commentedon Feb 3, 2020
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_NAMEand_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.Symbai commentedon Feb 3, 2020
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
stephentoub commentedon Feb 3, 2020
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.
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.Entomy commentedon Feb 3, 2020
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.
Entomy commentedon Feb 3, 2020
Maybe there's something available in
/proc? I've never done a deep dive into that though.stephentoub commentedon Feb 3, 2020
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.
Windowing information is not surfaced in procfs.
Entomy commentedon Feb 3, 2020
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.
stephentoub commentedon Feb 3, 2020
Right :-(
wfurt commentedon Feb 3, 2020
cc: @tmds
Symbai commentedon Feb 3, 2020
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:
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.
stephentoub commentedon Feb 3, 2020
That's why I suggested a PR could be submitted to the docs repo, which @Entomy kindly did.
Entomy commentedon Feb 3, 2020
Yup, Stephen and I put it together here.
Update Process.xml (#3844)
[-]System.Diagnostics.Process.MainWindowTitle does not work on Linux[/-][+]Why System.Diagnostics.Process.MainWindowTitle returns empty string on Linux?[/+]adamsitnik commentedon Jun 25, 2020
The docs have been updated in dotnet/dotnet-api-docs#3844, closing.