Skip to content

Instantly share code, notes, and snippets.

@NickSlash
Created January 4, 2025 14:17
Show Gist options
  • Select an option

  • Save NickSlash/fb4670a8a06120d754fb646892f501b7 to your computer and use it in GitHub Desktop.

Select an option

Save NickSlash/fb4670a8a06120d754fb646892f501b7 to your computer and use it in GitHub Desktop.
Powershell function to evaluate Localized String Resources
# Add-Type block for ResourceLoader class
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class ResourceLoader {
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int LoadString(IntPtr hInstance, int uID, System.Text.StringBuilder lpBuffer, int nBufferMax);
[DllImport("kernel32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FreeLibrary(IntPtr hModule);
}
"@
function Get-StringResource {
param (
[string]$ResourceString
)
if ($ResourceString -match '^@(.+?),(-?\d+)$') {
$filePath = $Matches[1]
$resourceId = [int]$Matches[2]
$filePath = [Environment]::ExpandEnvironmentVariables($filePath)
$hInstance = [ResourceLoader]::LoadLibrary($filePath)
if ($hInstance -eq [IntPtr]::Zero) {
Write-Error "Failed to load library: $filePath"
return
}
$buffer = New-Object System.Text.StringBuilder 1024
$result = [ResourceLoader]::LoadString($hInstance, $resourceId, $buffer, $buffer.Capacity)
if ($result -eq 0 -and $resourceId -lt 0) {
# Try with the positive resource ID
$result = [ResourceLoader]::LoadString($hInstance, -$resourceId, $buffer, $buffer.Capacity)
}
[ResourceLoader]::FreeLibrary($hInstance) | Out-Null
if ($result -gt 0) {
return $buffer.ToString()
} else {
Write-Error "Failed to load string resource ID: $resourceId"
}
} else {
Write-Error "Invalid resource string format: $ResourceString"
}
}
# Get-StringResource -ResourceString "@%SystemRoot%\system32\mspaint.exe,-59420" -> "&Edit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment