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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 838 - (show annotations) (download)
Tue Sep 16 00:57:18 2014 UTC (6 years ago) by william
File size: 10172 byte(s)
+ add an icon resource

1 #region Logging Defines
2 // include this any class or method that required logging, and comment-out what is not needed
3
4 #region Enabled logging levels
5 #define LOGGING_ENABLE_INFO
6 #define LOGGING_ENABLE_WARN
7 #define LOGGING_ENABLE_DEBUG
8 #define LOGGING_ENABLE_VERBOSEDEBUG
9 #define LOGGING_ENABLE_ERROR
10 #define LOGGING_ENABLE_VERBOSEERROR
11 #define LOGGING_ENABLE_PROFILER
12 #endregion
13 #endregion
14 using System;
15 using System.Collections.Generic;
16 using System.ComponentModel;
17 using System.Data;
18 using System.Drawing;
19 using System.Linq;
20 using System.Text;
21 using System.Windows.Forms;
22 using WeifenLuo.WinFormsUI.Docking;
23 using System.Diagnostics;
24 using System.IO;
25 using RomCheater.Logging;
26 using System.Reflection;
27 using Sojaner.MemoryScanner.MemoryProviers;
28 using Sojaner.MemoryScanner;
29 using RomCheater.PluginFramework.Events;
30 using RomCheater.Core;
31
32 namespace RomCheater.Docking
33 {
34 public partial class FloatingRamDumperDialog : DockContent,
35 IAcceptsPlugin<IConfigPlugin>,
36 IAcceptsProcess<Process>,
37 IAcceptsProcessAndConfig,
38 IAcceptPEData
39 {
40 #region sub-classes
41 private const int BYTE_CORRECTION_VALUE = 23;
42 public enum DumpSize
43 {
44 Bytes,
45 KiloBytes,
46 MegaBytes,
47 GigaBytes,
48 }
49 #endregion
50 private DumpSize dumpSize = DumpSize.Bytes;
51
52 public FloatingRamDumperDialog() { InitializeComponent(); this.Icon = Core.Properties.Resources.romcheater_icon; this.AcceptedPlugin = null; this.AcceptedProcess = null; }
53 public FloatingRamDumperDialog(IConfigPlugin config) : this() { this.AcceptedPlugin = config; }
54 public FloatingRamDumperDialog(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; }
55
56 private void FloatingRamDumperDialog_Load(object sender, EventArgs e)
57 {
58 txtStart.Value = MemorySizeConstants.MinimumAddress;
59 if (this.PEData != null)
60 {
61 if (this.PEData.Is32bitAssembly())
62 {
63 txtEnd.Value = MemorySizeConstants.MinimumAddress + MemorySizeConstants.MaximumAddressSize_x86;
64 }
65 else
66 {
67 txtEnd.Value = MemorySizeConstants.MinimumAddress + MemorySizeConstants.MaximumAddressSize_x64;
68 }
69 }
70 else
71 {
72 txtEnd.Value = MemorySizeConstants.MinimumAddress + MemorySizeConstants.MaximumAddressSize_x86;
73 }
74
75 }
76
77
78 #region IAcceptsProcess<Process> Members
79 public Process AcceptedProcess { get; set; }
80 #endregion
81 #region IAcceptsPlugin<IConfigPlugin> Members
82 public IConfigPlugin AcceptedPlugin { get; set; }
83 #endregion
84 #region ram-dump specific
85 private void radioBTNBytes_CheckedChanged(object sender, EventArgs e) { dumpSize = DumpSize.Bytes; }
86 private void radioBTNKiloBytes_CheckedChanged(object sender, EventArgs e) { dumpSize = DumpSize.KiloBytes; }
87 private void radioBTNMegaBytes_CheckedChanged(object sender, EventArgs e) { dumpSize = DumpSize.MegaBytes; }
88 private void radioBTNGigaBytes_CheckedChanged(object sender, EventArgs e) { dumpSize = DumpSize.GigaBytes; }
89 private void btnCalcEndAddr_Click(object sender, EventArgs e)
90 {
91 ulong start = 0;
92 ulong end = 0;
93 start = txtStart.Value;
94 switch (dumpSize)
95 {
96 case DumpSize.Bytes:
97 end = (ulong)(Convert.ToDouble(txtDumpSize.Text) * 1.0 + (double)start) + BYTE_CORRECTION_VALUE;
98 txtEnd.Value = end;
99 break;
100 case DumpSize.KiloBytes:
101 end = (ulong)(Convert.ToDouble(txtDumpSize.Text) * 1000.0 + (double)start) + BYTE_CORRECTION_VALUE;
102 txtEnd.Value = end;
103 break;
104 case DumpSize.MegaBytes:
105 end = (ulong)(Convert.ToDouble(txtDumpSize.Text) * 1000000.0 + (double)start) + BYTE_CORRECTION_VALUE;
106 txtEnd.Value = end;
107 break;
108 case DumpSize.GigaBytes:
109 end = (ulong)(Convert.ToDouble(txtDumpSize.Text) * 1000000000.0 + (double)start) + BYTE_CORRECTION_VALUE;
110 txtEnd.Value = end;
111 break;
112 }
113 }
114 private void btnCalcStartAddr_Click(object sender, EventArgs e)
115 {
116 long start = 0;
117 long end = 0;
118 end = txtEnd.ToInt64();
119 switch (dumpSize)
120 {
121 case DumpSize.Bytes:
122 start = (long)((double)end - (Convert.ToDouble(txtDumpSize.Text) * 1.0)) + BYTE_CORRECTION_VALUE;
123 txtStart.Value = (ulong)start;
124 break;
125 case DumpSize.KiloBytes:
126 start = (long)((double)end - (Convert.ToDouble(txtDumpSize.Text) * 1000.0)) + BYTE_CORRECTION_VALUE;
127 txtStart.Value = (ulong)start;
128 break;
129 case DumpSize.MegaBytes:
130 start = (long)((double)end - (Convert.ToDouble(txtDumpSize.Text) * 1000000.0)) + BYTE_CORRECTION_VALUE;
131 txtStart.Value = (ulong)start;
132 break;
133 case DumpSize.GigaBytes:
134 start = (long)((double)end - (Convert.ToDouble(txtDumpSize.Text) * 1000000000.0)) + BYTE_CORRECTION_VALUE;
135 txtStart.Value = (ulong)start;
136 break;
137 }
138 }
139 private void btnCalcDumpSize_Click(object sender, EventArgs e)
140 {
141 ulong start = txtStart.Value;
142 ulong end = txtEnd.Value;
143 ulong byte_diff = (end - start) + BYTE_CORRECTION_VALUE;
144 switch (dumpSize)
145 {
146 case DumpSize.Bytes:
147 txtDumpSize.Text = string.Format("{0:n2}", (double)byte_diff);
148 break;
149 case DumpSize.KiloBytes:
150 txtDumpSize.Text = string.Format("{0:n3}", (double)byte_diff / 1000.0);
151 break;
152 case DumpSize.MegaBytes:
153 txtDumpSize.Text = string.Format("{0:n6}", (double)byte_diff / 1000000.0);
154 break;
155 case DumpSize.GigaBytes:
156 txtDumpSize.Text = string.Format("{0:n9}", (double)byte_diff / 1000000000.0);
157 break;
158 }
159 }
160 private void btnDumpRam_Click(object sender, EventArgs e)
161 {
162 if (this.AcceptedProcess == null)
163 {
164 MessageBox.Show("Please select a process to dump memory from", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
165 return;
166 }
167 DialogResult result = dumpsaver.ShowDialog();
168 if (result != DialogResult.OK) return;
169
170
171 if(txtEnd.Value < txtStart.Value)
172 {
173 MessageBox.Show("Failed to dump memory...end address cannot be less than start address.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
174 return;
175 }
176 ulong count = (txtEnd.Value - txtStart.Value) + 1; // add one to get the last address included
177 DumpRam(txtStart.Value, count, dumpsaver.FileName);
178 }
179 #endregion
180
181 #region memory support
182 private void DumpRam(ulong start, ulong count, string filename)
183 {
184 if (this.AcceptedProcess == null) return;
185 ulong bytesReadSize;
186 using (GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
187 {
188 provider.OpenProvider();
189 if (provider.WriteProcessMemoryToFile(filename, (ulong)start, count, out bytesReadSize))
190 {
191 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);
192 }
193 else
194 {
195 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);
196 }
197 provider.CloseProvider();
198 }
199
200 //ProcessMemoryReader provider = new ProcessMemoryReader();
201 //provider.ReadProcess = this.AcceptedProcess;
202 //provider.OpenProcess();
203 //provider.WriteProcessMemoryToFile(filename, (int)start, count, out bytesReadSize);
204 //provider.CloseHandle();
205
206 }
207 #endregion
208
209 private IPEDData _PEData;
210 private IPEDData PEData { get { return _PEData; } set { _PEData = value; } }
211 public void SetPEViewerData(IPEDData peData)
212 {
213 this.PEData = peData;
214 txtStart.Value = MemorySizeConstants.MinimumAddress;
215 if (this.PEData != null)
216 {
217 if (this.PEData.Is32bitAssembly())
218 {
219 txtEnd.Value = MemorySizeConstants.MinimumAddress + MemorySizeConstants.MaximumAddressSize_x86;
220 }
221 else
222 {
223 txtEnd.Value = MemorySizeConstants.MinimumAddress + MemorySizeConstants.MaximumAddressSize_x64;
224 }
225 }
226 else
227 {
228 txtEnd.Value = MemorySizeConstants.MinimumAddress + MemorySizeConstants.MaximumAddressSize_x86;
229 }
230 }
231 }
232 }

  ViewVC Help
Powered by ViewVC 1.1.22