我正在制作一个PowerShell脚本来从压缩的tarball(.tar.gz)安装任何Linux用户空间.但它总会返回这样的数字2147943746.我正在使用WslRegisterDistribution函数wslapi.dll.C++中的语法如下:
HRESULT WINAPI WslRegisterDistribution(
_In_ PCWSTR distroName,
_In_ PCWSTR tarGzFilename
);
Run Code Online (Sandbox Code Playgroud)
我的PowerShell脚本如下:
$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");
Run Code Online (Sandbox Code Playgroud)
我可以使用C++/C#调用该方法,但我无法在PowerShell中执行此操作.在C#中我导入函数并在其参数中传递两个命令参数,如下所示:
[DllImport("wslapi.dll", CharSet = CharSet.Unicode)]
public static extern uint WslRegisterDistribution(string distributionName, string tarGzFilename);
Run Code Online (Sandbox Code Playgroud)
可以在PowerShell中导入吗?我该如何更正脚本来做到这一点?请参阅我的替代GitHub项目https://github.com/Biswa96/WSLInstall.
cpuid在Windows X86中,可以通过内在函数查询CPU品牌。这是代码示例:
#include <stdio.h>
#include <intrin.h>
int main(void)
{
int cpubrand[4 * 3];
__cpuid(&cpubrand[0], 0x80000002);
__cpuid(&cpubrand[4], 0x80000003);
__cpuid(&cpubrand[8], 0x80000004);
char str[48];
memset(str, 0, sizeof str);
memcpy(str, cpubrand, sizeof cpubrand);
printf("%s\n", str);
}
Run Code Online (Sandbox Code Playgroud)
Windows ARM64 中的替代方案是什么?
让我们有这样的结构:
struct _Example {
int a;
int b;
int c;
} Example;
Run Code Online (Sandbox Code Playgroud)
一个函数通过指针获取此结构并输出struct成员的值.我想打印这样的值:
Example example;
func(&example);
if(example.a)
printf("%d", example.a);
if(example.b)
printf("%d", example.b);
if(example.c)
printf("%d", example.c);
Run Code Online (Sandbox Code Playgroud)
如何if用循环替换这些多个条件?
这是我的代码示例:
void MyFunc()
{
if(funcA(varA) == 0)
{
if(funcB(varB) == 0)
{
if(funcC(varC) == 0)
{
//Success funcC
}
else
{
//error with funcC
}
}
else
{
//error with funcB
}
}
else
{
//error with funcA
}
}
Run Code Online (Sandbox Code Playgroud)
所以代码就像是,如果funcA()只返回零,则执行funcB(),如果funcB()仅返回零,则执行funcC(),依此类推.我的问题是如何if...else用其他优化方法替换嵌套语句?