Platform invoke 乱码

本文介绍了一个在跨平台调用中遇到的字符串乱码问题,并详细解释了解决方案。通过调整C#中字符串的编码格式匹配API的编码设置,成功避免了乱码现象。

最近在做一个关于智能卡的项目的时候,遇到一个乱码的问题,具体情况是这样的,调用API的一个方法的时候,在传递的一个结构体变量中有字符串类型的变量,该变量从API中返回的时候变成了乱码,而和它同一个结构体的整型值却是个正常的值。

typedef struct {
   LPSTR ErrorNumber;           /* Error Number */
   LPSTR ErrorString;           /* Description of the error */
   LPSTR ResolutionString;      /* Description of the resolution for the error. */
   DWORD HelpID;                 /* help identifier for the error */
} CARD_ERROR_INFO_1_A, *PCARD_ERROR_INFO_1_A, FAR *LPCARD_ERROR_INFO_1_A;

#ifndef HOST_16BIT
typedef struct {
   LPWSTR ErrorNumber;           /* Error Number */
   LPWSTR ErrorString;           /* Description of the error */
   LPWSTR ResolutionString;      /* Description of the resolution for the error. */
   DWORD HelpID;                 /* help identifier for the error */
} CARD_ERROR_INFO_1_W, *PCARD_ERROR_INFO_1_W, FAR *LPCARD_ERROR_INFO_1_W;
#endif

 

#if defined _UNICODE || defined UNICODE

#define LPCARD_ERROR_INFO_1 LPCARD_ERROR_INFO_1_W

#else

#define LPCARD_ERROR_INFO_1 LPCARD_ERROR_INFO_1_A

 

BOOL ICEAPI GetCardPrinterErrors (LPTSTR lpPrinterName,DWORD level,LPCARD_ERROR_INFO_1 lpbErrorInfo, DWORD cbBuf, LPDWORD lpdwNeeded,LPDWORD lpdwError);

 

下面是对应的C#的方法,在转化前,先要搞清楚几个数据类型,从WinNT.h 中可以看到

LPSTR 实际上就是 char* ,LPWSTR = WCHAR*, WCHAR是Unicode 类型的字符(用2Bytes分配一个字符),而ANSI则用一个Byte来分配字符。char就是Ansi类型。

为了实验方便,我把上面的方法的参数变成一个,LPCARD_ERROR_INFO_1 lpbErrorInfo

 

还有C#部分的关于MarshalAs 和structlayout 的知识

1.基础部分
MarshalAs是什么?它可以用来做什么?
MarshalAs 这个属性用来指示如何在托管代码和非托管代码之间封送数据,可以用于参数,字段或者返回值.该属性为可选属性,因为每个数据类型都有默认的封送处理行为.仅在可以将给定类型封送到多个类型时需要此属性.(MSDN).从这里看,并不是每个p/invoke 或者 com invoke都要用到MarshalAS的.

 

StructLayoutAttribute 类使用户可以控制类或结构的数据字段的物理布局.

通常,公共语言运行库控制类或结构的数据字段在托管内存中的物理布局。如果类或结构需要按某种方式排列,则可以使用 StructLayoutAttribute。如果要将类传递给需要指定布局的非托管代码,则显式控制类布局是重要的。LayoutKind 值 Sequential 用于强制将成员按其出现的顺序进行顺序布局。Explicit 控制每个数据成员的精确位置。如果使用 Explicit,则每个成员必须使用 FieldOffsetAttribute 来指示该字段在类型中的位置.(MSDN)

 

 

[StructLayout(LayoutKind.Sequential)]
public struct testAttr

{
        [MarshalAs(UnmanagedType.LPTStr)]
        public string ErrorNumber;

        [MarshalAs(UnmanagedType.LPTStr)]
        public string ErrorString;

        [MarshalAs(UnmanagedType.LPTStr)]
        public string ResolutionString;

        public UInt32 HelpID;
}

 

[DllImport("PlatformCPlusPlus.dll", EntryPoint = "GetCardPrinterErrors")]
extern static int GetCardPrinterErrors(ref testAttr ta);

 

一开始这样做的时候是出现乱码的,后来仔细看下,原来 LPTSTR在winnt ,winxp中的系统中都是Unicode编码,所以会出现乱码,默认的是用Ansi的,最后的解决方法是,将 [MarshalAs(UnmanagedType.LPTStr)]去掉就可以了

 

下面是实验用的代码

 

#ifdef PLATFORMCPLUSPLUS_EXPORTS
#define ICEAPI __declspec(dllexport)
#else
#define ICEAPI __declspec(dllimport)
#endif


extern "C"
{
 typedef struct
 {
  LPSTR ErrorNumber;
  LPSTR ErrorString;
  LPSTR ResolutionString;
  DWORD HelpID;
 }*LPCARD_ERROR_INFO_1;

ICEAPI BOOL GetCardPrinterErrors1A(LPCARD_ERROR_INFO_1);

}

 

ICEAPI BOOL GetCardPrinterErrors1A
(
 LPCARD_ERROR_INFO_1 lpCard
)
{
 char* temp = (char*)CoTaskMemAlloc(sizeof(char) * strlen(lpCard->ErrorString) + 6);
 strcpy_s(temp,sizeof(char) * (strlen(lpCard->ErrorString) + 6),"test:");
 lpCard->ErrorString = temp;
 lpCard->ErrorNumber = temp;
 lpCard->ResolutionString = temp;
 lpCard->HelpID = 123456;
 return true;

}

 

 

[DllImport("PlatformCPlusPlus.dll", EntryPoint = "GetCardPrinterErrors1A", CharSet = CharSet.Ansi)]
        extern static int GetCardPrinterErrors(ref testAttr ta);

 

static void Main(string[] args)
        {

            testAttr taa = new testAttr();
            taa.ErrorNumber = string.Empty;
            taa.ErrorString = string.Empty;
            taa.HelpID = 0;
            taa.ResolutionString = string.Empty;

            GetCardPrinterErrors(ref taa);

            Console.WriteLine(taa.ErrorNumber);
            Console.WriteLine(taa.ErrorString);
            Console.WriteLine(taa.ResolutionString);
            Console.WriteLine(taa.HelpID.ToString());

            Console.Read();
        }

所以,以后做类似的平台调用涉及到string的都必须要指定该string用的是什么样的编码格式,否则,会出现乱码的情况
而且要明白一点,所提高api的平台,支持的是什么编码格式。

PS C:\Users\Administrator> # CurlTools.psm1 >> # ============== >> # 完整修复的模块代码 >> >> # 模块初始化块 >> $script:Config = $null >> $script:CurlPath = $null >> $script:ConfigDir = $null >> $script:ConfigPath = $null >> $script:ModuleInitialized = $false >> >> # 检测操作系统 >> function Get-Platform { >> if ($PSVersionTable.PSEdition -eq "Core") { >> if ($IsWindows) { "Windows" } >> elseif ($IsLinux) { "Linux" } >> elseif ($IsMacOS) { "macOS" } >> } else { >> if ($env:OS -like "Windows*") { "Windows" } >> elseif (Test-Path "/etc/os-release") { "Linux" } >> else { "macOS" } >> } >> } >> >> # 获取配置目录 >> function Get-ConfigDir { >> $platform = Get-Platform >> switch ($platform) { >> "Linux" { "$HOME/.config/curltools" } >> "macOS" { "$HOME/Library/Application Support/CurlTools" } >> default { "$env:APPDATA\CurlTools" } >> } >> } >> >> # 核心初始化函数 >> function Initialize-Module { >> try { >> # 设置配置目录 >> $script:ConfigDir = Get-ConfigDir >> if (-not (Test-Path $script极乐净土:ConfigDir)) { >> New-Item -ItemType Directory -Path $script:ConfigDir -Force | Out-Null >> } >> $script:ConfigPath = Join-Path $script:ConfigDir "config.json" >> >> # 加载或创建配置 >> if (Test-Path $script:ConfigPath) { >> $script:Config = Get-Content $script:ConfigPath | ConvertFrom-Json >> } >> >> if (-not $script:Config) { >> $script:Config = [PSCustomObject]@{ >> CurlPath = $null >> LastUpdate = (Get-Date).ToString("o") >> AutoUpdate = $true >> } >> } >> >> # 查找或安装curl >> if (-not $script:Config.CurlPath -or -not (Test-Path $script:Config.CurlPath)) { >> $script:Config.CurlPath = Find-CurlPath >> } >> >> # 保存配置 >> $script:Config | ConvertTo-Json | Set-Content $script:ConfigPath -Force >> $script:CurlPath = $script:Config.CurlPath >> $script:ModuleInitialized = $true >> >> return $true >> } catch { >> Write-Warning "模块初始化失败: $_" >> return $false >> } >> } >> >> # 查找curl路径 >> function Find-CurlPath { >> $platform = Get-Platform >> $exeName = if ($platform -eq "Windows") { "curl.exe" } else { "curl" } >> >> # 检查系统PATH >> $pathCurl = Get-Command $exeName -ErrorAction SilentlyContinue >> if ($pathCurl) { >> return $pathCurl.Source | Split-Path >> } >> >> # 平台特定路径 >> $searchPaths = switch ($platform) { >> "Windows" { >> @( >> "C:\Windows\System32", >> "C:\Program Files\curl\bin", >> "${env:ProgramFiles}\Git\usr\bin" >> ) >> } >> "Linux" { @("/usr/bin", "/usr/local/bin") } >> "macOS" { @("/usr/local/bin", "/opt/homebrew/bin") } >> } >> >> foreach ($path in $searchPaths) { >> $fullPath = Join-Path $path $exeName >> if (Test-Path $fullPath) { >> return $path >> } >> } >> >> # 终极方案:自动安装 >> return Install-Curl >> } >> >> # curl安装函数 >> function Install-Curl { >> $platform = Get-Platform >> try { >> if ($platform -eq "Windows") { >> $tempDir = [System.IO.Path]::GetTempPath() >> $curlZip = Join-Path $tempDir "curl.zip" >> $curlUrl = "https://curl.se/windows/dl-8.8.0_5/curl-8.8.0_5-win64-mingw.zip" >> >> # 使用安全下载函数 >> Invoke-SecureDownload -Url $curlUrl -OutputPath $curlZip >> >> Expand-Archive $curlZip -DestinationPath $tempDir -Force >> >> # 验证安装目录是否存在 >> $installDir = Join-Path $tempDir "curl-8.8.0_5-win64-mingw\bin" >> if (-not (Test-Path $installDir)) { >> throw "安装目录不存在: $installDir" >> } >> >> return $installDir >> } >> else { >> if ($platform -eq "Linux") { >> if (Get-Command apt -ErrorAction SilentlyContinue) { >> Start-Process -Wait -FilePath "sudo" -ArgumentList "apt", "update" >> Start-Process -Wait -FilePath "sudo" -ArgumentList "apt", "install", "-y", "curl" >> } elseif (Get-Command yum -ErrorAction SilentlyContinue) { >> Start-Process -Wait -FilePath "sudo" -ArgumentList "yum", "install", "-y", "curl" >> } >> return "/usr/bin" >> } >> else { >> if (-not (Get-Command brew -ErrorAction SilentlyContinue)) { >> # 使用安全下载安装Homebrew >> $installScript = Join-Path $env:TEMP "brew_install.sh" >> Invoke-SecureDownload -Url "https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh" -OutputPath $installScript >> & "/bin/bash" $installScript >> } >> Start-Process -Wait -FilePath "brew" -ArgumentList "install", "curl" >> return "/usr/local/bin" >> } >> } >> } catch { >> throw "无法自动安装curl: $_" >> } >> } >> >> # ========== 公共函数 ========== >> function Get-CurlPath { >> Ensure-ModuleInitialized >> return $script:CurlPath >> } >> >> function Set-CurlPath { >> param( >> [Parameter(Mandatory=$true)] >> [ValidateScript({ >> # 允许设置不存在的路径(创建前) >> if (-not (Test-Path $_)) { >> Write-Warning "路径不存在,将在设置后创建: $_" >> $true >> } else { >> $true >> } >> })] >> [string]$Path >> ) >> >> Ensure-ModuleInitialized >> >> # 确保路径存在 >> if (-not (Test-Path $Path)) { >> New-Item -ItemType Directory -Path $Path -Force | Out-Null >> } >> >> $script:CurlPath = $Path >> $script:Config.CurlPath = $Path >> $script:Config | ConvertTo-Json | Set-Content $script:ConfigPath -Force >> Write-Host "已设置curl路径: $Path" -ForegroundColor Green >> >> # 验证路径下是否有curl可执行文件 >> $exeName = if ((Get-Platform) -极乐净土eq "Windows") { "curl.exe" } else { "curl" } >> $curlExe = Join-Path $Path $exeName >> if (-not (Test-Path $curlExe)) { >> try { >> Write-Warning "路径 $Path 下找不到 $exeName,尝试自动安装..." >> $script:Config.CurlPath = Install-Curl >> $script:CurlPath = $script:Config.CurlPath >> $script:Config | ConvertTo-Json | Set-Content $script:Config极乐净土Path -Force >> Write-Host "已自动安装curl到: $script:CurlPath" -ForegroundColor Green >> >> # 再次验证安装结果 >> $curlExe = Join-Path $script:CurlPath $exeName >> if (-not (Test-Path $curlExe)) { >> throw "自动安装后仍未找到curl可执行文件" >> } >> } catch { >> throw "自动安装失败: $_" >> } >> } >> } >> >> function Get-CurlVersion { >> Ensure-ModuleInitialized >> $exe = if ((Get-Platform) -eq "Windows") { "curl.exe" } else { "curl" } >> $curlExe = Join-Path $script:CurlPath $exe >> >> # 检查可执行文件是否存在 >> if (-not (Test-Path $curlExe)) { >> throw "在路径 $script:CurlPath 下找不到 $exe,请使用Set-CurlPath设置正确的路径或重新初始化模块" >> } >> >> & $curlExe --version | Select-Object -First 1 >> } >> >> function Invoke-SecureDownload { >> param( >> [Parameter(Mandatory=$true)] >> [string]$Url, >> >> [Parameter(Mandatory=$true)] >> [string]$OutputPath, >> >> [string]$ExpectedHash, >> [string]$HashAlgorithm = "SHA256" >> ) >> >> # 添加重试机制 >> $maxRetries = 3 >> $retryCount = 0 >> $success = $false >> >> do { >> try { >> Write-Verbose "下载文件: $Url (尝试 #$($retryCount + 1))" >> $ProgressPreference = 'SilentlyContinue' >> >> # 创建输出目录(如果不存在) >> $outputDir = [System.IO.Path]::GetDirectoryName($OutputPath) >> if (-not [string]::IsNullOrWhiteSpace($outputDir) -and -not (Test-Path $outputDir)) { >> New-Item -ItemType Directory -Path $outputDir -Force | Out-Null >> } >> >> # 下载文件 >> Invoke-WebRequest -Uri $Url -OutFile $OutputPath -UseBasicParsing -ErrorAction Stop >> >> # 验证文件是否成功下载 >> if (-not (Test-Path $OutputPath)) { >> throw "文件下载后未找到: $OutputPath" >> } >> >> # 验证哈希(如果提供) >> if (-not [string]::IsNullOrWhiteSpace($ExpectedHash)) { >> $actualHash = (Get-FileHash -Path $OutputPath -Algorithm $HashAlgorithm).Hash >> if ($actualHash -ne $ExpectedHash) { >> Remove-Item -Path $OutputPath -Force >> throw "文件哈希不匹配! 期望: $ExpectedHash, 实际: $actualHash" >> } >> } >> >> $success = $true >> return $OutputPath >> } >> catch { >> $retryCount++ >> if ($retryCount -ge $maxRetries) { >> # 清理部分下载的文件 >> if (Test-Path $OutputPath) { >> Remove-Item -Path $OutputPath -Force -ErrorAction SilentlyContinue >> } >> throw "下载失败: $($_.Exception.Message)" >> } >> >> # 随机延迟后重试 >> $retryDelay = Get-Random -Minimum 1 -Maximum 5 >> Write-Warning "下载失败,$retryDelay 秒后重试: $($_.Exception.Message)" >> Start-Sleep -Seconds $retryDelay >> } >> } while (-not $success) >> } >> >> # ========== 辅助函数 ========== >> function Ensure-ModuleInitialized { >> if (-not $script:ModuleInitialized) { >> if (-not (Initialize-Module)) { >> throw "模块未正确初始化" >> } >> } >> } >> >> # ========== 模块初始化 ========== >> # 尝试初始化但不强制 >> try { >> Initialize-Module | Out-Null >> Write-Verbose "CurlTools 模块初始化成功" >> } catch { >> Write-Warning "模块初始化错误: $_" >> } >> >> # ========== 函数导出 ========== >> # 导出公共函数 >> Export-ModuleMember -Function Get-CurlPath, Set-CurlPath, Get-CurlVersion, Invoke-SecureDownload >> 所在位置 行:192 字符: 36 + $exeName = if ((Get-Platform) -极乐净土eq "Windows") { "curl.exe" } e ... + ~ 必须在“-”运算符后面提供一个值表达式。 所在位置 行:192 字符: 36 + $exeName = if ((Get-Platform) -极乐净土eq "Windows") { "curl.exe" } e ... + ~~~~~~ 表达式或语句中包含意外的标记“极乐净土eq”。 所在位置 行:192 字符: 36 + $exeName = if ((Get-Platform) -极乐净土eq "Windows") { "curl.exe" } e ... + ~~~~~~ “if”语句中的表达式后面缺少右“)”。 所在位置 行:164 字符: 23 + function Set-CurlPath { + ~ 语句块或类型定义中缺少右“}”。 所在位置 行:192 字符: 52 + $exeName = if ((Get-Platform) -极乐净土eq "Windows") { "curl.exe" } e ... + ~ 表达式或语句中包含意外的标记“)”。 所在位置 行:192 字符: 69 + ... e = if ((Get-Platform) -极乐净土eq "Windows") { "curl.exe" } else { "curl ... + ~~~~ 表达式或语句中包含意外的标记“else”。 所在位置 行:211 字符: 1 + } + ~ 表达式或语句中包含意外的标记“}”。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ExpectedValueExpression PS C:\Users\Administrator> # Install-CurlTools.ps1 >> >> # 创建模块目录 >> $moduleDir = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules\CurlTools" >> New-Item -ItemType Directory -Path $moduleDir -Force | Out-Null >> >> # 保存模块代码 >> $moduleCode = @' >> # 将上面的完整模块代码粘贴在这里 >> '@ >> >> Set-Content -Path "$moduleDir\CurlTools.psm1" -Value $moduleCode -Force >> >> # 创建模块清单 >> $manifest = @" >> @{ >> ModuleVersion = '1.8.0' >> RootModule = 'CurlTools.psm1' >> FunctionsToExport = @( >> 'Get-CurlPath', >> 'Set-CurlPath', >> 'Get-CurlVersion', >> 'Invoke-SecureDownload' >> ) >> CompatiblePSEditions = @('Desktop', 'Core') >> GUID = 'c0d1b1e1-1a2b-4c3d-8e4f-9a0b1c2d3e4f' >> Author = 'Your Name' >> Description = 'Powerful curl tools for PowerShell' >> } >> "@ >> >> Set-Content -Path "$moduleDir\CurlTools.psd1" -Value $manifest -Force >> >> # 重新加载模块 >> Remove-Module CurlTools -ErrorAction SilentlyContinue >> Import-Module CurlTools -Force -Verbose >> >> # 测试模块 >> if (Get-Module CurlTools) { >> Write-Host "CurlTools 模块安装成功!" -ForegroundColor Green >> >> # 测试基本功能 >> try { >> $curlPath = Get-CurlPath >> Write-Host "Curl路径: $curlPath" >> >> # 测试设置新路径 >> $newPath = Join-Path $env:TEMP "CurlTestPath" >> Set-CurlPath -Path $newPath >> >> $curlPath = Get-CurlPath >> Write-Host "新Curl路径: $curlPath" >> >> $version = Get-CurlVersion >> Write-Host "Curl版本: $version" >> >> # 测试下载功能 >> $testUrl = "https://raw.githubusercontent.com/PowerShell/PowerShell/master/README.md" >> $outputPath = "$env:TEMP\PowerShell_README.md" >> Invoke-SecureDownload -Url $testUrl -OutputPath $outputPath >> Write-Host "测试下载成功: $outputPath" -ForegroundColor Green >> } catch { >> Write-Warning "基本功能测试失败: $_" >> } >> } else { >> Write-Error "模块安装失败,请检查。" >> } >> 详细信息: 正在从路径“C:\Users\Administrator\Documents\WindowsPowerShell\Modules\CurlTools\CurlTools.psd1”加载模块。 详细信息: 正在从路径“C:\Users\Administrator\Documents\WindowsPowerShell\Modules\CurlTools\CurlTools.psm1”加载模块。 CurlTools 模块安装成功! 警告: 基本功能测试失败: 无法将“Get-CurlPath”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 PS C:\Users\Administrator> # 模块目录结构 >> # CurlTools/ >> # ├── Public/ >> # │ ├── Get-CurlPath.ps1 >> # │ ├── Set-CurlPath.ps1 >> # │ ├── Get-CurlVersion.ps1 >> # │ └── Invoke-SecureDownload.ps1 >> # ├── Private/ >> # │ ├── Get-Platform.ps1 >> # │ ├── Get-ConfigDir.ps1 >> # │ ├── Initialize-Module.极乐净土ps1 >> # │ ├── Find-CurlPath.ps1 >> # │ └── Install-Curl.ps1 >> # └── CurlTools.psm1 >> >> # CurlTools.psm1 内容 >> $publicFunctions = @(Get-ChildItem -Path "$PSScriptRoot\Public\*.ps1") >> $privateFunctions = @(Get-ChildItem -Path "$PSScriptRoot\Private\*.ps1") >> >> # 导入所有函数 >> foreach ($file in @($publicFunctions + $privateFunctions)) { >> try { >> . $file.FullName >> } catch { >> Write-Error "Failed to import function $($file.FullName): $_" >> } >> } >> >> # 导出公共函数 >> $functionsToExport = $publicFunctions | ForEach-Object { $_.BaseName } >> Export-ModuleMember -Function $functionsToExport >> >> # 初始化模块 >> Initialize-Module >> Get-ChildItem : 找不到路径“C:\Public”,因为该路径不存在。 所在位置 行:17 字符: 22 + ... publicFunctions = @(Get-ChildItem -Path "$PSScriptRoot\Public\*.ps1") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Public:String) [Get-ChildItem], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand Get-ChildItem : 找不到路径“C:\Private”,因为该路径不存在。 所在位置 行:18 字符: 23 + ... ivateFunctions = @(Get-ChildItem -Path "$PSScriptRoot\Private\*.ps1") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Private:String) [Get-ChildItem], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand Export-ModuleMember : 只能从模块内调用 Export-ModuleMember cmdlet。 所在位置 行:31 字符: 1 + Export-ModuleMember -Function $functionsToExport + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (:) [Export-ModuleMember], InvalidOperationException + FullyQualifiedErrorId : Modules_CanOnlyExecuteExportModuleMemberInsideAModule,Microsoft.PowerShell.Commands.Expo rtModuleMemberCommand Initialize-Module : 无法将“Initialize-Module”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如 果包括路径,请确保路径正确,然后再试一次。 所在位置 行:34 字符: 1 + Initialize-Module + ~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Initialize-Module:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator> # Test-CurlTools.ps1 >> >> # 加载模块 >> Import-Module CurlTools -Force >> >> # 测试1:获取Curl路径 >> $path = Get-CurlPath >> Write-Host "测试1: 当前Curl路径: $path" -ForegroundColor Cyan >> >> # 测试2:设置新的Curl路径 >> $newPath = Join-Path $env:TEMP "CurlTestPath" >> Set-CurlPath -Path $newPath >> >> # 测试3:再次获取Curl路径 >> $newPathActual = Get-CurlPath >> Write-Host "测试3: 设置后的Curl路径: $newPathActual" -ForegroundColor Cyan >> >> # 测试4:获取Curl版本 >> $version = Get-CurlVersion >> Write-Host "测试4: Curl版本: $version" -ForegroundColor Cyan >> >> # 测试5:安全下载 >> $testUrl = "https://raw.githubusercontent.com/PowerShell/PowerShell/master/README.md" >> $outputFile = Join-Path $env:TEMP "PowerShell_README.md" >> Invoke-SecureDownload -Url $testUrl -OutputPath $outputFile >> if (Test-Path $outputFile) { >> Write-Host "测试5: 文件下载成功: $outputFile" -ForegroundColor Green >> } else { >> Write-Host "测试5: 文件下载失败" -ForegroundColor Red >> } >> >> # 测试6:带哈希验证的下载 >> $expectedHash = (Get-FileHash -Path $outputFile -Algorithm SHA256).Hash >> $outputFile2 = Join-Path $env:TEMP "PowerShell_README_Verified.md" >> Invoke-SecureDownload -Url $testUrl -OutputPath $outputFile2 -ExpectedHash $expectedHash -HashAlgorithm SHA256 >> if (Test-Path $outputFile2) { >> Write-Host "测试6: 带哈希验证的文件下载成功" -ForegroundColor Green >> } else { >> Write-Host "测试6: 带哈希验证的文件下载失败" -ForegroundColor Red >> } >> Get-CurlPath : 无法将“Get-CurlPath”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径 ,请确保路径正确,然后再试一次。 所在位置 行:7 字符: 9 + $path = Get-CurlPath + ~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-CurlPath:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException 测试1: 当前Curl路径: Set-CurlPath : 无法将“Set-CurlPath”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径 ,请确保路径正确,然后再试一次。 所在位置 行:12 字符: 1 + Set-CurlPath -Path $newPath + ~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Set-CurlPath:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException Get-CurlPath : 无法将“Get-CurlPath”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径 ,请确保路径正确,然后再试一次。 所在位置 行:15 字符: 18 + $newPathActual = Get-CurlPath + ~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-CurlPath:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException 测试3: 设置后的Curl路径: Get-CurlVersion : 无法将“Get-CurlVersion”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包 括路径,请确保路径正确,然后再试一次。 所在位置 行:19 字符: 12 + $version = Get-CurlVersion + ~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-CurlVersion:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException 测试4: Curl版本: Invoke-SecureDownload : 无法将“Invoke-SecureDownload”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的 拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:25 字符: 1 + Invoke-SecureDownload -Url $testUrl -OutputPath $outputFile + ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Invoke-SecureDownload:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException 测试5: 文件下载成功: E:\ai_temp\PowerShell_README.md Invoke-SecureDownload : 无法将“Invoke-SecureDownload”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的 拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:35 字符: 1 + Invoke-SecureDownload -Url $testUrl -OutputPath $outputFile2 -Expecte ... + ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Invoke-SecureDownload:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException 测试6: 带哈希验证的文件下载失败
最新发布
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值