为什么这个结构的变量在被调用时没有被填满?
我有以下代码,当我运行它时,将1000字节传递给函数中的参数,结构MEMORY_BASIC_INFORMATION
没有使用任何变量,它们都保持为0.我想知道这是否应该是?为什么这个结构的变量在被调用时没有被填满?
public unsafe static bool CheckForSufficientStack(long bytes)
{
MEMORY_BASIC_INFORMATION stackInfo = new MEMORY_BASIC_INFORMATION();
IntPtr currentAddr = new IntPtr((uint)&stackInfo - 4096);
VirtualQuery(currentAddr, ref stackInfo, sizeof(MEMORY_BASIC_INFORMATION));
return ((uint)currentAddr.ToInt64() - stackInfo.AllocationBase) > (bytes + STACK_RESERVED_SPACE);
}
private const long STACK_RESERVED_SPACE = 4096 * 16;
[DllImport("kernel32.dll")]
private static extern int VirtualQuery(
IntPtr lpAddress,
ref MEMORY_BASIC_INFORMATION lpBuffer,
int dwLength);
private struct MEMORY_BASIC_INFORMATION
{
internal uint BaseAddress;
internal uint AllocationBase;
internal uint AllocationProtect;
internal uint RegionSize;
internal uint State;
internal uint Protect;
internal uint Type;
}
我在Core Duo 2.0Ghz上运行Vista Enterprise X64。
那么,使用uint
来谈论X64上的地址可能是一个问题。为什么-4096?
我刚才想:
IntPtr currentAddr = new IntPtr(&stackInfo);
岂不一个'UINT在64位上也是64位的? – casablanca 2010-07-04 16:16:03
卡萨布兰卡:不; uint始终是System.UInt32类的包装器。 – 2010-07-04 16:35:52
@Warren - 特别是它总是一个*别名为'System.UInt32'(不是一个包装) – 2010-07-04 18:41:18
是的,这个代码不能在64位操作系统上运行。演员阵容不对,MEMORY_BASIC_INFORMATION声明也是错误的。这应该是更接近,未经检验的,因为我现在无法接近到x64机器:
public unsafe static bool CheckForSufficientStack(long bytes) {
var stackInfo = new MEMORY_BASIC_INFORMATION();
IntPtr currentAddr = new IntPtr((long)&stackInfo - 4096);
VirtualQuery(currentAddr, ref stackInfo, sizeof(MEMORY_BASIC_INFORMATION));
return (currentAddr.ToInt64() - (long)stackInfo.AllocationBase) > (bytes + STACK_RESERVED_SPACE);
}
private struct MEMORY_BASIC_INFORMATION {
internal IntPtr BaseAddress;
internal IntPtr AllocationBase;
internal uint AllocationProtect;
internal IntPtr RegionSize;
internal uint State;
internal uint Protect;
internal uint Type;
}
它没有改变,仍然是结构变量是0 ... – 2010-07-04 16:52:03
还有一个问题:dwLength参数声明也是错误的,它是IntPtr。 – 2010-07-04 17:00:42
http://www.pinvoke.net/default.aspx/kernel32.virtualquery – 2010-07-04 16:10:18