Skip to content

Instantly share code, notes, and snippets.

@pnck
Forked from mariotaku/LspWslFix.ps1
Last active April 15, 2023 08:07
Show Gist options
  • Save pnck/5a99adc0b443941b07db198f238de004 to your computer and use it in GitHub Desktop.
Save pnck/5a99adc0b443941b07db198f238de004 to your computer and use it in GitHub Desktop.
Fix WSL Connection Issue Caused by Winsock
#Requires -RunAsAdministrator
# Fix for https://github.com/microsoft/WSL/issues/4177
$MethodDefinition = @'
[DllImport("ws2_32.dll", CharSet = CharSet.Unicode)]
public static extern int WSCSetApplicationCategory([MarshalAs(UnmanagedType.LPWStr)] string Path, uint PathLength, [MarshalAs(UnmanagedType.LPWStr)] string Extra, uint ExtraLength, uint PermittedLspCategories, out uint pPrevPermLspCat, out int lpErrno);
'@
$Ws2Spi = Add-Type -MemberDefinition $MethodDefinition -Name 'Ws2Spi' -PassThru
$WslLocation = Get-AppxPackage MicrosoftCorporationII.WindowsSubsystemForLinux | Select-Object -expand InstallLocation
$Executables = ("wsl.exe", "wslservice.exe", "wslg.exe", "wslhost.exe");
foreach ($Exe in $Executables) {
$ExePath = "${WslLocation}\${Exe}";
$ExePathLength = $ExePath.Length;
$PrevCat = $null;
$ErrNo = $null;
if ($Ws2Spi::WSCSetApplicationCategory($ExePath, $ExePathLength, $null, 0, [uint32]"0x80000000", [ref] $PrevCat, [ref] $ErrNo) -eq 0) {
Write-Output "Added $ExePath!";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment