Ich möchte Thread erstellen, die Skript-Code in Powershell mit CreateThread WinApi ausführen. Ich benutze diesen Code aber Fehler aufgetreten:Verwenden von CreateThread winapi in Powershell-Skript
function local:Get-ProcAddress {
Param (
[OutputType([IntPtr])]
[Parameter(Position = 0, Mandatory = $True)]
[String]
$Module,
[Parameter(Position = 1, Mandatory = $True)]
[String]
$Procedure
)
# Get a reference to System.dll in the GAC
$SystemAssembly = [AppDomain]::CurrentDomain.GetAssemblies() |
Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split('\\')[-1].Equals('System.dll') }
$UnsafeNativeMethods = $SystemAssembly.GetType('Microsoft.Win32.UnsafeNativeMethods')
# Get a reference to the GetModuleHandle and GetProcAddress methods
$GetModuleHandle = $UnsafeNativeMethods.GetMethod('GetModuleHandle')
$GetProcAddress = $UnsafeNativeMethods.GetMethod('GetProcAddress')
# Get a handle to the module specified
$Kern32Handle = $GetModuleHandle.Invoke($null, @($Module))
$tmpPtr = New-Object IntPtr
$HandleRef = New-Object System.Runtime.InteropServices.HandleRef($tmpPtr, $Kern32Handle)
# Return the address of the function
$GetProcAddress.Invoke($null, @([Runtime.InteropServices.HandleRef]$HandleRef, $Procedure))
}
function local:Get-DelegateType {
Param (
[OutputType([Type])]
[Parameter(Position = 0)]
[Type[]]
$Parameters = (New-Object Type[](0)),
[Parameter(Position = 1)]
[Type]
$ReturnType = [Void]
)
$Domain = [AppDomain]::CurrentDomain
$DynAssembly = New-Object Reflection.AssemblyName('ReflectedDelegate')
$AssemblyBuilder = $Domain.DefineDynamicAssembly($DynAssembly, [System.Reflection.Emit.AssemblyBuilderAccess]::Run)
$ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('InMemoryModule', $false)
$TypeBuilder = $ModuleBuilder.DefineType('MyDelegateType', 'Class, Public, Sealed, AnsiClass, AutoClass', [System.MulticastDelegate])
$ConstructorBuilder = $TypeBuilder.DefineConstructor('RTSpecialName, HideBySig, Public', [System.Reflection.CallingConventions]::Standard, $Parameters)
$ConstructorBuilder.SetImplementationFlags('Runtime, Managed')
$MethodBuilder = $TypeBuilder.DefineMethod('Invoke', 'Public, HideBySig, NewSlot, Virtual', $ReturnType, $Parameters)
$MethodBuilder.SetImplementationFlags('Runtime, Managed')
$TypeBuilder.CreateType()
}
und definieren Create winapi:
# CreateThread
$CreateThreadAddr = Get-ProcAddress kernel32.dll CreateThread
$CreateThreadDelegate = Get-DelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr], [UInt32], [IntPtr]) ([IntPtr])
$CreateThread = [Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($CreateThreadAddr, $CreateThreadDelegate)
i Create winapi auf diese Weise nennen:
$ThreadProcScript = {
Param (
[Parameter()]
[IntPtr]$lpParameter
)
calc.exe
return [Int32]0
}
$Delegate = Get-DelegateType @([IntPtr]) ([int32])
$Callback = $ThreadProcScript -as $Delegate
$CreateThread.Invoke(0,0,$Callback ,0,0,0)
aber Fehler aufgetreten: Kann nicht konvertieren die "MyDelegateType" -Wert des Typs "MyDelegateType" zum Eingeben von "System.IntPtr" Wie kann ich diesen Fehler beheben? Und gibt es eine andere Möglichkeit, den Thread WinAPI in Powershell zu erstellen?
Was der Zweck von all dem? – PetSerAl
Ich möchte WinAPI-Programmierung in Powershell. –
warum nicht Runsspaces verwenden? – Avshalom