SelectWechat.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Management;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Shapes;
  19. using System.Xml.Linq;
  20. using WechatPCMsgBakTool.Helpers;
  21. using WechatPCMsgBakTool.Model;
  22. namespace WechatPCMsgBakTool
  23. {
  24. /// <summary>
  25. /// SelectWechat.xaml 的交互逻辑
  26. /// </summary>
  27. public partial class SelectWechat : Window
  28. {
  29. List<ProcessInfo> processInfos = new List<ProcessInfo>();
  30. public ProcessInfo? SelectProcess { get; set; } = null;
  31. public SelectWechat()
  32. {
  33. InitializeComponent();
  34. //GetWechatProcess();
  35. GetWechatProcessInfos();
  36. list_process.ItemsSource = processInfos;
  37. }
  38. private void GetWechatProcessInfos()
  39. {
  40. processInfos.Clear();
  41. Process[] processes = Process.GetProcessesByName("wechat");
  42. foreach (Process p in processes)
  43. {
  44. var h_list = ProcessHelper.GetHandles(p);
  45. foreach (var h in h_list)
  46. {
  47. string name = ProcessHelper.FindHandleName(h, p);
  48. if (name != "")
  49. {
  50. // 预留handle log
  51. if (File.Exists("handle.log"))
  52. {
  53. File.AppendAllText("handle.log", string.Format("{0}|{1}|{2}|{3}\n", p.Id, h.ObjectType, h.Handle, name));
  54. }
  55. if (name.Contains("\\MicroMsg.db") && name.Substring(name.Length - 3, 3) == ".db")
  56. {
  57. ProcessInfo info = new ProcessInfo();
  58. info.ProcessId = p.Id.ToString();
  59. info.ProcessName = p.ProcessName;
  60. info.DBPath = DevicePathMapper.FromDevicePath(name);
  61. processInfos.Add(info);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. public void GetWechatProcess()
  68. {
  69. Process p = new Process();
  70. p.StartInfo.FileName = "tools/handle64.exe";
  71. p.StartInfo.Arguments = "-p wechat.exe";
  72. p.StartInfo.UseShellExecute = false;
  73. p.StartInfo.CreateNoWindow = true;
  74. p.StartInfo.RedirectStandardOutput = true;
  75. p.Start();
  76. string i = p.StandardOutput.ReadToEnd();
  77. if (i.Contains("SYSINTERNALS SOFTWARE LICENSE TERMS"))
  78. {
  79. MessageBox.Show("请先同意Handle64的使用协议,同意后关闭弹窗重新打开新增工作区即可");
  80. Process p1 = new Process();
  81. p1.StartInfo.FileName = "tools/handle64.exe";
  82. p1.StartInfo.Arguments = "-p wechat.exe";
  83. p1.Start();
  84. }
  85. string[] lines = i.Split(new string[] { "\r\n" }, StringSplitOptions.None);
  86. bool hitFind = false;
  87. ProcessInfo processInfo = new ProcessInfo();
  88. foreach (string line in lines)
  89. {
  90. if (line.Length < 6)
  91. continue;
  92. if (line.Substring(0, 6).ToLower() == "wechat")
  93. {
  94. hitFind = true;
  95. processInfo = new ProcessInfo();
  96. string[] lineInfo = line.Split(' ');
  97. processInfo.ProcessName = lineInfo[0];
  98. processInfo.ProcessId = lineInfo[2];
  99. }
  100. if (hitFind)
  101. {
  102. if (line.Substring(line.Length - 11, 11) == "MicroMsg.db")
  103. {
  104. Regex regex = new Regex("[a-zA-Z]:\\\\([a-zA-Z0-9() ]*\\\\)*\\w*.*\\w*");
  105. string path = regex.Match(line).Value;
  106. processInfo.DBPath = path;
  107. processInfos.Add(processInfo);
  108. hitFind = false;
  109. }
  110. }
  111. }
  112. list_process.ItemsSource = processInfos;
  113. }
  114. private void list_process_SelectionChanged(object sender, SelectionChangedEventArgs e)
  115. {
  116. SelectProcess = list_process.SelectedItem as ProcessInfo;
  117. if(SelectProcess != null)
  118. {
  119. string[] name_raw = SelectProcess.DBPath.Split("\\");
  120. txt_username.Text = name_raw[name_raw.Length - 3];
  121. }
  122. }
  123. private void btn_close_Click(object sender, RoutedEventArgs e)
  124. {
  125. if (SelectProcess != null)
  126. {
  127. SelectProcess.Account = txt_username.Text;
  128. }
  129. Close();
  130. }
  131. }
  132. }