Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取地址值_C# - Fatal编程技术网

C# 读取地址值

C# 读取地址值,c#,C#,我想读取一个地址的值,而不是我在游戏中通过作弊引擎发现的值,我如何读取该地址的值?我有这段代码,但它返回读取的字节,我如何才能使返回值 const int PROCESS_WM_READ = 0x0010; const int addr = 0x10ACE333; [DllImport("kernel32.dll")] public static extern IntPtr OpenProcess(int dwDesiredAccess, boo

我想读取一个地址的值,而不是我在游戏中通过作弊引擎发现的值,我如何读取该地址的值?我有这段代码,但它返回读取的字节,我如何才能使返回值

 const int PROCESS_WM_READ = 0x0010;
        const int addr = 0x10ACE333;
        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

        [DllImport("kernel32.dll")]
        public static extern bool ReadProcessMemory(int hProcess,
        int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);

        static void Main(string[] args)
        {
            Process process = Process.GetProcessesByName("Process")[0];
            IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);

            int bytesRead = 0;
            byte[] buffer = new byte[4]; //To read a 4 byte unicode string

            ReadProcessMemory((int)processHandle, addr, buffer, buffer.Length, ref bytesRead);

            Console.WriteLine(Encoding.Unicode.GetString(buffer) +
                  " (" + bytesRead.ToString() + "bytes)");
            Console.ReadLine();
        }

这是在C#中读取以null结尾的宽字符数组的方式:

公共静态字符串ReadNullTerminatedWString(IntPtr handle、IntPtr addr、int maxlength)
{
var bytearray=新字节[maxlength*2];
IntPtr bytesread=IntPtr.Zero;
ReadProcessMemory(句柄、地址、字节数组、最大长度*2、输出字节读取);
int nullterm=0;
while(nullterm
缓冲区中有什么?我是c#的新手,随附的代码是我从另一个问题复制的。。。我想读取一个内存地址的值,比如在CE中,我可以看到它的值,我想在C中看到它,为此,你需要知道实际的内部类型。有些“值”可能在一个字节(0到255,或-128到127)上,另一些在两个字节(0到32767,或~-16k到~16k)上,等等。。。你知道可能是什么吗?首先,知道使用的字节数是最重要的一步。通常的“整数”值是4字节,通常的“浮点”值是单精度的4字节/32位,或双精度的8字节/64位等等。我在CE上看到的类型是4字节,现在我知道了内部类型,我应该如何读取它的值?首先,将4字节作为字节数组获取。然后,您可以使用
位转换器
inti=BitConverter.ToInt32(字节,0);WriteLine(“int:{0}”,i)如果这不起作用,您可以尝试在转换之前反转字节数组。(实际上,您不需要创建另一个数组,第二个参数是值的起始索引)。在我的示例中,我假设您有一个类似于.NET
Int32
值的标准整数。
public static string ReadNullTerminatedWString(IntPtr handle, IntPtr addr, int maxlength)
{
    var bytearray = new byte[maxlength * 2];

    IntPtr bytesread = IntPtr.Zero;

    ReadProcessMemory(handle, addr, bytearray, maxlength * 2, out bytesread);

    int nullterm = 0;
    while (nullterm < bytesread.ToInt64() && bytearray[nullterm] != 0)
    {
        nullterm = nullterm + 2;
    }

    string s = Encoding.Unicode.GetString(bytearray, 0, nullterm);

    return s;
}