#copied from https://gist.github.com/mklement0/006c2352ddae7bb05693be028240f5b6
#Add-Type -TypeDefinition @"
# using System;
# using System.Diagnostics;
# using System.Runtime.InteropServices;
#
# public static class Imagehlp
# {
# [DllImport("imagehlp.dll", CharSet=CharSet.Auto)]
# public static extern bool MapFileAndCheckSumA(
# [MarshalAs(UnmanagedType.LPStr)] string Filename,
# out int HeaderSum,
# out int CheckSum);
# }
#"@
Add-Type -Namespace net.same2u.WinApiHelper -Name IniFile -MemberDefinition @"
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, [Out] byte[] lpBuffer, uint nSize, string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
"@
Function Get-IniValue {
[CmdletBinding()]
param(
[Parameter(Mandatory)] [string] $LiteralPath,
[string] $Section,
[string] $Key,
[string] $DefaultValue
)
# Make sure that bona fide `null` is passed for omitted parameters, as only true `null`
# values are recognized as requests to enumerate section names / key names in a section.
$enumerate = $false
if (-not $PSBoundParameters.ContainsKey('Section')) { $Section = [NullString]::Value; $enumerate = $true }
if (-not $PSBoundParameters.ContainsKey('Key')) { $Key = [NullString]::Value; $enumerate = $true }
# Convert the path to an *absolute* one, since .NET's and the WinAPI's
# current dir. is usually differs from PowerShell's.
$fullPath = Convert-Path -ErrorAction Stop -LiteralPath $LiteralPath
$bufferCharCount = 0
$bufferChunkSize = 1024 # start with reasonably large default value.
do {
$bufferCharCount += $bufferChunkSize
# Note: We MUST use raw byte buffers, because [System.Text.StringBuilder] doesn't support
# returning values with embedded NULs - see https://stackoverflow.com/a/15274893/45375
$buffer = New-Object byte[] ($bufferCharCount * 2)
# Note: The return value is the number of bytes copied excluding the trailing NUL / double NUL
# It is only ever 0 if the buffer char. count is pointlessly small (1 with single NUL, 2 with double NUL)
$copiedCharCount = [net.same2u.WinApiHelper.IniFile]::GetPrivateProfileString($Section, $Key, $DefaultValue, $buffer, $bufferCharCount, $fullPath)
} while ($copiedCharCount -ne 0 -and $copiedCharCount -eq $bufferCharCount - (1, 2)[$enumerate]) # Check to see if the full value was retrieved or whether the buffer was too small.
# Convert the byte buffer contents back to a string.
if ($copiedCharCount -eq 0) {
# Nothing was copied (non-existent section or entry or empty value) - return the empty string.
''
} else {
# If entries are being enumerated (if -Section or -Key were omitted),
# the resulting string must be split by embedded NUL chars. to return the enumerated values as an *array*
# If a specific value is being retrieved, this splitting is an effective no-op.
[Text.Encoding]::Unicode.GetString($buffer, 0, ($copiedCharCount - (0, 1)[$enumerate]) * 2) -split "`0"
}
}
Get-IniValue TAL.ini REALTICK ToolBar
---------------------------------------------------
TAL.ini
[YPERMCACHE]
URLList=https://qayperms2.realtick.com
LoginWindowPosX=200
LoginWindowPosY=100
BrandingID=4
UseAuthLocale=1
[REALTICK]
ShutdownStatus=0
YPermsFile=data\yperms.xml
YPERMS_EXCHANGE_FILE=parm\exchange.lst
ApplicationLook=7
ChartTrendType=2
PREVPAGE=daily_stocks_forex_and_futures
Position=0 0 1920 1040
Maximized=N
ToolBar=N
StatusBar=N
AutoPageNavigator=N
PageNavigatorWidth=0
UpTickRgb=32768
DownTickRgb=-842150450
StatusMonitorPos=327,137,826,386
RTConnectURL=http://www.RealTick.com/RealTicksplash/default.aspx?username=$USER&domain=$DOMAIN
AUTOPREVPAGE=N
PurgeDelay=0
AUTOFIRSTPAGE=Y
AutoPageManager=N
ShowTabBar=N
THINFRAMES=N
NOCAPTIONS=N
HoverWindowControl=N