在64位机器上强制执行小于32位的VirtualAlloc地址。

问题描述 投票:0回答:1

我需要在内存中分配一定的空间,我一直在使用 VirtualAlloc 然而,我越来越注意到,在这一点上。VirtualAlloc 返回一个超过32位的地址,尽管总是小于33位。结果是当我复制数据到这个内存地址时,计算机崩溃到BSOD。

我使用的是64位的windows和64位的Python。我怀疑复制数据到内存的程序只能处理32位。有没有一种方法可以强制执行 VirtualAlloc 提供一个32位以内的地址?

我使用的是 Python,特别是 ctypes 包调用 VirtualAlloc,见下面的代码.多次执行这段代码会改变地址,所以反复调用下面的函数最终会导致地址低于32位。然而,我正在寻找这个问题的原因和故障安全解决方案。任何帮助都将非常感激。

import ctypes
mem_commit = 0x1000
page_readwrite = 0x4
size_bytes = 200000  # Allocation sizes are usually around this value

ctypes.windll.kernel32.VirtualAlloc.argtypes = [
    ctypes.c_void_p, ctypes.c_long, ctypes.c_long, ctypes.c_long]
ctypes.windll.kernel32.VirtualAlloc.restype = ctypes.c_int

addr = ctypes.windll.kernel32.VirtualAlloc(
    0, ctypes.c_long(size_bytes), mem_commit, page_readwrite)    

请注意,我在释放内存后,使用 VirtualFree.

python memory ctypes kernel32 virtualalloc
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.