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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 471 - (hide annotations) (download)
Mon Jun 3 07:13:44 2013 UTC (8 years ago) by william
File size: 10320 byte(s)
+ pass process informatin by using the process's PID, instead of passing the process as a reference

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

  ViewVC Help
Powered by ViewVC 1.1.22