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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 246 - (hide annotations) (download)
Sun Jun 3 15:15:51 2012 UTC (8 years, 10 months ago) by william
File size: 9446 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22