Created
August 13, 2020 00:16
-
-
Save jborean93/5a4e94fffa4798d5e433508234065981 to your computer and use it in GitHub Desktop.
Converts an indirect string to the actual string value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright: (c) 2020, Jordan Borean (@jborean93) <jborean93@gmail.com> | |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) | |
Function ConvertFrom-IndirectString { | |
<# | |
.SYNOPSIS | |
Converts a indirect string identifier to the actual string value it represents. | |
.PARAMETER IndirectString | |
The indirect string that begins with '@' to convert to the proper string value. | |
.EXAMPLE | |
An example | |
.NOTES | |
An indirect string can be provided in several forms: | |
File name and resource ID | |
'@filename,resource' | |
File name and resource ID with a version modifier | |
'@filename,resource;v2' | |
PRI file path and resource ID | |
'@{PRIFilepath?resource}' | |
Package name and resource ID | |
'@{PackageFullName?resource}' | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
[String] | |
$IndirectString | |
) | |
begin { | |
Add-Type -Namespace Shlwapi -Name NativeMethods -UsingNamespace System.Text -MemberDefinition @' | |
[DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)] | |
public static extern int SHLoadIndirectString( | |
string pszSource, | |
StringBuilder pszOutBuf, | |
int cchOutBuf, | |
IntPtr ppvReserved); | |
'@ | |
} | |
process { | |
$buffer = [System.Text.StringBuilder]::new() | |
$res = [Shlwapi.NativeMethods]::SHLoadIndirectString( | |
$IndirectString, | |
$buffer, | |
$buffer.MaxCapacity, | |
[IntPtr]::Zero | |
) | |
if ($res -ne 0) { | |
$msg = "Failed to convert indirect string '{0}': HRESULT 0x{1:X8}" -f ($IndirectString, $res) | |
Write-Error -Message $msg | |
return | |
} | |
$buffer.ToString() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment