0

所以我在 x86 上运行了 2 年的代码,两周前我尝试将它编译到任何 CPU。它不起作用。我做了一些阅读,但没有找到任何东西。我是初学者,如果您想帮助我,我将不胜感激。

 <StructLayout(LayoutKind.Sequential)>
Public Structure MEMORY_BASIC_INFORMATION
    Public BaseAddress As Integer
    Public AllocationBase As Integer
    Public AllocationProtect As Integer
    Public RegionSize As Integer
    Public State As Integer
    Public Protect As Integer
    Public lType As Integer
End Structure
<StructLayout(LayoutKind.Sequential)>
Public Structure SYSTEM_INFO
    Dim dwOemID As Integer
    Dim dwPageSize As Integer
    Dim lpMinimumApplicationAddress As Integer
    Dim lpMaximumApplicationAddress As Integer
    Dim dwActiveProcessorMask As Integer
    Dim dwNumberOrfProcessors As Integer
    Dim dwProcessorType As Integer
    Dim dwAllocationGranularity As Integer
    Dim dwReserved As Integer
End Structure
<DllImport("kernel32.dll", EntryPoint:="VirtualQueryEx", SetLastError:=True), SuppressUnmanagedCodeSecurity()>
Public Function VirtualQueryEx(ByVal hProcess As IntPtr, ByVal lpAddress As UInteger, ByRef lpBuffer As MEMORY_BASIC_INFORMATION, ByVal dwLength As Integer) As Integer
End Function
<DllImport("kernel32.dll", EntryPoint:="GetSystemInfo", SetLastError:=True), SuppressUnmanagedCodeSecurity()>
Public Sub GetSystemInfo(ByRef lpSystemInfo As SYSTEM_INFO)
End Sub
<DllImport("kernel32.dll", EntryPoint:="OpenProcess", SetLastError:=True), SuppressUnmanagedCodeSecurity()>
Public Function OpenProcess(ByVal dwDesiredAccess As Integer, ByVal blnheritHandle As Boolean, ByVal dwAppProcessId As Integer) As IntPtr
End Function
<DllImport("kernel32.dll", EntryPoint:="CloseHandle", SetLastError:=True), SuppressUnmanagedCodeSecurity()>
Public Function CloseHandle(ByVal hObject As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("kernel32.dll", EntryPoint:="ReadProcessMemory", SetLastError:=True), SuppressUnmanagedCodeSecurity()>
Public Function ReadProcessMemory(ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer() As Byte, ByVal iSize As Integer, ByRef lpNumberOfBytesRead As Integer) As Boolean
End Function
Public Const PROCESS_VM_READ = (&H10)
Public Const PROCESS_VM_OPERATION = (&H8)
Public Const PROCESS_QUERY_INFORMATION = (&H400)
Public Const PROCESS_READ_WRITE_QUERY = PROCESS_VM_READ + PROCESS_VM_OPERATION + PROCESS_QUERY_INFORMATION

        Public Sub Test2()
        Dim _targetProcessHandle As IntPtr = System.Diagnostics.Process.GetProcesses("solitaire")(0).Handle
        Dim _mbi As MEMORY_BASIC_INFORMATION, _sysInfo As SYSTEM_INFO
        Dim _mbiSize As Int32 = System.Runtime.InteropServices.Marshal.SizeOf(_mbi)
        GetSystemInfo(_sysInfo)
        Dim _addr As Integer = _sysInfo.lpMinimumApplicationAddress
        Dim _readBuff(_sysInfo.dwPageSize - 1) As Byte
        Dim _actualBytesRead As Int32 = 0
        Dim _oldPageProtection As UInt32 = 0
        Dim _accessRightsChanged As Boolean = False
        _targetProcessHandle = OpenProcess(PROCESS_READ_WRITE_QUERY, False, CInt(_targetProcessHandle))
        Dim ret As Integer
        Do
            ret = VirtualQueryEx(_targetProcessHandle, CType(_addr, IntPtr), _mbi, _mbiSize)
            If ret = _mbiSize Then
                If ((_mbi.lType = &H20000) And (_mbi.State = &H1000) And (_mbi.RegionSize > 0)) Then
                    Dim _byteBuff(_mbi.RegionSize) As Byte
                    ReadProcessMemory(_targetProcessHandle, _mbi.BaseAddress, _byteBuff, _mbi.RegionSize, 0)
                    'Do some work
                    Array.Clear(_byteBuff, 0, _byteBuff.Length)
                End If
                _addr = _mbi.BaseAddress + _mbi.RegionSize
            End If
        Loop While _addr < _sysInfo.lpMaximumApplicationAddress
        CloseHandle(_targetProcessHandle)
    End Sub

这在 X86 上工作得很好,但它不想在 AnyCpu 上运行。你能帮我吗 ?提前致谢。

4

1 回答 1

-1

你得到 badimageformatexception 吗?我假设您正在加载 x86 dll 并尝试在 x64 机器上运行它,如果 cpu 是 x64,任何 cpu 都会强制您的程序在 x64 中运行。强制你的程序到 x86 仍然可以在 x64 机器上工作。

于 2016-04-23T21:04:23.333 回答