|
|
- param(
- [Parameter(Mandatory=$true)]
- [string]$ResourceString
- )
- function Parse-ResourceString {
- param([string]$inputString)
-
- $cleanString = $inputString.TrimStart('@')
- $parts = $cleanString -split ','
- if ($parts.Count -ne 2) {
- throw "无效的资源字符串格式: $inputString。正确格式: 路径,-ID"
- }
- $dllPathRaw = $parts[0].Trim()
- $idRaw = $parts[1].Trim()
- if ($idRaw -match '^-?(\d+)$') {
- $stringId = [uint32]$matches[1]
- } else {
- throw "无效的 ID 格式: $idRaw"
- }
- $dllPath = [System.Environment]::ExpandEnvironmentVariables($dllPathRaw)
- return @{
- DllPath = $dllPath
- StringId = $stringId
- }
- }
- Add-Type @"
- using System;
- using System.Runtime.InteropServices;
- using System.Text;
- public class winAPI {
- [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- private static extern int LoadStringW(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int cchBufferMax);
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, uint dwFlags);
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern bool FreeLibrary(IntPtr hModule);
- private const uint LOAD_LIBRARY_AS_DATAFILE = 0x00000002;
- public static string LoadString(string dllPath, uint stringId)
- {
- IntPtr hModule = LoadLibraryEx(dllPath, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
- if (hModule == IntPtr.Zero)
- {
- return "无法加载 DLL: " + dllPath;
- }
- try
- {
- StringBuilder buffer = new StringBuilder(1024);
- int charsCopied = LoadStringW(hModule, stringId, buffer, buffer.Capacity);
-
- if (charsCopied > 0)
- {
- return buffer.ToString();
- }
- else
- {
- return "未找到 ID 为 " + stringId.ToString() + " 的字符串。";
- }
- }
- finally
- {
- FreeLibrary(hModule);
- }
- }
- }
- "@
- try {
- # Write-Host "输入资源字符串: $ResourceString" -ForegroundColor Cyan
- $parsed = Parse-ResourceString -inputString $ResourceString
- # Write-Host "解析结果:" -ForegroundColor Green
- # Write-Host " DLL路径: $($parsed.DllPath)"
- # Write-Host " 字符串ID: $($parsed.StringId)"
- # Write-Host ""
- if (-not (Test-Path $parsed.DllPath)) {
- Write-Host "错误: DLL 文件不存在 - $($parsed.DllPath)" -ForegroundColor Red
- exit 1
- }
- $result = [winAPI]::LoadString($parsed.DllPath, $parsed.StringId)
- # Write-Host "读取结果:" -ForegroundColor Green
- Write-Host $result
- } catch {
- Write-Host "错误: $_" -ForegroundColor Red
- exit 1
- }
复制代码 这是可供调用的ps,参数格式 @%systemroot%\system32\ActionCenterCPL.dll,-1
比如一个批处理调用的例子- @echo off
- set "RESOURCE_STRING=@%%systemroot%%\system32\DeviceCenter.dll,-1000"
- powershell -Exec Bypass -F ".\ReadDllString.ps1" -ResourceString "%RESOURCE_STRING%"
- pause
复制代码 |
|