我的程序哪里有问题,请帮忙解决,谢谢!
按下"button1"接钮后,listbox本应显示许多artmoney进程里内存的文本,但确显示了一堆空行.可能是读内存的那几行有问题,我改了3个小时,还是发现不了问题,请高手帮帮我.谢谢![code language="c#"]
/*Test.cs*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;namespace RylandFM
{
public partial class Test : Form
{
public Test()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
ProcessMemoryReader pm = new ProcessMemoryReader();
pm.ReadProcess = Process.GetProcessesByName("artmoney")[0];
pm.OpenProcess();
byte[] buffer;
int bytesReaded=100;
String a;
for (int i = 0x0000000; i <= 0x00000ff; i++)
{
buffer = pm.ReadProcessMemoryReader((IntPtr)i, (uint)1, out bytesReaded);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
a = enc.GetString(buffer);
listBox1.Items.Add(a);
}
} }
}/*ProcessMemoryReader.cs*/
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;namespace RylandFM
{
/// <summary>
/// ProcessMemoryReader is a class that enables direct reading a process memory
/// </summary>
class ProcessMemoryReaderApi
{
// constants information can be found in <winnt.h>
public const uint PROCESS_VM_READ = (0x0010);
// 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 ReadProcessMemoryReader(
// 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 Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,[In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
} public class ProcessMemoryReader
{ public ProcessMemoryReader()
{
} /// <summary>
/// Process from which to read
/// </summary>
public Process ReadProcess
{
get
{
return m_ReadProcess;
}
set
{
m_ReadProcess = value;
}
} private Process m_ReadProcess = null; private IntPtr m_hProcess = IntPtr.Zero; public void OpenProcess()
{
m_hProcess = ProcessMemoryReaderApi.OpenProcess(ProcessMemoryReaderApi.PROCESS_VM_READ, 1, (uint)m_ReadProcess.Id);
} public void CloseHandle()
{
int iRetValue;
iRetValue = ProcessMemoryReaderApi.CloseHandle(m_hProcess);
if (iRetValue == 0)
throw new Exception("CloseHandle failed");
} public byte[] ReadProcessMemoryReader(IntPtr MemoryAddress, uint bytesToRead, out int bytesReaded)
{
byte[] buffer = new byte[bytesToRead];
IntPtr ptrBytesReaded;
ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess,MemoryAddress,buffer ,bytesToRead,out ptrBytesReaded);
bytesReaded = ptrBytesReaded.ToInt32(); return buffer;
}
}
}/*Program.cs Main Program*/
using System;
using System.Collections.Generic;
using System.Windows.Forms;namespace RylandFM
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Test());
}
}
}
[/code]
按下"button1"接钮后,listbox本应显示许多artmoney进程里内存的文本,但确显示了一堆空行.可能是读内存的那几行有问题,我改了3个小时,还是发现不了问题,请高手帮帮我.谢谢![code language="c#"]
/*Test.cs*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;namespace RylandFM
{
public partial class Test : Form
{
public Test()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
ProcessMemoryReader pm = new ProcessMemoryReader();
pm.ReadProcess = Process.GetProcessesByName("artmoney")[0];
pm.OpenProcess();
byte[] buffer;
int bytesReaded=100;
String a;
for (int i = 0x0000000; i <= 0x00000ff; i++)
{
buffer = pm.ReadProcessMemoryReader((IntPtr)i, (uint)1, out bytesReaded);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
a = enc.GetString(buffer);
listBox1.Items.Add(a);
}
} }
}/*ProcessMemoryReader.cs*/
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;namespace RylandFM
{
/// <summary>
/// ProcessMemoryReader is a class that enables direct reading a process memory
/// </summary>
class ProcessMemoryReaderApi
{
// constants information can be found in <winnt.h>
public const uint PROCESS_VM_READ = (0x0010);
// 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 ReadProcessMemoryReader(
// 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 Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,[In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
} public class ProcessMemoryReader
{ public ProcessMemoryReader()
{
} /// <summary>
/// Process from which to read
/// </summary>
public Process ReadProcess
{
get
{
return m_ReadProcess;
}
set
{
m_ReadProcess = value;
}
} private Process m_ReadProcess = null; private IntPtr m_hProcess = IntPtr.Zero; public void OpenProcess()
{
m_hProcess = ProcessMemoryReaderApi.OpenProcess(ProcessMemoryReaderApi.PROCESS_VM_READ, 1, (uint)m_ReadProcess.Id);
} public void CloseHandle()
{
int iRetValue;
iRetValue = ProcessMemoryReaderApi.CloseHandle(m_hProcess);
if (iRetValue == 0)
throw new Exception("CloseHandle failed");
} public byte[] ReadProcessMemoryReader(IntPtr MemoryAddress, uint bytesToRead, out int bytesReaded)
{
byte[] buffer = new byte[bytesToRead];
IntPtr ptrBytesReaded;
ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess,MemoryAddress,buffer ,bytesToRead,out ptrBytesReaded);
bytesReaded = ptrBytesReaded.ToInt32(); return buffer;
}
}
}/*Program.cs Main Program*/
using System;
using System.Collections.Generic;
using System.Windows.Forms;namespace RylandFM
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Test());
}
}
}
[/code]
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货