/// <summary>
        /// Shows the exported funtions helper.
        /// </summary>
        /// <param name="process">The process.</param>
        /// <param name="selected">The selected.</param>
        private void ShowExportedFuntionsHelper(ProcessInformation process, ModuleInfo selected)
        {
            using (BackgroundWorker worker = new BackgroundWorker()) {
                // Work to do
                worker.DoWork += delegate(object a, DoWorkEventArgs b) {
                    ManageVisualState(AnimationState.Loading);
                    var symbols = DataExchange.GetSymbols(process.ProcessId.ToString(), selected);
                    b.Result = new object[] { symbols, selected };
                };

                // Code executed when Backgroundworker is completed
                worker.RunWorkerCompleted += delegate(object a, RunWorkerCompletedEventArgs b) {
                    ManageVisualState(AnimationState.Loaded);
                    ModuleInfo module = (ModuleInfo)((object[])b.Result)[1];
                    IEnumerable <SymbolInfo> symbols = (IEnumerable <SymbolInfo>)((object[])b.Result)[0];

                    if (symbols.Any())
                    {
                        (new Symbols(symbols, module)
                        {
                            Owner = this
                        }).ShowDialog();
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("The selected module does not export any symbol", "Information",
                                                       MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                };
                worker.RunWorkerAsync();
            }
        }
        /// <summary>
        /// Displays the memory helper.
        /// </summary>
        /// <param name="process">The process.</param>
        /// <param name="selected">The selected.</param>
        private void DisplayMemoryHelper(ProcessInformation process, ModuleInfo selected)
        {
            using (BackgroundWorker worker = new BackgroundWorker()) {
                // Work to do
                worker.DoWork += delegate(object a, DoWorkEventArgs b) {
                    ManageVisualState(AnimationState.Loading);

                    var allocations = DataExchange.GetAllocations(process.ProcessId.ToString(), selected.Name)
                                      .Where(x => !string.IsNullOrEmpty(x.AllocationBaseInHex) &&
                                             x.AllocationBaseInHex.Equals(selected.BaseOfDllInHex,
                                                                          StringComparison.OrdinalIgnoreCase));

                    b.Result = new object[] { allocations, selected.Name, process.ProcessId.ToString() };
                };

                // Code executed when Backgroundworker is completed
                worker.RunWorkerCompleted += delegate(object a, RunWorkerCompletedEventArgs b) {
                    ManageVisualState(AnimationState.Loaded);
                    string v = ((object[])b.Result)[1].ToString(), w = ((object[])b.Result)[2].ToString();
                    IEnumerable <AllocationInformation> allocations = (IEnumerable <AllocationInformation>)((object[])b.Result)[0];

                    if (allocations.Any())
                    {
                        (new MemoryAllocation(allocations, v, w)).ShowDialog();
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Unable to retrieve allocation information", "Information",
                                                       MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                };
                worker.RunWorkerAsync();
            }
        }
        /// <summary>
        /// Handles the SelectionChanged event of the cboProcesses control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void cboProcesses_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string             memAsXml = string.Empty;
            ProcessInformation selected = cboProcesses.SelectedItem as ProcessInformation;

            if (selected != null)
            {
                loadedModules.ItemsSource = DataExchange.GetModules(selected.ProcessId.ToString(), out memAsXml);
                MemoryDetailAsXml         = memAsXml;
                RefreshStatusBar();
            }
        }
        /// <summary>
        /// Checks if running inside VS.
        /// </summary>
        private void CheckIfRunningInsideVS()
        {
            IntPtr        hWnd   = IntPtr.Zero;
            StringBuilder buffer = new StringBuilder();

            if ((hWnd = DataExchange.GetForegroundWindow()) != IntPtr.Zero)
            {
                buffer.Capacity = DataExchange.GetWindowTextLength(hWnd) + 1;
                if (DataExchange.GetWindowText(hWnd, buffer, buffer.Capacity) > 0 &&
                    buffer.ToString().Equals(Title, StringComparison.Ordinal) &&
                    DataExchange.GetWindow(hWnd, (uint)DataExchange.uCmd.GW_HWNDNEXT) == IntPtr.Zero &&
                    DataExchange.GetWindow(hWnd, (uint)DataExchange.uCmd.GW_HWNDPREV) == IntPtr.Zero)
                {
                    WindowStyle = System.Windows.WindowStyle.None;
                }
            }
        }
        /// <summary>
        /// Gets the running processes.
        /// </summary>
        private void GetRunningProcesses()
        {
            ProcessInfoCollection.Clear();

            foreach (Process currentProcess in Process.GetProcesses())
            {
                try {
                    ProcessInfoCollection.Add(new ProcessInformation()
                    {
                        ProcessId   = currentProcess.Id,
                        ProcessName = currentProcess.ProcessName,
                        Icon        = DataExchange.ExtractIcon(currentProcess.MainModule.FileName)
                    });
                } catch (Exception) {
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Handles the Clicked event of the menuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void menuItem_Clicked(object sender, RoutedEventArgs e)
        {
            XDocument             dump     = null;
            AllocationInformation selected = lstMemAllocations.SelectedItem as AllocationInformation;

            using (BackgroundWorker worker = new BackgroundWorker()) {
                // Work to do
                worker.DoWork += delegate(object a, DoWorkEventArgs b) {
                    Dispatcher.Invoke(new Action(() => {
                        ((MainWindow)App.Current.MainWindow).ManageVisualState(MainWindow.AnimationState.Loading);
                    }));

                    if (selected != null && (dump = DataExchange.ReadProcessMemory(ProcessNameOrId, selected)) != null)
                    {
                        b.Result = dump;
                    }
                };

                // Code executed when Backgroundworker is completed
                worker.RunWorkerCompleted += delegate(object a, RunWorkerCompletedEventArgs b) {
                    XDocument dumpAsXml = b.Result as XDocument;

                    Dispatcher.Invoke(new Action(() => {
                        ((MainWindow)App.Current.MainWindow).ManageVisualState(MainWindow.AnimationState.Loaded);
                    }));

                    if (dumpAsXml != null)
                    {
                        (new MemoryDump(selected, Module, dump)
                        {
                            Owner = this.Owner
                        }).ShowDialog();
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Unable to read the specified memory region", "Information",
                                                       MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                };
                worker.RunWorkerAsync();
            }
        }
        /// <summary>
        /// Handles the Clicked event of the menuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void menuItem_Clicked(object sender, RoutedEventArgs e)
        {
            ProcessInformation process  = cboProcesses.SelectedItem as ProcessInformation;
            ModuleInfo         selected = ((FrameworkElement)(e.Source)).DataContext as ModuleInfo;

            switch (((System.Windows.Controls.MenuItem)sender).Name)
            {
            case "ShowInExplorer":
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                       (DispatcherOperationCallback) delegate(object arg) {
                    return(Process.Start("explorer.exe", String.Format("/select,\"{0}\"",
                                                                       selected.ImagePath)));
                }, null);
                break;

            case "ShowExportedFunctions":
                ShowExportedFuntionsHelper(process, selected);
                break;

            case "DisplayMemory":
                DisplayMemoryHelper(process, selected);
                break;

            case "FileProperties":
                DataExchange.SHObjectProperties(IntPtr.Zero, DataExchange.SHOP_FILEPATH, selected.ImagePath, null);
                break;

            case "SearchOnline":
                Utilities.SearchOnline(selected.Name);
                break;

            case "RunDialog":
                DataExchange.RunFileDlg((new WindowInteropHelper(this)).Handle, IntPtr.Zero, string.Empty, "Run",
                                        "Type the name of a program, folder, document, or Internet\nresource, and " +
                                        "Windows will open it for you.", 0);
                break;

            case "AboutBox":
                DataExchange.ShellAbout((new WindowInteropHelper(this)).Handle,
                                        "Memory Inspector", "Memory inspector is a tool for analyzing memory usage and allocations made " +
                                        "by an application and its dependent modules.", IntPtr.Zero);
                break;

            case "_Find":
                Find();
                break;

            case "Exit":
                Close();
                break;

            case "AboutThisApp":
                (new About()).ShowDialog();
                break;

            case "SaveImage":
                SaveAsImage();
                break;

            case "SaveXml":
                SaveAsXml();
                break;

            case "ColorKey":
                (new ColorKey()).ShowDialog();
                break;
            }
        }