如果可能的話我會去簡單的解決方案(即基於安裝目錄猜測二進制類型)。如果大部分時間都不正確,可以從註冊表中枚舉runtimelib位置,然後檢查您找到的jvm.dll的PE頭。
下面是Powershell中的一個示例實現。
# Determine the machinetype of an executable
function Get-BinaryType {
param ($filename)
$PE_POINTER_OFFSET = 60;
$MACHINE_OFFSET = 4;
$data = get-content $filename -encoding byte -totalcount 4096
$pe_header_addr = [BitConverter]::ToInt32($data, $PE_POINTER_OFFSET)
$machineUint = [BitConverter]::ToUint16($data, $pe_header_addr + $MACHINE_OFFSET)
$machineType = @{0 = "Native"; 0x014c = "I386"; 0x0200 = "Itanium"; 0x8664 = "x64"}
$machineType.Get_Item([int]$machineUint)
}
# Look for the value of the RuntimeLib value found under the JavaSoft registry
# keys and determine the binarytype of the file found under this value.
$registrykeys = "HKLM:\software\Wow6432Node\JavaSoft", "HKLM:\software\JavaSoft"
$registrykeys | Where-Object {test-path $_} | % {
dir $_ -rec -ea SilentlyContinue |
ForEach-Object {
(get-itemproperty -Path $_.PsPath).RuntimeLib
} |
select -Unique |
select @{N="Name";E={$_}}, @{N="BinaryType";E={Get-BinaryType $_}}
}