NEWS

[C#] Hướng dẫn lấy thông tin Your ID và Password của Teamviewer Winform

[C#] Hướng dẫn lấy thông tin Your ID và Password của Teamviewer Winform
Đăng bởi: Thảo Meo - Lượt xem: 4381 09:17:02, 16/03/2021DEVEXPRESS   In bài viết

Lại là mình Thảo Meo đây các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn cách lấy Your ID và Password trên Teamviewer C# Winform.

[C#] Get Your ID And Password Teamviewer Winform

Vậy chúng ta cần lấy thông tin từ ứng dụng này nhằm mục đích gì?

Mình có một ví dụ như sau:

Mình phát triển một phần mềm và có tích hợp sẵn ứng dụng Teamviewer Portable kèm theo ứng dụng sản phẩm của mình.

Khi khách hàng báo lỗi ứng dụng, thì mình có thể dễ dàng gọi App lên mở Teamviewer và đọc thông tin Your ID và Password và gởi về cho mình.

Giúp mình dễ dàng Remote từ xa một cách nhanh chóng.

Dưới đây là giao diện Lấy YourID và Password Teamviewer c#, Winform:

get_id_and_password_teamviewer

Chi tiết ứng dụng các bạn xem video demo của mình:

Đầu tiên các bạn tạo cho mình WindowsApi.cs C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace GetIDAndPassTeamViewer
{
    public class WindowsApi
    {
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);


        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, StringBuilder lParam);

        [DllImport("user32.dll", EntryPoint = "GetWindowText")]
        public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int cch);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr GetWindow(IntPtr hWnd, GetWindowCmd uCmd);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    }
    public enum GetWindowCmd : uint
    {
        GW_HWNDFIRST = 0,
        GW_HWNDLAST = 1,
        GW_HWNDNEXT = 2,
        GW_HWNDPREV = 3,
        GW_OWNER = 4,
        GW_CHILD = 5,
        GW_ENABLEDPOPUP = 6
    }
}

Tiếp theo, các bạn tạo thêm một class TeamviewerHelper.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace GetIDAndPassTeamViewer
{
    class TeamviewerHelper
    {
        private static Regex userReg;

        static TeamviewerHelper()
        {
            userReg = new Regex(@"d+ d+ d+", RegexOptions.Singleline | RegexOptions.Compiled);
        }
        public TeamviewerHelper()
        {
            Username = string.Empty;
            Password = string.Empty;
            Holder = string.Empty;
        }
        internal int _count;
        public string Username;
        public string Password;
        public string Holder;

        public static TeamviewerHelper GetUser()
        {
            TeamviewerHelper user = new TeamviewerHelper();
            IntPtr tvHwnd = WindowsApi.FindWindow(null, "TeamViewer");
            if (tvHwnd != IntPtr.Zero)
            {
                IntPtr winParentPtr = WindowsApi.GetWindow(tvHwnd, GetWindowCmd.GW_CHILD);
                while (winParentPtr != IntPtr.Zero)
                {

                    IntPtr winSubPtr = WindowsApi.GetWindow(winParentPtr, GetWindowCmd.GW_CHILD);
                    while (winSubPtr != IntPtr.Zero)
                    {
                        StringBuilder controlName = new StringBuilder(512);                     
                        WindowsApi.GetClassName(winSubPtr, controlName, controlName.Capacity);

                        if (controlName.ToString() == "Edit")
                        {
                            StringBuilder winMessage = new StringBuilder(512);                           
                            WindowsApi.SendMessage(winSubPtr, 0xD, (IntPtr)winMessage.Capacity, winMessage);
                            string message = winMessage.ToString();
                            if (userReg.IsMatch(message))
                            {
                                user.Username = message;
                                user._count += 1;

                            }
                            else if (user.Password != string.Empty)
                            {
                                user.Holder = message;
                                user._count += 1;
                            }
                            else
                            {
                                user.Password = message;
                                user._count += 1;
                            }
                            if (user._count == 3)
                            {
                                return user;
                            }
                        }
                        winSubPtr = WindowsApi.GetWindow(winSubPtr, GetWindowCmd.GW_HWNDNEXT);
                    }                 
                    winParentPtr = WindowsApi.GetWindow(winParentPtr, GetWindowCmd.GW_HWNDNEXT);
                }
            }
            return user;
        }


        public static Dictionary<string, string> GetInfos(params string[] keys)
        {
            IntPtr tvHwnd = WindowsApi.FindWindow(null, "TeamViewer");
            var sets = new HashSet<string>();
            sets.UnionWith(keys);
            return CheckPtrInfo(tvHwnd, sets);
        }

        public static Dictionary<string, string> CheckPtrInfo(IntPtr tvHwnd, HashSet<string> keys)
        {
            Dictionary<string, string> result = null;
            if (tvHwnd != IntPtr.Zero)
            {

                tvHwnd = WindowsApi.GetWindow(tvHwnd, GetWindowCmd.GW_CHILD);

                if (tvHwnd != IntPtr.Zero)
                {

                    var message = GetMessage(tvHwnd);
                    if (keys.Contains(message))
                    {
                        tvHwnd = WindowsApi.GetWindow(tvHwnd, GetWindowCmd.GW_HWNDNEXT);
                        var text = GetMessage(tvHwnd);
                        if (result == null)
                        {
                            result = new Dictionary<string, string>();
                        }
                        result[message] = text;
                    }

                    while (tvHwnd != IntPtr.Zero)
                    {
                        var tempReuslt = CheckPtrInfo(tvHwnd, keys);
                        if (tempReuslt != null)
                        {
                            if (result == null)
                            {
                                result = tempReuslt;
                            }
                            else
                            {
                                foreach (var item in tempReuslt)
                                {
                                    result[item.Key] = item.Value;
                                }
                            }
                        }
                        tvHwnd = WindowsApi.GetWindow(tvHwnd, GetWindowCmd.GW_HWNDNEXT);
                    }

                }
            }

            return result;
        }

        public static string GetMessage(IntPtr tvHwnd)
        {
            StringBuilder winMessage = new StringBuilder(512);
            WindowsApi.SendMessage(tvHwnd, 0xD, (IntPtr)winMessage.Capacity, winMessage);
            return winMessage.ToString();
        }
    }
}

Và cuối cùng chúng ta gọi class TeamviewerHelper để sử dụng:

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

namespace GetIDAndPassTeamViewer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            Process.Start(@"C:Program Files (x86)TeamViewerTeamViewer.exe");
            GetIDAndPassTeamViewer();
        }

        void GetIDAndPassTeamViewer()
        {
            var dicInfo = TeamviewerHelper.GetInfos("Your ID", "Password");
            if (dicInfo != null)
            {
                string yourID = "";
                string password = "";
                dicInfo.TryGetValue("Your ID", out yourID);
                dicInfo.TryGetValue("Password", out password);
                txtYourID.Text = yourID;
                txtPassword.Text = password;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            GetIDAndPassTeamViewer();
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn lấy thông tin Your ID và Password của Teamviewer Winform
Đăng bởi: Thảo Meo - Lượt xem: 4381 09:17:02, 16/03/2021DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.