【PC微信探秘】使用C#进行微信二维码解码及编码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;
using ZXing.QrCode;

namespace L018WeChatQrcodeDecodeEncode
{
    public partial class Form1 : Form
    {
        //微信进程
        Process WxProcess = null;
        Boolean Init()
        {
            WxProcess = null;
            Process[] processes = Process.GetProcesses();
            foreach (Process process in processes)
            {
                if (process.ProcessName == "WeChat")
                {
                    WxProcess = process;
                    break;
                }
            }
            if (WxProcess == null)
            {
                MessageBox.Show("微信没有找到!");
                return false;
            }
            return true;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (Init() == false) return;

            //   1)获取设备上下文句柄:GetWindowDC-- > ReleaseDC
            IntPtr windowDCHandle = GetWindowDC(IntPtr.Zero);
            if (windowDCHandle == IntPtr.Zero)
            {
                MessageBox.Show("获取设备上下文句柄失败!");
                return;
            }

            //   2)获取指定窗口边界和尺寸:GetWindowRect,
            Rectangle rectangle = new Rectangle();
            if (GetWindowRect(WxProcess.MainWindowHandle, ref rectangle) == 0)
            {
                MessageBox.Show("获取指定窗口边界和尺寸失败!");
                return;
            };

            //	注意C#中的Rectangle与C++中RECT区别
            //3)计算窗口大小
            int width = rectangle.Width - rectangle.X;
            int height = rectangle.Height - rectangle.Y;

            //   4)创建一个设备上下文相关的位图:CreateCompatibleBitmap->DeleteObject
            IntPtr compatibleBitmapHandle = CreateCompatibleBitmap(windowDCHandle, width, height);
            if (compatibleBitmapHandle == IntPtr.Zero)
            {
                MessageBox.Show("创建一个设备上下文相关的位图失败!");
                return;
            }

            //   5)创建一个内存上下文兼容的句柄:CreateCompatibleDC->DeleteDC
            IntPtr compatibleDCHandle = CreateCompatibleDC(windowDCHandle);
            if (compatibleDCHandle == IntPtr.Zero)
            {
                MessageBox.Show("创建一个内存上下文兼容的句柄失败!");
                return;
            }

            //   6)选择一个设备上下文对象:SelectObject
            if (SelectObject(compatibleDCHandle, compatibleBitmapHandle) == IntPtr.Zero)
            {
                MessageBox.Show("选择一个设备上下文对象失败!");
                return;
            }

            //   7)拷贝窗口到设备上下文:PrintWindow
            if (PrintWindow(WxProcess.MainWindowHandle, compatibleDCHandle, 0) == 0)
            {
                MessageBox.Show("拷贝窗口到设备上下文失败!");
                return;
            }

            this.pictureBox1.Width = width;
            this.pictureBox1.Height = height;
            this.pictureBox1.Image = Image.FromHbitmap(compatibleBitmapHandle);

            this.pictureBox1.Image.Save(Environment.CurrentDirectory + "\\Wx.png", ImageFormat.Png);

            int qrWidth = 197;
            int qrHeight = 197;

            Image qrImage = new Bitmap(qrWidth, qrHeight);
            Graphics graphics = Graphics.FromImage(qrImage);
            graphics.DrawImage(Image.FromHbitmap(compatibleBitmapHandle),
                new Rectangle(0, 0, qrWidth, qrHeight),
                new Rectangle(40, 80, qrWidth, qrHeight),
                GraphicsUnit.Pixel);
            graphics.Dispose();

            this.pictureBox2.Width = qrWidth;
            this.pictureBox2.Height = qrHeight;
            this.pictureBox2.Image = qrImage;


            //   8)清理垃圾
            DeleteObject(compatibleBitmapHandle);
            DeleteDC(compatibleDCHandle);
            ReleaseDC(WxProcess.MainWindowHandle, windowDCHandle);

            IBarcodeReader reader = new BarcodeReader();
            var barcodeBitmap = (Bitmap)(this.pictureBox1.Image);
            var result = reader.Decode(barcodeBitmap);
            if (result != null)
            {
                this.textBox1.Text = result.Text;
            }

            BarcodeWriter barcodeWriter = new BarcodeWriter();
            barcodeWriter.Format = BarcodeFormat.QR_CODE;

            QrCodeEncodingOptions options = new QrCodeEncodingOptions();
            options.Width = 150;
            options.Height = 150;
            options.Margin = 2;

            barcodeWriter.Options = options;
            Bitmap bitmap = barcodeWriter.Write(this.textBox1.Text);
            this.pictureBox3.Width = 150;
            this.pictureBox3.Height = 150;
            this.pictureBox3.Image = bitmap;



        }

        [DllImport("Gdi32.dll")]
        public static extern int DeleteDC(IntPtr hdc);

        [DllImport("Gdi32.dll")]
        public static extern int DeleteObject(IntPtr ho);

        [DllImport("User32.dll")]
        public static extern int PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);

        [DllImport("Gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hdc, IntPtr h);

        [DllImport("Gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        [DllImport("Gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int cx, int cy);

        [DllImport("User32.dll")]
        public static extern int GetWindowRect(IntPtr hWnd, ref Rectangle lpRect);

        [DllImport("User32.dll")]
        public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("User32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr hWnd);
    }

}

示例来源:
网易云课堂《2019 PC 微信探秘》

交流QQ群:

456197310 PC微信HOOK逆向分析

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵庆明老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值