Call WslRegisterDistribution function from wslapi.dll in PowerShell

Viewed 1152

I am making a PowerShell script to install any Linux userspace from compressed tarballs (.tar.gz). But it always return some number like this 2147943746. I am using WslRegisterDistribution function from wslapi.dll. The syntax in C++ is as follows:

HRESULT WINAPI WslRegisterDistribution(
  _In_ PCWSTR distroName,
  _In_ PCWSTR tarGzFilename
);

My PowerShell script is as follows:

$X=Read-Host "Enter Distro Name"
$Y=Read-Host "Enter .tar.gz file path"
Write-Host "Distro name '$X' and file path '$Y'"
pause

$MethodDefinition=@'
[DllImport("wslapi.dll", CharSet = CharSet.Unicode)]
public static extern uint WslRegisterDistribution(string distributionName, string tarGzFilename);
'@

$WslApi=Add-Type -MemberDefinition $MethodDefinition -Name 'Wslapi' -PassThru
$WslApi::WslRegisterDistribution("$X", "$Y");

I can call that method using C++/C# but I can not do that in PowerShell. In C# I import the function and pass two command arguments in its parameter shown as below:

[DllImport("wslapi.dll", CharSet = CharSet.Unicode)]
public static extern uint WslRegisterDistribution(string distributionName, string tarGzFilename);

Is that possible to import that in PowerShell? How can I corrent the script to do that? See my alternative GitHub project https://github.com/Biswa96/WSLInstall.

Further readings:

0 Answers
Related