[英]I'm trying to convert some C# code so it can handle 64 bit addresses
I've tried replacing我试过更换
public static extern Int64 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
with和
public static extern bool ReadProcessMemory(int hProcess, Int64 lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);
and it still doesn't do anything except give me errors.它仍然没有做任何事情,除了给我错误。 My full code if it matters:
如果重要的话,我的完整代码:
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Transient.Utilities
{
/// <summary>
/// ProcessMemoryReader is a class that enables direct reading a process memory
/// </summary>
class ProcessMemoryReaderApi
{
// constants information can be found in <winnt.h>
[Flags]
public enum ProcessAccessType
{
PROCESS_TERMINATE = (0x0001),
PROCESS_CREATE_THREAD = (0x0002),
PROCESS_SET_SESSIONID = (0x0004),
PROCESS_VM_OPERATION = (0x0008),
PROCESS_VM_READ = (0x0010),
PROCESS_VM_WRITE = (0x0020),
PROCESS_DUP_HANDLE = (0x0040),
PROCESS_CREATE_PROCESS = (0x0080),
PROCESS_SET_QUOTA = (0x0100),
PROCESS_SET_INFORMATION = (0x0200),
PROCESS_QUERY_INFORMATION = (0x0400),
PROCESS_QUERY_LIMITED_INFORMATION = (0x1000)
}
// function declarations are found in the MSDN and in <winbase.h>
// HANDLE OpenProcess(
// DWORD dwDesiredAccess, // access flag
// BOOL bInheritHandle, // handle inheritance option
// DWORD dwProcessId // process identifier
// );
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, UInt32 dwProcessId);
// BOOL CloseHandle(
// HANDLE hObject // handle to object
// );
[DllImport("kernel32.dll")]
public static extern Int32 CloseHandle(IntPtr hObject);
// BOOL ReadProcessMemory(
// HANDLE hProcess, // handle to the process
// LPCVOID lpBaseAddress, // base of memory area
// LPVOID lpBuffer, // data buffer
// SIZE_T nSize, // number of bytes to read
// SIZE_T * lpNumberOfBytesRead // number of bytes read
// );
[DllImport("kernel32.dll")]
public static extern Int64 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
// BOOL WriteProcessMemory(
// HANDLE hProcess, // handle to process
// LPVOID lpBaseAddress, // base of memory area
// LPCVOID lpBuffer, // data buffer
// SIZE_T nSize, // count of bytes to write
// SIZE_T * lpNumberOfBytesWritten // count of bytes written
// );
[DllImport("kernel32.dll")]
public static extern Int64 WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesWritten);
class PMR
{
}
}
public class ProcessMemoryReader
{
public Process ReadProcess { get; set; }
private IntPtr handle;
public void OpenProcess()
{
ProcessMemoryReaderApi.ProcessAccessType access = ProcessMemoryReaderApi.ProcessAccessType.PROCESS_QUERY_INFORMATION |
ProcessMemoryReaderApi.ProcessAccessType.PROCESS_VM_READ |
ProcessMemoryReaderApi.ProcessAccessType.PROCESS_VM_WRITE |
ProcessMemoryReaderApi.ProcessAccessType.PROCESS_VM_OPERATION;
handle = ProcessMemoryReaderApi.OpenProcess((uint)access, 1, (uint)ReadProcess.Id);
}
public void CloseHandle()
{
int returnValue = ProcessMemoryReaderApi.CloseHandle(handle);
if (returnValue != 0)
throw new Exception("Closing Handle Failure");
}
public Byte[] ReadMemory(IntPtr memoryAdress, uint bytesToRead, out int bytesRead)
{
byte[] buffer = new byte[bytesToRead];
IntPtr pBytesRead = IntPtr.Zero;
ProcessMemoryReaderApi.ReadProcessMemory(handle, memoryAdress, buffer, bytesToRead, out pBytesRead);
bytesRead = pBytesRead.ToInt32();
return buffer;
}
public void WriteMemory(IntPtr memoryAddress, byte[] buffer, out int bytesWritten)
{
IntPtr pBytesWritten = IntPtr.Zero;
ProcessMemoryReaderApi.WriteProcessMemory(handle, memoryAddress, buffer, (uint)buffer.Length, out pBytesWritten);
bytesWritten = pBytesWritten.ToInt32();
}
}
}
Any help would be great.任何帮助都会很棒。 I used a tutorial for the code, please don't hurt me.
我使用了代码教程,请不要伤害我。
Errors: 1. Argument 5 must be passed with the ref keyword |错误: 1. 必须使用 ref 关键字 | 传递参数 5 2. Argument 2: Cant convert from System.IntPtr to long |
2. 参数 2:不能从 System.IntPtr 转换为 long | 3. Argument 1: Cant convert from System.IntPtr to int.
3. 参数 1:不能从 System.IntPtr 转换为 int。 These all take place on line 101, which is
这些都发生在第 101 行,即
ProcessMemoryReaderApi.ReadProcessMemory(handle, memoryAdress, buffer, bytesToRead, out pBytesRead);
IntPtr can hold 4 bytes pointers when building x86, and 8 byte when building in x64 mode. IntPtr 在构建 x86 时可以容纳 4 个字节的指针,在 x64 模式下构建时可以容纳 8 个字节。 If you want your code to be universal for x86 and x64, just use IntPtr for all pointer types.
如果您希望您的代码对 x86 和 x64 通用,只需对所有指针类型使用 IntPtr。 You do not need to change them to int64.
您无需将它们更改为 int64。
The first RPM pinvoke definition uses "out" and your second one uses "ref".第一个 RPM pinvoke 定义使用“out”,第二个使用“ref”。 You did not update your actual function calls to use ref, causing your compiler errors.
您没有更新实际的 function 调用以使用 ref,从而导致编译器错误。
Just use "out" and stick to IntPtr只需使用“out”并坚持 IntPtr
Change dwSize to be a IntPtr将 dwSize 更改为 IntPtr
Then call the function using the correct data types然后使用正确的数据类型调用 function
Compile for x64, and your problem will be solved.为 x64 编译,您的问题将得到解决。
If required you can use IntPtr.toInt64() etc...如果需要,您可以使用 IntPtr.toInt64() 等...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.