Skip to content

Instantly share code, notes, and snippets.

@mariotaku
Created December 20, 2022 11:09
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mariotaku/8a9811bdc031d3388ceef98c53cce3c4 to your computer and use it in GitHub Desktop.
Save mariotaku/8a9811bdc031d3388ceef98c53cce3c4 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");
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