Powershell 添加具有结构参数的类型处理函数
我正在尝试将一个函数导入到PowerShell中,该函数的参数为Struct。我收到一个“找不到类型”错误,但我不确定如何包含它。任何帮助都将不胜感激Powershell 添加具有结构参数的类型处理函数,powershell,Powershell,我正在尝试将一个函数导入到PowerShell中,该函数的参数为Struct。我收到一个“找不到类型”错误,但我不确定如何包含它。任何帮助都将不胜感激 $MethodDefinition = ( '[DllImport("kernel32.dll")]static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, uint dwLength);
$MethodDefinition = (
'[DllImport("kernel32.dll")]static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, uint dwLength);')
$Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru
==编辑====
因此,此代码不会加载函数:
$MethodDefinition =
@'
public struct MEMORY_BASIC_INFORMATION
{
public IntPtr BaseAddress;
public IntPtr AllocationBase;
public uint AllocationProtect;
public uint RegionSize;
public uint State;
public uint Protect;
public uint Type;
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]public static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);
[DllImport("kernel32.dll")]static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, uint dwLength);
'@
$Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru
但我在尝试访问函数时出错
代码:
错误:
Method invocation failed because [Win32.Kernel32] doesn't contain a method named 'VirtualQueryEx'.
我哪里出错了?如果我运行此命令,它会告诉我您缺少以下声明: 所以试试这个:
$MethodDefinition =
@'
public struct MEMORY_BASIC_INFORMATION
{
public IntPtr BaseAddress;
public IntPtr AllocationBase;
public uint AllocationProtect;
public uint RegionSize;
public uint State;
public uint Protect;
public uint Type;
}
[DllImport("kernel32.dll")]
static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, uint dwLength);
'@
$Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru
Hi jisaak(和Mathias),您的解决方案有效,但该方法不存在。您可以查看我的编辑吗?您的
VirtualQueryEx
没有标记为public
,因此默认情况下它是private
,因此对PowerShell方法解析不可见。
The type or namespace name 'MEMORY_BASIC_INFORMATION' could not be found (are you missing a using directive or an
assembly reference?)
$MethodDefinition =
@'
public struct MEMORY_BASIC_INFORMATION
{
public IntPtr BaseAddress;
public IntPtr AllocationBase;
public uint AllocationProtect;
public uint RegionSize;
public uint State;
public uint Protect;
public uint Type;
}
[DllImport("kernel32.dll")]
static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, uint dwLength);
'@
$Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru