SelectWechat.xaml.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Shapes;
  18. using System.Xml.Linq;
  19. using WechatPCMsgBakTool.Helpers;
  20. using WechatPCMsgBakTool.Model;
  21. namespace WechatPCMsgBakTool
  22. {
  23. /// <summary>
  24. /// SelectWechat.xaml 的交互逻辑
  25. /// </summary>
  26. public partial class SelectWechat : Window
  27. {
  28. List<ProcessInfo> processInfos = new List<ProcessInfo>();
  29. public ProcessInfo? SelectProcess { get; set; } = null;
  30. public SelectWechat()
  31. {
  32. InitializeComponent();
  33. //GetWechatProcess();
  34. GetWechatProcessInfos();
  35. list_process.ItemsSource = processInfos;
  36. }
  37. private void GetWechatProcessInfos()
  38. {
  39. processInfos.Clear();
  40. Process[] processes = Process.GetProcessesByName("wechat");
  41. foreach (Process p in processes)
  42. {
  43. File.AppendAllText("debug.log", "wechat=>" + p.Id + "\r\n");
  44. var lHandles = NativeAPIHelper.GetHandleInfoForPID((uint)p.Id);
  45. foreach (var h in lHandles)
  46. {
  47. string name = NativeAPIHelper.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.ObjectTypeIndex, h.HandleValue, 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. }