找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 17060|回复: 3

[问题求助] PowerShell怎么读取dll文件中的字符串?[已解决]

[复制链接]
发表于 2024-4-24 10:09:10 | 显示全部楼层 |阅读模式
本帖最后由 czjt1234 于 2026-4-29 22:47 编辑

@%SystemRoot%\System32\ActionCenterCPL.dll,-1

比如这个,对应字符串是:操作中心

请问怎么用ps读取该字符串?
发表于 2024-4-24 13:49:03 | 显示全部楼层
 楼主| 发表于 前天 22:44 | 显示全部楼层
  1. Add-Type @"
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using System.Text;

  5. public class winAPI
  6. {
  7.     [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  8.     private static extern int LoadStringW(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int cchBufferMax);

  9.     [DllImport("kernel32.dll", SetLastError = true)]
  10.     private static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, uint dwFlags);

  11.     [DllImport("kernel32.dll", SetLastError = true)]
  12.     private static extern bool FreeLibrary(IntPtr hModule);

  13.     private const uint LOAD_LIBRARY_AS_DATAFILE = 0x00000002;

  14.     public static string LoadString(string dllPath, uint stringId)
  15.     {
  16.         IntPtr hModule = LoadLibraryEx(dllPath, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
  17.         if (hModule == IntPtr.Zero)
  18.         {
  19.             return "无法加载 DLL: " + dllPath;
  20.         }

  21.         try
  22.         {
  23.             StringBuilder buffer = new StringBuilder(1024);
  24.             int charsCopied = LoadStringW(hModule, stringId, buffer, buffer.Capacity);
  25.             
  26.             if (charsCopied > 0)
  27.             {
  28.                 return buffer.ToString();
  29.             }
  30.             else
  31.             {
  32.                 return "未找到 ID 为 " + stringId.ToString() + " 的字符串。";
  33.             }
  34.         }
  35.         finally
  36.         {
  37.             FreeLibrary(hModule);
  38.         }
  39.     }
  40. }
  41. "@

  42. $dllPath = [System.Environment]::ExpandEnvironmentVariables("%SystemRoot%\System32\ActionCenterCPL.dll")
  43. # Write-Host "DLL路径: $dllPath"
  44. # Write-Host "是否存在: $(Test-Path $dllPath)"
  45. $result = [winAPI]::LoadString($dllPath, 1)
  46. Write-Host "$result"
复制代码
注册表中读取一般是这样 @%systemroot%\system32\ActionCenterCPL.dll,-1
这是手动指定文件和ID的ps

评分

参与人数 1技术 +1 收起 理由
cutebe + 1 感谢分享

查看全部评分

 楼主| 发表于 前天 22:47 | 显示全部楼层
  1. param(
  2.     [Parameter(Mandatory=$true)]
  3.     [string]$ResourceString
  4. )

  5. function Parse-ResourceString {
  6.     param([string]$inputString)
  7.    
  8.     $cleanString = $inputString.TrimStart('@')
  9.     $parts = $cleanString -split ','
  10.     if ($parts.Count -ne 2) {
  11.         throw "无效的资源字符串格式: $inputString。正确格式: 路径,-ID"
  12.     }
  13.     $dllPathRaw = $parts[0].Trim()
  14.     $idRaw = $parts[1].Trim()
  15.     if ($idRaw -match '^-?(\d+)$') {
  16.         $stringId = [uint32]$matches[1]
  17.     } else {
  18.         throw "无效的 ID 格式: $idRaw"
  19.     }
  20.     $dllPath = [System.Environment]::ExpandEnvironmentVariables($dllPathRaw)

  21.     return @{
  22.         DllPath = $dllPath
  23.         StringId = $stringId
  24.     }
  25. }

  26. Add-Type @"
  27. using System;
  28. using System.Runtime.InteropServices;
  29. using System.Text;

  30. public class winAPI {
  31.     [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  32.     private static extern int LoadStringW(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int cchBufferMax);

  33.     [DllImport("kernel32.dll", SetLastError = true)]
  34.     private static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, uint dwFlags);

  35.     [DllImport("kernel32.dll", SetLastError = true)]
  36.     private static extern bool FreeLibrary(IntPtr hModule);

  37.     private const uint LOAD_LIBRARY_AS_DATAFILE = 0x00000002;

  38.     public static string LoadString(string dllPath, uint stringId)
  39.     {
  40.         IntPtr hModule = LoadLibraryEx(dllPath, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
  41.         if (hModule == IntPtr.Zero)
  42.         {
  43.             return "无法加载 DLL: " + dllPath;
  44.         }

  45.         try
  46.         {
  47.             StringBuilder buffer = new StringBuilder(1024);
  48.             int charsCopied = LoadStringW(hModule, stringId, buffer, buffer.Capacity);
  49.             
  50.             if (charsCopied > 0)
  51.             {
  52.                 return buffer.ToString();
  53.             }
  54.             else
  55.             {
  56.                 return "未找到 ID 为 " + stringId.ToString() + " 的字符串。";
  57.             }
  58.         }
  59.         finally
  60.         {
  61.             FreeLibrary(hModule);
  62.         }
  63.     }
  64. }
  65. "@

  66. try {
  67.     # Write-Host "输入资源字符串: $ResourceString" -ForegroundColor Cyan
  68.     $parsed = Parse-ResourceString -inputString $ResourceString
  69.     # Write-Host "解析结果:" -ForegroundColor Green
  70.     # Write-Host "  DLL路径: $($parsed.DllPath)"
  71.     # Write-Host "  字符串ID: $($parsed.StringId)"
  72.     # Write-Host ""
  73.     if (-not (Test-Path $parsed.DllPath)) {
  74.         Write-Host "错误: DLL 文件不存在 - $($parsed.DllPath)" -ForegroundColor Red
  75.         exit 1
  76.     }
  77.     $result = [winAPI]::LoadString($parsed.DllPath, $parsed.StringId)
  78.     # Write-Host "读取结果:" -ForegroundColor Green
  79.     Write-Host $result
  80. } catch {
  81.     Write-Host "错误: $_" -ForegroundColor Red
  82.     exit 1
  83. }
复制代码
这是可供调用的ps,参数格式 @%systemroot%\system32\ActionCenterCPL.dll,-1

比如一个批处理调用的例子
  1. @echo off

  2. set "RESOURCE_STRING=@%%systemroot%%\system32\DeviceCenter.dll,-1000"
  3. powershell -Exec Bypass -F ".\ReadDllString.ps1" -ResourceString "%RESOURCE_STRING%"

  4. pause
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-5-1 00:49

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表