批处理:是否有将 %errorlevel% 转换为 HResult 的函数?

dph*_*c23 0 batch-file

我想从 %errorlevel% 得到 HResult。我尝试使用此命令安装错误的产品密钥

wmic path SoftwareLicensingService WHERE (Version is not null) call InstallProductKey ProductKey='xyz'>nul 2>&1
Run Code Online (Sandbox Code Playgroud)

%errorlevel% 现在返回-1073418160。将其转换为十六进制并在其前面添加“0x”。我得到了0xC004F050

我尝试使用此命令:powershell "\"0x{0:X}\" -F %errorlevel%. 但我chcp 65001在第一行。因此,我的控制台运行后并不平坦。无论如何,是否可以在不执行 Powershell 的情况下将 %errorlevel% 转换为 HResult? 在此处输入图片说明

“Xin chào”是越南人的传统问候语

Ste*_*han 5

来自DosTips

call cmd /c exit /b -1073418160
echo %=exitcode%
Run Code Online (Sandbox Code Playgroud)

结果:

C004F050
Run Code Online (Sandbox Code Playgroud)

所以你可能想要:

call cmd /c exit /b %errorlevel%
echo 0x%=exitcode%
Run Code Online (Sandbox Code Playgroud)