C# 将System.Int32转换为System.UINTPTTR

C# 将System.Int32转换为System.UINTPTTR,c#,powershell,pinvoke,C#,Powershell,Pinvoke,我正在使用PowerShell中的p/Invoke功能调用Win32VirtualProtectEx函数 根据函数签名,对于第三个参数,它需要一个无符号int指针作为参数 static extern bool VirtualProtectEx(IntPtr hProcess、IntPtr lpAddress、, UIntPtr dwSize、uint flNewProtect、out uint LPFLOLLDPROTECT) 不过,我有一个名为$shellcode的变量,希望将其写入某个内存地

我正在使用PowerShell中的p/Invoke功能调用Win32
VirtualProtectEx
函数

根据函数签名,对于第三个参数,它需要一个无符号int指针作为参数

static extern bool VirtualProtectEx(IntPtr hProcess、IntPtr lpAddress、,
UIntPtr dwSize、uint flNewProtect、out uint LPFLOLLDPROTECT)

不过,我有一个名为
$shellcode
的变量,希望将其写入某个内存地址,因此我需要保留
$shellcode.Length
字节。但是我不知道该把谁投给指针。我已经试过了

[System.uintpttr]$shellcode.Length

# Set memory for shellcode to Execute and Read
$oldProtect = 0
$procHandle.GetType().fullname
$basePtr.GetType().fullname
$shellcode.Length.GetType().fullname
$PAGE_EXECUTE_READWRITE.GetType().fullname
$oldProtect.GetType().fullname
$result = $Kernel32::VirtualProtectEx($procHandle, $basePtr, $shellcode.Length, $PAGE_EXECUTE_READ, [ref] $oldProtect);
但它又回来了

System.IntPtr
System.IntPtr
System.Int32
System.Int32
System.Int32
Cannot convert argument "dwSize", with value: "168", for "VirtualProtectEx" to type
"System.UIntPtr": "Cannot convert the "168" value of type "System.Int32" to type
"System.UIntPtr"."
At C:\Users\Admin\Desktop\malware.ps1:59 char:1
+ $result = $Kernel32::VirtualProtectEx($procHandle, $basePtr, $shellco ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

使用:
[UIntPtr]::new($shellcode.Length)

使用:
[UIntPtr]::new($shellcode.Length)

这很有效!如果你想添加这个作为答案,我会将它标记为已解决:)这很有效!如果要将此添加为答案,我会将其标记为已解决:)