首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

为什么ReadProcessMemory返回的是0,该如何处理

2012-01-30 
为什么ReadProcessMemory返回的是0一个老的单机游戏,自己想写个修改器,可是返回的是0不知道是哪不对?帮我

为什么ReadProcessMemory返回的是0
一个老的单机游戏,自己想写个修改器,可是返回的是0
不知道是哪不对?帮我看看吧

using   System;
using   System.Collections.Generic;
using   System.ComponentModel;
using   System.Data;
using   System.Drawing;
using   System.Text;
using   System.Windows.Forms;
using   System.Runtime.InteropServices;
                       
namespace   WindowsApplication1
{
        public   partial   class   Form1   :   Form
        {
                [DllImport( "User32.dll ")]
                public   static   extern   int   FindWindow(string   s,string   ss);

                [DllImport( "Kernel32.dll ")]
                public   static   extern   int   OpenProcess(int   dwDesiredAccess,   bool   bInheritHandle,int   dwProcessId);

                [DllImport( "user32.dll ",EntryPoint   =   "GetWindowThreadProcessId ")]
                public   static   extern   int   GetWindowThreadProcessId(int   hWnd,ref   int   lpdwProcessId);

                [DllImport( "kernel32.dll ")]
                static   extern   bool   ReadProcessMemory(uint   hProcess,IntPtr   lpBaseAddress,   IntPtr   lpBuffer,   uint   nSize,   ref   uint   lpNumberOfBytesRead);

                [DllImport( "Kernel32.dll ")]
                public   static   extern   int   CloseHandle(int   handle);

                public   void   game()
                {
                        const   int   PROCESS_VM_READ   =   16;
                        int   pid=0;
                        GetWindowThreadProcessId(Phandle,ref   pid);
                        pro2=OpenProcess(PROCESS_VM_READ,   false,pid);
                        textBox2.Text   =   pro2.ToString();
                        textBox4.Text   =   pid.ToString();
                        timer2.Enabled   =   true;
                }

                public   Form1()
                {
                        InitializeComponent();
                }

                int   Phandle,pro2;


                //uint   pro2;
                private   void   button1_Click(object   sender,   EventArgs   e)
                {
                }

                private   void   Form1_Load(object   sender,   EventArgs   e)
                {
                }

                private   void   timer1_Tick(object   sender,   EventArgs   e)
                {
                        Phandle   =   FindWindow( "Sword3   Class ",   null);
                        if   (Phandle   !=   0)
                        {
                                label1.Text   =   "游戏运行! ";
                                game();
                                textBox1.Text   =   Phandle.ToString();
                                timer1.Interval=10000;
                        }
                        else
                        {
                                label1.Text   =   "游戏未运行! ";
                                timer1.Interval=2000;
                        }
                }

                private   void   textBox1_TextChanged(object   sender,   EventArgs   e)
                {

                }

                private   void   timer2_Tick(object   sender,   EventArgs   e)
                {
                        byte[]   vBuffer   =   new   byte[4];
                        IntPtr   vBytesAddress   =   Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer,   0);   //   得到缓冲区的地址
                        uint   vNumberOfBytesRead   =   0;
                        ReadProcessMemory((uint)pro2,   (IntPtr)0xc0890c0,   vBytesAddress,   (uint)vBuffer.Length,   ref   vNumberOfBytesRead);


                        int   vInt   =   Marshal.ReadInt32(vBytesAddress);
                        textBox5.Text   =   vInt.ToString();
                }

                private   void   Form1_FormClosed(object   sender,   FormClosedEventArgs   e)
                {
                        CloseHandle(pro2);
                }
        }
}

[解决办法]
how old are you 怎么老是你!

那款游戏大吗?有下的地方没?下到我机器上看看
[解决办法]
检查一下声明对不对,如果对的话再检查一下操作权限够不够.

然后还可能是游戏中有保护手段.
[解决办法]
在ReadProcessMemory后面调用 GetLastError 函数可以获取执行错误的原因

如果要读取一个进程中不可访问空间的数据,该函数就会返回0.

热点排行