Reputation: 163
When attempting to access a WSL distribution through the wslapi within a WPF application, it indicates that the distribution is not registered, even though it is definitely registered. (For example, I can launch it from powershell, etc.)
The issue seems to only occur in WPF applications (or, at least it doesn't occur in console applications).
This issue suddenly appeared two days ago, when it did not exist before.
I have a program that interacts with a custom Linux distribution in WSL. The distro is not important; what is important is this:
The program, upon startup, uses the wslapi (dll imports) to detect whether the distribution is installed. If it's installed, then it launches the program. If not, it diverts to a wizard to load the wsl distro.
A couple of days ago, the INSTALLED version of the program (so we're NOT talking about code changes) suddenly stopped detecting that the WSL distro is installed. This is indicated by always diverting to the wizard even though the distro is installed.
Since then, I've been able to narrow down the circumstances of the failure to detect installation to cases where I'm NOT in a console application.
To troubleshoot this, I created a single WslLink class library, which has a WslService class, which executes this code to detect whether a distro is registered:
[DllImport("wslapi.dll", CharSet = CharSet.Unicode)]
public static extern bool WslIsDistributionRegistered(string distributionName);
...
bool IsInstalled()
{
var isRegistered = WslIsDistributionRegistered("MyLinux");
return isRegistered;
}
This is the same code that was working in the installed application up to two days ago.
In a bare-bones .NET Framework WPF App, which refrences the WslLink project described above, I have the following code in App.OnStartup() (having removed the StartupUri setting from the xaml):
var wslService = new WslService();
if (wslService.IsInstalled())
{
MessageBox.Show("The MyLinux is installed");
}
else
{
MessageBox.Show("The MyLinux is not installed");
}
This code posts a message box with "The MyLinux is not installed" -- indicating that the distro is not registered according to wslapi.
In a similar bare-bones .NET Framework Console App in the same solution, and referencing the same WslLink project, I have:
var wslService = new WslService();
if (wslService.IsInstalled())
{
Console.WriteLine("The MyLinux is installed");
}
else
{
Console.WriteLine("The MyLinux is not installed");
}
This outputs "The MyLinux is installed" -- indicating that the distro IS registered according to wslapi.
Well, either the distro is registered or it is not, and based on the fact that I can launch it from powershell, and commands like wslconfig /l shows the distro, it clearly is.
In either case, though, since the console app and the wpf app are using the SAME CLASS LIBRARY to invoke the wslapi registration check, I would expect both of them to yield the same result--either both saying "not installed" or both saying "installed".
But, instead, the wpf app always fails to detect the installation and the console app always detects the installation.
To re-iterate (I'm beating this drum really hard, because it just doesn't make sense): The INSTALLED wpf app that was running identical code had been working for months, and then suddenly, a couple of days ago, it stopped working. I'm not talking about a re-install, or anything like that. I'm talking about starting it up one day and suddenly it's not detecting the installation anymore.
Does anyone know anything that might have changed in Windows to cause this?
Upvotes: 0
Views: 161
Reputation: 163
I found out that automatic Windows update KB4493464 breaks wslapi calls from WPF applications. This is why the installed program just suddenly stopped working. I verified this with A/B/A testing (backing out the update to verify, then letting it re-apply).
Thanks for the suggestion, @Biswapriyo, but the LxssManager was running in all cases, which is why the console app was able to work correctly.
I've notified Microsoft, but I don't expect action to fix this soon. I'm sure only console application use cases were considered for this API, so the fact that we can't use it from a GUI application is probably not a priority.
Upvotes: 1