捕获错误。修复找不到微信注册表路径时,程序闪退的问题

This commit is contained in:
huiyadanli 2019-08-01 23:02:28 +08:00
parent 22b8138283
commit 43ce6ecb96
3 changed files with 70 additions and 44 deletions

View File

@ -34,7 +34,9 @@
## :book:使用方法
**请以管理员身份运行本程序,由于修改了微信的 `WeChatWin.dll` 文件,杀毒软件可能会弹出警告,放行即可**
**请以管理员身份运行本程序,由于修改了微信的 `WeChatWin.dll` 文件,杀毒软件可能会弹出警告,放行即可。**
启动时会自动从注册表中获取微信的安装路径,如果没找到,需要手动选择微信路径。
环境要求:

View File

@ -14,45 +14,62 @@ namespace RevokeMsgPatcher
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
#if DEBUG
Application.Run(new FormMain());
#else
//当前用户是管理员的时候,直接启动应用程序
//如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
//获得当前登录的Windows用户标示
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
//如果是管理员,则直接运行
Application.Run(new FormMain());
}
else
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
try
#else
//当前用户是管理员的时候,直接启动应用程序
//如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
//获得当前登录的Windows用户标示
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
System.Diagnostics.Process.Start(startInfo);
//如果是管理员,则直接运行
Application.Run(new FormMain());
}
catch
else
{
return;
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
try
{
System.Diagnostics.Process.Start(startInfo);
}
catch
{
return;
}
//退出
Application.Exit();
}
//退出
Application.Exit();
}
#endif
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show((e.ExceptionObject as Exception).Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

View File

@ -17,25 +17,32 @@ namespace RevokeMsgPatcher
public static string AutoFindInstallPath()
{
// 微信的注册表路径
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\WeChat");
object installLocation = key.GetValue("InstallLocation");
key.Close();
if (installLocation == null || string.IsNullOrEmpty(installLocation.ToString()) || !IsWechatInstallPath(installLocation.ToString()))
try
{
// 从默认安装目录查找
string[] drives = Environment.GetLogicalDrives(); //获取当前计算机逻辑磁盘名称列表
foreach (string d in drives)
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\WeChat");
object installLocation = key.GetValue("InstallLocation");
key.Close();
if (installLocation == null || string.IsNullOrEmpty(installLocation.ToString()) || !IsWechatInstallPath(installLocation.ToString()))
{
string assertPath = Path.Combine(d, @"Program Files (x86)\Tencent\WeChat");
if (IsWechatInstallPath(assertPath))
// 从默认安装目录查找
string[] drives = Environment.GetLogicalDrives(); //获取当前计算机逻辑磁盘名称列表
foreach (string d in drives)
{
return assertPath;
string assertPath = Path.Combine(d, @"Program Files (x86)\Tencent\WeChat");
if (IsWechatInstallPath(assertPath))
{
return assertPath;
}
}
}
else
{
return installLocation.ToString();
}
}
else
catch(Exception e)
{
return installLocation.ToString();
Console.WriteLine(e.Message);
}
return null;
}