我正在尝试从特定进程中读出内存。为了开始,我使用了这个教程:http://www.codeproject.com/Articles/670373/Csharp-Read-Write-another-Process-Memory这对于记事本到目前为止工作正常,但现在我想与Open Office Writer(swriter)一样。
这也可行,但我只是得到了中国的迹象。我在另一个Thread上读到,你必须将字符串转换为另一种格式。
我的程序目前看起来像这样:
public string ReadProcessMemory(IntPtr procHandle, int position)
{
string result = string.Empty;
int bytesRead = 0;
byte[] buffer = new byte[24];
DllImports.ReadProcessMemory(procHandle, position, buffer, buffer.Length, ref bytesRead);
var regex = new Regex(@"^\w+$");
result = Encoding.Unicode.GetString(buffer);
if (!string.IsNullOrEmpty(result) && regex.IsMatch(result, 0))
{
System.Diagnostics.Debug.WriteLine("Unicode: " + result);
}
result = Encoding.Default.GetString(buffer);
if (!string.IsNullOrEmpty(result) && regex.IsMatch(result, 0))
{
System.Diagnostics.Debug.WriteLine("ANSI: " + result);
}
result = Encoding.ASCII.GetString(buffer);
if (!string.IsNullOrEmpty(result) && regex.IsMatch(result, 0))
{
System.Diagnostics.Debug.WriteLine("ASCII: " + result);
}
result = Encoding.UTF8.GetString(buffer);
if (!string.IsNullOrEmpty(result) && regex.IsMatch(result, 0))
{
System.Diagnostics.Debug.WriteLine("UTF8: " + result);
}
return result;
}
我只是在每个可能的位置调用它来进行测试。
ProcessHelper p = new ProcessHelper();
p.EnumProcessNames();
var handle = p.GetProcessHandleByProcessName("swriter");
Parallel.For(0, Int32.MaxValue, f => p.ReadProcessMemory(handle, f));
可悲的是,我得到的结果好坏参半。大部分时间输出看起来像这样:
Unicode:煱煱煱煱煱煱煱煱煱煱煱 ANSI:qqqqqqqqqqqqqqqqqqqqqqqq UTF8:444333344444444444444444 Unicode:ᡐᡐᡐᡐᡐᡐᡐἰᡐᡐᡐᡐ UTF8:444444444443344444444444 ANSI:444444444433444444444444 ANSI:443333444444444444444444 ASCII:443333444444444444444444 UTF8:443333444444444444444444 ASCII:qqqqqqqqqqqqqqqqqqqqqqqq Unicode:ᡐᡐᡐᡐᡐᡐἰᡐᡐᡐᡐᡐ ASCII:444444444433444444444444 UTF8:444444444433444444444444 ANSI:433334444444444444444444 UTF8:qqqqqqqqqqqqqqqqqqqqqqqq ANSI:44444444433444444444444444 Unicode:ᡐᡐᡐᡐᡐἰᡐᡐᡐᡐᡐὀ Unicode:ᡐᡐᡐᡐἰᡐᡐᡐᡐᡐὀᡐ Unicode:ᡐᡐᡐἰᡐᡐᡐᡐᡐὀᡐὐ ASCII:44444444433444444444444444 ASCII:433334444444444444444444 UTF8:43333444444444444444444444 Unicode:ᡐᡐἰᡐᡐᡐᡐᡐὀᡐὐ彟 Unicode:煱煱煱煱煱煱煱煱煱煱q UTF8:444444444334444444444444 Unicode:煱煱煱煱煱煱煱煱煱煱伥 ANSI:444444443344444444444444 ANSI:333344444444444444444443
在后来的数字上,我得到:
Unicode:耇耇耇耇耇耇㠇㠸㠸㠸㠸 Unicode:ހހހހހހހ㠸㠸㠸㠸㠸 Unicode:耇耇耇耇耇耇耇耇耇耇耇 Unicode:ހހހހހހހހހހހހ Unicode:耇耇耇耇耇耇耇耇耇耇耇 Unicode:ހހހހހހހހހހހހ Unicode:耇耇耇耇耇耇耇耇耇耇耇 Unicode:ހހހހހހހހހހހހ Unicode:耇耇耇耇耇耇耇耇耇耇㠇 Unicode:耇耇耇耇耇㠇㠸㠸㠸㠸㠸 Unicode:ހހހހހހ㠸㠸㠸㠸㠸㠸 Unicode:耇耇耇耇耇㠸㠸㠸㠸㠸㠸 Unicode:ހހހހހ㠸㠸㠸㠸㠸㠸㠸 Unicode:ހހހހހހހހހހހ㠸
使用OllyDbg工具,我可以毫无问题地读取整个内存。
我在这里遗漏了什么吗?除了这一个,我没有找到ReadProcessMemory的另一个实现:
[DllImport("kernel32.dll")]
internal static extern bool ReadProcessMemory(IntPtr hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);