简体   繁体   English

我正在尝试转换一些 C# 代码,以便它可以处理 64 位地址

[英]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.

相关问题 我正在尝试将一些 python 代码转换为 C#,这个数组语法是什么? - I'm trying to convert some python code to C#, what is this array syntax? 我可以将C#代码转换为Linq代码吗? - Can I can convert this C# code into some Linq code? 所以我试图在不使用 C# 中的排序 function 的情况下按升序对列表进行排序,而且我有点新,所以我想知道我该怎么做? - So I'm trying to sort the list in ascending order without using the sort function in C# and I'm a bit new so I was wondering how could I do it? 我想用C#将文本转换成语音,我该如何做,请详细帮助我。 我在C#中是新手 - I want to convert text into speech in c# how can i so this please help me in detail . i m new in C# 我正在尝试使用 c# 文件句柄,但它不可用 - I'm trying to use the c# file handle, but it's not available 如何修改此C#代码以便Visual Studio认识到我不是白痴? - How can I modify this C# code so that Visual Studio recognizes that I'm not an idiot? 如何从 API 转换一些 RAW 数据并将其保存到变量中,以便我可以在 C# 中使用它们 - How to convert some RAW data from an API and save it into variables so i can work with them in C# 如何快速准确地将 64 位 integer 乘以 C# 中的 64 位小数? - How can I quickly and accurately multiply a 64-bit integer by a 64-bit fraction in C#? 如何将这部分C ++代码转换为C#? - How do I convert this bit of C++ code to C#? 将一些VB位匹配转换为C# - Convert some VB bit fidding to c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM