Получение ссылки на метод GetProcAddress
вызывает исключение :
$UnsafeNativeMethods.GetMethod('GetProcAddress')
Exception calling "GetMethod" with "1" argument(s): "Ambiguous match found."
Давайте найдем правильный синтаксический паттерн :
$GetProcAddresses = $UnsafeNativeMethods.GetMethods() |
Where-Object Name -Match 'GetProcAddress'
$GetProcAddresses |
ForEach-Object { $_.ReturnTypeCustomAttributes |
Select-Object -Property Member }
Member
------
IntPtr GetProcAddress(IntPtr, System.String)
IntPtr GetProcAddress(System.Runtime.InteropServices.HandleRef, System.String)
Чтобы получить ссылку на метод GetProcAddress
: используйте либо
$GetProcAddress = $UnsafeNativeMethods.GetMethod('GetProcAddress',
[type[]]('IntPtr', 'System.String'))
или
$GetProcAddress = $UnsafeNativeMethods.GetMethod('GetProcAddress',
[type[]]('System.Runtime.InteropServices.HandleRef', 'System.String'))
Пример кода (проверено в PowerShell 5.1 ):
Remove-Variable -Name Get* -ErrorAction SilentlyContinue
# Get a reference to System.dll in the GAC
$SystemAssembly = [AppDomain]::CurrentDomain.GetAssemblies() |
Where-Object { $_.GlobalAssemblyCache -and $_.Location -and (
( $_.Location -split '\\' )[-1] -eq 'System.dll') }
$UnsafeNativeMethods = $SystemAssembly.GetType('Microsoft.Win32.UnsafeNativeMethods')
# Get a reference to the GetModuleHandle method
$GetModuleHandle = $UnsafeNativeMethods.GetMethod('GetModuleHandle')
# Let's find correct syntax pattern
$GetProcAddresses = $UnsafeNativeMethods.GetMethods() |
Where-Object Name -Match 'GetProcAddress'
$GetProcAddresses |
ForEach-Object { $_.ReturnTypeCustomAttributes |
Select-Object -Property Member }
# Get a reference to the GetProcAddress method: use either
$GetProcAddress1 = $UnsafeNativeMethods.GetMethod('GetProcAddress',
[type[]]('IntPtr', 'System.String'))
# or
$GetProcAddress2 = $UnsafeNativeMethods.GetMethod('GetProcAddress',
[type[]]('System.Runtime.InteropServices.HandleRef', 'System.String'))
Get-Variable -Name Get*, SystemAssembly, UnsafeNativeMethods |
Format-List -Property Name, Value
Вывод : D:\PShell\SO\60820994.ps1
Member
------
IntPtr GetProcAddress(IntPtr, System.String)
IntPtr GetProcAddress(System.Runtime.InteropServices.HandleRef,System.String)
Name : GetModuleHandle
Value : IntPtr GetModuleHandle(System.String)
Name : GetProcAddress1
Value : IntPtr GetProcAddress(IntPtr, System.String)
Name : GetProcAddress2
Value : IntPtr GetProcAddress(System.Runtime.InteropServices.HandleRef, System.String)
Name : GetProcAddresses
Value : {IntPtr GetProcAddress(IntPtr, System.String), IntPtr GetProcAddress(System.Runtime.InteropServices.HandleRef, System.String)}
Name : SystemAssembly
Value : System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Name : UnsafeNativeMethods
Value : Microsoft.Win32.UnsafeNativeMethods
Off-topi c note : код не запускается в PowerShell Ядро как System.dll
не установлено в глобальный кэш сборок (GA C) там по умолчанию.