Private Declare PtrSafe Function VirtualProtect Lib "kernel32" ( _
ByVal memAddress As LongPtr, _
ByVal lengthInBytes As LongPtr, _
ByVal newProtect As protectFlags, _
ByRef outOldProtect As protectFlags _
) As Long
Public Enum protectFlags 'an enum is a Long in VBA which is 32-bit so can be used in place of DWORD
PAGE_NOACCESS = &H1
PAGE_READONLY = &H2
PAGE_READWRITE = &H4
PAGE_WRITECOPY = &H8
PAGE_EXECUTE = &H10
PAGE_EXECUTE_READ = &H20
PAGE_EXECUTE_READWRITE = &H40
PAGE_EXECUTE_WRITECOPY = &H80
PAGE_GUARD = &H100
PAGE_NOCACHE = &H200
PAGE_WRITECOMBINE = &H400
End Enum
我这样称呼它:
Dim memoryAddress As LongPtr
memoryAddress = ... 'some protected memory
Dim originalProtection As protectFlags
If CBool(VirtualProtect(memoryAddress, 8, PAGE_EXECUTE_READWRITE, originalProtection)) Then
Debug.Print "Previous protection cached: &H"; Hex(originalProtection)
Else
Err.Raise 5, Description:="Memory address cannot be unprotected with PAGE_EXECUTE_READWRITE"
End If
...然后我正在恢复保护
VirtualProtect(memoryAddress, 8, originalProtection, temp)
问题是,originalProtection
第一次调用后仍然是 &H0,所以在第二次调用中我得到 87 的 LastDllError -“参数不正确。”。我很确定内存最初受到保护,因为删除对 VirtualProtect 的调用会使我的程序崩溃,而包含它是成功的。但我无法重置保护。
作为参考,这是我正在翻译的内容:
BOOL VirtualProtect(
LPVOID lpAddress,
SIZE_T dwSize,
DWORD flNewProtect,
PDWORD lpflOldProtect
);