public SubClassManager(WindowsManager owner) { _wm = owner; Type enumType = typeof(NativeMethods.Msgs); Array msgArray = Enum.GetValues(enumType); msgValues = new Dictionary <NativeMethods.Msgs, string>(msgArray.Length); for (int i = 0; i < msgArray.Length; i++) { NativeMethods.Msgs value = (NativeMethods.Msgs)msgArray.GetValue(i); string name = Enum.GetName(enumType, value); if (msgValues.ContainsKey(value)) { msgValues[value] = name; } else { msgValues.Add(value, name); } } // create Listener control which we will use to interact with the hooked window listener = new ListenerControl(this); listener.Show(); //_Breakpoints = new Dictionary<NativeMethods.Msgs, MessageBreakpoint>(); }
private void lvwMessages_SelectedValueChanged(object sender, EventArgs e) { if (suspendSelection) { return; } FinishEdit(); if (lvwMessages.SelectedItems.Count == 0) { propertyGrid.SelectedObjects = null; } else { List <FormMain.MessageBreakpoint> watchs = new List <FormMain.MessageBreakpoint>(); for (int i = 0; i < lvwMessages.SelectedItems.Count; i++) { NativeMethods.Msgs msg = (NativeMethods.Msgs)lvwMessages.SelectedItems[i]; if (_Watchs.ContainsKey(msg)) { watchs.Add(_Watchs[msg]); } else { FormMain.MessageBreakpoint mwatch = new FormMain.MessageBreakpoint(); mwatch.Msg = msg; mwatch.ModifiedMsg = msg; mwatch.Action = FormMain.BreakpointAction.None; _Watchs.Add(msg, mwatch); watchs.Add(mwatch); } } propertyGrid.SelectedObjects = watchs.ToArray(); } }
/// <summary> /// Initialize a new instance of this class with the provided values /// </summary> /// <param name="hWnd"></param> /// <param name="msg"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> public FormEditMessage(IntPtr hWnd, NativeMethods.Msgs msg, IntPtr wParam, IntPtr lParam) : this() { lblHeader.Text = string.Format(CultureInfo.InvariantCulture, lblHeader.Text, hWnd.ToInt32()); _EditMessage = new EditMessage(msg, wParam, lParam); propertyGrid.SelectedObject = _EditMessage; propertyGrid.ExpandAllGridItems(); }
/// <summary> /// Initialize a new instance of this class /// </summary> /// <param name="msg"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> public EditMessage(NativeMethods.Msgs msg, IntPtr wParam, IntPtr lParam) { _Msg = msg; _ModifiedMsg = msg; mParams = new int[] { wParam.ToInt32(), lParam.ToInt32() }; _WParam = new FormMain.MessageParam(mParams[0]); _LParam = new FormMain.MessageParam(mParams[1]); }
/// <summary> /// This is also a very important method for us.. /// This method is invoke when we are going to change the parameters of the message /// By one way or another we have to pass these new parameters to the hooked window /// which is generally belongs to a different process. /// /// To resolve this problem. We are going to write our new values to the memory of that process /// which ProcessViewer.Hooks.dll will read these values from there... /// </summary> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> IntPtr WriteToTargetProcessMemory(IntPtr wParam, IntPtr lParam, NativeMethods.Msgs modifiedMsg) { // Open hooked window's process IntPtr hProcess = NativeMethods.OpenProcess(NativeMethods.PROCESS_VM_OPERATION | NativeMethods.PROCESS_VM_READ | NativeMethods.PROCESS_VM_WRITE, false, _Window.ProcessId); // allocate memory from target proces's memory block // 12 bytes : 4 modified msg + 4 wParam + 4 lParam IntPtr memAddress = NativeMethods.VirtualAllocEx(hProcess, IntPtr.Zero, 12, NativeMethods.MEM_COMMIT, NativeMethods.PAGE_READWRITE); if (memAddress == IntPtr.Zero) { return(IntPtr.Zero); } int written = 0; // write our new parameter values to the tarhet process memory bool hr = NativeMethods.WriteProcessMemory(hProcess, memAddress, new int[] { (int)modifiedMsg, wParam.ToInt32(), lParam.ToInt32() }, 12, out written); // close handle NativeMethods.CloseHandle(hProcess); if (!hr) { return(IntPtr.Zero); } //if (Properties.Settings.Default.ShowChangedParameterValues) //{ // StringBuilder text = new StringBuilder(); // text.Append(Properties.Resources.Hook_Parameters_Changed); // text.Append("MSG: "); // text.Append(modifiedMsg.ToString()); // text.Append(", WParam: "); // text.Append(wParam.ToInt32()); // text.Append(", LParam: "); // text.Append(lParam.ToInt32()); // text.AppendLine(); // Instance.txtMessages.AppendText(text.ToString()); //} return(memAddress); }
private void lvwMessages_DrawItem(object sender, DrawItemEventArgs e) { e.DrawBackground(); NativeMethods.Msgs msg = (NativeMethods.Msgs)lvwMessages.Items[e.Index]; Brush br = SystemBrushes.WindowText; if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { br = SystemBrushes.HighlightText; } if (_Watchs.ContainsKey(msg) && _Watchs[msg].Action != FormMain.BreakpointAction.None) { e.Graphics.DrawString(msg.ToString(), boltFont, br, e.Bounds); } else { e.Graphics.DrawString(msg.ToString(), lvwMessages.Font, br, e.Bounds); } }
private void FillList() { // first finish edit opeation FinishEdit(); lvwMessages.Items.Clear(); Array values = Enum.GetValues(typeof(NativeMethods.Msgs)); Array.Sort(values); string filter = txtFilter.Text; if (filter.Equals(Properties.Resources.Filter_Key)) { filter = string.Empty; } filter = filter.ToUpper(CultureInfo.InvariantCulture); bool onlyWatched = chkOnlyWactheds.Checked; CompareInfo info = CompareInfo.GetCompareInfo(1033); for (int i = 0; i < values.Length; i++) { NativeMethods.Msgs msg = (NativeMethods.Msgs)values.GetValue(i); if ((onlyWatched && !_Watchs.ContainsKey(msg)) || info.IndexOf(msg.ToString().ToUpper(CultureInfo.InvariantCulture), filter) == -1) { continue; } lvwMessages.Items.Add(msg); } foreach (KeyValuePair <NativeMethods.Msgs, FormMain.MessageBreakpoint> watch in _Watchs) { if (Array.IndexOf(values, watch.Key) == -1) { lvwMessages.Items.Add(watch.Key); } } }
/// <summary> /// Process message which belongs to the hooked window /// </summary> /// <param name="hWnd"></param> /// <param name="hookedMessage"></param> /// <returns> /// This return value will be sended to the Hooked window by our Listener Control /// /// Return value might be; /// 0 : which means ignore this message (Hooked window will ignore this message) /// 1 : Do nothing (Hooked window will do its original action for this message) /// <Other Values> : We are going to change the wParam and lParam for this message /// here the return value indicates an adress in the target process memory which contains /// the new values of the wParam and lParam /// By passing this address to the Hooked window, we make it read the new values /// from this address and replace original wParam and lParam with the new values /// and process message according to this new parameters /// </returns> internal IntPtr ProcessMessage(IntPtr hWnd, ref NativeMethods.HOOK_MSG hookedMessage) { lock (this) { IntPtr result = IntPtr.Zero; NativeMethods.Msgs msg = (NativeMethods.Msgs)hookedMessage.msg; switch (msg) { case NativeMethods.Msgs.WM_MOVE: _wm.OnMove(); break; case NativeMethods.Msgs.WM_SIZE: _wm.OnResize(); break; } //if (Enabled && Breakpoints.ContainsKey(msg)) //{ // // if Breakpoints enabled and if we have a breakpoint for the // // given message then do our action // MessageBreakpoint watch = Breakpoints[msg]; // switch (watch.Action) // { // case BreakpointAction.IgnoreMessage: // result = new IntPtr(1); // break; // case BreakpointAction.ManuelEditParameters: // FormEditMessage em = new FormEditMessage(hWnd, msg, hookedMessage.wParam, hookedMessage.lParam); // em.ShowDialog(FormMain.Instance); // if (!em.Ignore) // result = WriteToTargetProcessMemory(em.WParam, em.LParam, em.ModifiedMsg); // else // result = new IntPtr(1); // break; // case BreakpointAction.AutoChangeParameters: // NativeMethods.Msgs modifiedMsg = (NativeMethods.Msgs)hookedMessage.msg; // IntPtr wParam = hookedMessage.wParam; // IntPtr lParam = hookedMessage.lParam; // if ((watch.Modifications & ModifiyingSections.Message) == ModifiyingSections.Message // && watch.ModifiedMsg != NativeMethods.Msgs.WM_NULL) // modifiedMsg = watch.ModifiedMsg; // if ((watch.Modifications & ModifiyingSections.WParam) == ModifiyingSections.WParam) // wParam = new IntPtr(watch.WParam.Value); // if ((watch.Modifications & ModifiyingSections.LParam) == ModifiyingSections.LParam) // lParam = new IntPtr(watch.LParam.Value); // result = WriteToTargetProcessMemory(wParam, lParam, modifiedMsg); // break; // } //} if (ShowMessages) { //StringBuilder text = new StringBuilder(); //text.AppendFormat("0x{0:X8}", hWnd.ToInt32()); //text.Append(" "); //if (msgValues.ContainsKey(msg)) // text.Append(msgValues[msg]); //else // text.Append(msg); //text.Append(" "); //text.AppendFormat("wParam:{0}", hookedMessage.wParam); //text.AppendFormat(" lParam:{0}", hookedMessage.lParam); //if (result == (IntPtr)1) // text.Append(Properties.Resources.Hook_Ignored); //text.AppendLine(); //FormMain.Instance.txtMessages.AppendText(text.ToString()); } return(result); } }