/[RomCheater]/trunk/RomCheater/Docking/FloatingRamDumperDialog.cs
ViewVC logotype

Contents of /trunk/RomCheater/Docking/FloatingRamDumperDialog.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 238 - (show annotations) (download)
Sun Jun 3 12:48:38 2012 UTC (8 years, 7 months ago) by william
File size: 8494 byte(s)
+ change inteface implementation
+ implement GenericMemoryProvider

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using WeifenLuo.WinFormsUI.Docking;
10 using RomCheater.PluginFramework.Interfaces;
11 using System.Diagnostics;
12 using System.IO;
13 using RomCheater.Logging;
14 using System.Reflection;
15 using Sojaner.MemoryScanner.MemoryProviers;
16
17 namespace RomCheater.Docking
18 {
19 public partial class FloatingRamDumperDialog : DockContent, IAcceptsProcess<Process>, IAcceptsPlugin<IConfigPlugin>
20 {
21 #region sub-classes
22 private const int BYTE_CORRECTION_VALUE = 23;
23 public enum DumpSize
24 {
25 Bytes,
26 KiloBytes,
27 MegaBytes,
28 GigaBytes,
29 }
30 #endregion
31 private DumpSize dumpSize = DumpSize.Bytes;
32
33 public FloatingRamDumperDialog() { InitializeComponent(); this.AcceptedPlugin = null; this.AcceptedProcess = null; }
34 public FloatingRamDumperDialog(IConfigPlugin config) : this() { this.AcceptedPlugin = config; }
35 public FloatingRamDumperDialog(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; }
36
37 private void FloatingRamDumperDialog_Load(object sender, EventArgs e)
38 {
39 txtStart.Value = 0;
40 txtEnd.Value = int.MaxValue;
41 }
42
43
44 #region IAcceptsProcess<Process> Members
45 public Process AcceptedProcess { get; set; }
46 #endregion
47 #region IAcceptsPlugin<IConfigPlugin> Members
48 public IConfigPlugin AcceptedPlugin { get; set; }
49 #endregion
50 #region ram-dump specific
51 private void radioBTNBytes_CheckedChanged(object sender, EventArgs e) { dumpSize = DumpSize.Bytes; }
52 private void radioBTNKiloBytes_CheckedChanged(object sender, EventArgs e) { dumpSize = DumpSize.KiloBytes; }
53 private void radioBTNMegaBytes_CheckedChanged(object sender, EventArgs e) { dumpSize = DumpSize.MegaBytes; }
54 private void radioBTNGigaBytes_CheckedChanged(object sender, EventArgs e) { dumpSize = DumpSize.GigaBytes; }
55 private void btnCalcEndAddr_Click(object sender, EventArgs e)
56 {
57 ulong start = 0;
58 ulong end = 0;
59 start = txtStart.Value;
60 switch (dumpSize)
61 {
62 case DumpSize.Bytes:
63 end = (ulong)(Convert.ToDouble(txtDumpSize.Text) * 1.0 + (double)start) + BYTE_CORRECTION_VALUE;
64 txtEnd.Value = end;
65 break;
66 case DumpSize.KiloBytes:
67 end = (ulong)(Convert.ToDouble(txtDumpSize.Text) * 1000.0 + (double)start) + BYTE_CORRECTION_VALUE;
68 txtEnd.Value = end;
69 break;
70 case DumpSize.MegaBytes:
71 end = (ulong)(Convert.ToDouble(txtDumpSize.Text) * 1000000.0 + (double)start) + BYTE_CORRECTION_VALUE;
72 txtEnd.Value = end;
73 break;
74 case DumpSize.GigaBytes:
75 end = (ulong)(Convert.ToDouble(txtDumpSize.Text) * 1000000000.0 + (double)start) + BYTE_CORRECTION_VALUE;
76 txtEnd.Value = end;
77 break;
78 }
79 }
80 private void btnCalcStartAddr_Click(object sender, EventArgs e)
81 {
82 long start = 0;
83 long end = 0;
84 end = txtEnd.ToInt64();
85 switch (dumpSize)
86 {
87 case DumpSize.Bytes:
88 start = (long)((double)end - (Convert.ToDouble(txtDumpSize.Text) * 1.0)) + BYTE_CORRECTION_VALUE;
89 txtStart.Value = (ulong)start; ;
90 break;
91 case DumpSize.KiloBytes:
92 start = (long)((double)end - (Convert.ToDouble(txtDumpSize.Text) * 1000.0)) + BYTE_CORRECTION_VALUE;
93 txtStart.Value = (ulong)start; ;
94 break;
95 case DumpSize.MegaBytes:
96 start = (long)((double)end - (Convert.ToDouble(txtDumpSize.Text) * 1000000.0)) + BYTE_CORRECTION_VALUE;
97 txtStart.Value = (ulong)start; ;
98 break;
99 case DumpSize.GigaBytes:
100 start = (long)((double)end - (Convert.ToDouble(txtDumpSize.Text) * 1000000000.0)) + BYTE_CORRECTION_VALUE;
101 txtStart.Value = (ulong)start;
102 break;
103 }
104 }
105 private void btnCalcDumpSize_Click(object sender, EventArgs e)
106 {
107 ulong start = txtStart.Value;
108 ulong end = txtEnd.Value;
109 ulong byte_diff = (end - start) + BYTE_CORRECTION_VALUE;
110 switch (dumpSize)
111 {
112 case DumpSize.Bytes:
113 txtDumpSize.Text = string.Format("{0:n2}", (double)byte_diff);
114 break;
115 case DumpSize.KiloBytes:
116 txtDumpSize.Text = string.Format("{0:n3}", (double)byte_diff / 1000.0);
117 break;
118 case DumpSize.MegaBytes:
119 txtDumpSize.Text = string.Format("{0:n6}", (double)byte_diff / 1000000.0);
120 break;
121 case DumpSize.GigaBytes:
122 txtDumpSize.Text = string.Format("{0:n9}", (double)byte_diff / 1000000000.0);
123 break;
124 }
125 }
126 private void btnDumpRam_Click(object sender, EventArgs e)
127 {
128 if (this.AcceptedProcess == null)
129 {
130 MessageBox.Show("Please select a process to dump memory from", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
131 return;
132 }
133 DialogResult result = dumpsaver.ShowDialog();
134 if (result != DialogResult.OK) return;
135 DumpRam(txtStart.Value, txtEnd.Value, dumpsaver.FileName);
136 }
137 #endregion
138
139 #region memory support
140 private void DumpRam(ulong start, ulong end, string filename)
141 {
142 uint byte_count = (uint)(end - start);
143 string arch = ProcessorAssemblyArchitecture.GetProcessorArchitecture(typeof(FloatingRamDumperDialog).Assembly);
144 if (arch == ProcessorAssemblyArchitecture.x86)
145 {
146 // intptr is 4 bytes on x86
147 if (end > int.MaxValue)
148 logger.Warn.WriteLine("Warning: DumpRam(): ending address is greater than 0x{0:x8} and we are running x86, this will exceed the max value for IntPtr", int.MaxValue);
149 }
150 else if (arch == ProcessorAssemblyArchitecture.x64)
151 {
152 // inptr is 8 bytes on x64
153 if (end > uint.MaxValue)
154 logger.Warn.WriteLine("Warning: DumpRam(): ending address is greater than 0x{0:x8} and we are running x64, this will exceed the max value for UIntPtr", int.MaxValue);
155 }
156 else
157 {
158 throw new InvalidProgramException(string.Format("Unexcepted processor aritecture: expected x86 or x64 but we have: {0}", arch));
159 }
160 DumpRam(start, byte_count, filename);
161 }
162 private void DumpRam(ulong start, uint count, string filename)
163 {
164 if (this.AcceptedProcess == null) return;
165 GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
166 int bytesReadSize;
167 if (provider.WriteProcessMemoryToFile(filename, (uint)start, count, out bytesReadSize))
168 {
169 MessageBox.Show(string.Format("Succefully dumped memory (0x{0:x8}-0x{1:x8}) from pid=({3}) to file {2}", start, start + count, filename, string.Format("0x{0:x4} {1}.exe", this.AcceptedProcess.Id, AcceptedProcess.ProcessName)), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
170 }
171 else
172 {
173 MessageBox.Show(string.Format("Failed to dump memory (0x{0:x8}-0x{1:x8}) from pid=({3}) to file {2}", start, start + count, filename, string.Format("0x{0:x4} {1}.exe", this.AcceptedProcess.Id, AcceptedProcess.ProcessName)), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
174 }
175 }
176 #endregion
177 }
178 }

  ViewVC Help
Powered by ViewVC 1.1.22