binghe811215 2019-05-14 16:54 采纳率: 0%
浏览 642
已结题

c#如何hook拦截并屏蔽textbox里接收的消息,替换成自己指定的字符串?

1.一个程序里的窗口会接收com口或随机的字符串,想学习一下如何劫持这个窗口让它在接收前,先审核一下条件,后台运算后(比如+100)在到文本框。
2.小程序代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.IO.Ports;

namespace text
{
    public partial class Form1 : Form
    {
        public string receiveing = string.Empty;



        public Form1()
        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random r = new Random();
            textBox1.Text = r.Next(1000, 10000).ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            label1.Text = textBox1.Text+"保存成功!";
        }
        private void serialPort2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            receiveing = serialPort2.ReadLine();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

           serialPort2.Open();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            textBox1.Text = receiveing;


        }



        }
    }


3.要求大神提供c#hook程序的源代码,小白再次万分感谢
4.补充一下是通过您自己的程序代码起到作用,而不是修改我上面的代码,我的代码就是个测试的小程序

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-05-14 17:20
    关注

    不需要hook

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Q761397
    {
        public partial class Form1 : Form
        {
    
            private string preText = "";
            private int selstart = 0;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.TextChanged += TextBox1_TextChanged;
            }
    
            private void TextBox1_TextChanged(object sender, EventArgs e)
            {
                if (textBox1.Text.ToLower().Contains('a'))
                {
                    listBox1.Items.Add("试图修改为 " + textBox1.Text + ",已经拦截");
                    textBox1.Text = preText;
                    textBox1.SelectionStart = selstart;
                }
                else
                {
                    preText = textBox1.Text;
                    selstart = textBox1.SelectionStart;
                }
            }
    
            private void Button1_Click(object sender, EventArgs e)
            {
                textBox1.Text = Microsoft.VisualBasic.Interaction.InputBox("input:");
            }
        }
    }
    
    

    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键