My Icon Handler crashes Explorer

845 views Asked by At

I have a project using SharpShell for an Icon Handler. It peeks inside APKs in order to find and display their icon. I've got it working but there are a few side effects. If I try to rename an APK to something else, say from A.apk to B.apk, then Explorer crashes and I can't find a reason for why.

Here's the main section and the logging bits:

using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using ApkDetails;
using SharpShell.Attributes;
using SharpShell.SharpIconHandler;

namespace ApkIconHandler {
    [ComVisible(true)]
    [COMServerAssociation(AssociationType.ClassOfExtension, ".apk")]
    public class ApkHandler : SharpIconHandler {
        protected override Icon GetIcon(bool smallIcon, uint iconSize) {
            try {
                APK apk = new APK(SelectedItemPath);
                using (Bitmap icon = apk.GetIcon()) {
                    IntPtr icH = icon.GetHicon();
                    Icon ico = Icon.FromHandle(icH);
                    return ico;
                }
            } catch (Exception ex) {
                File.AppendAllText(@"C:\APKError.txt", ex.Message + Environment.NewLine + ex.StackTrace);
                return Icons.DefaultIcon;
            }
        }

        protected override void LogError(string message, Exception exception = null) {
            base.LogError(message, exception);
            String err = message;
            if (exception != null) err += Environment.NewLine + exception.StackTrace;
            File.AppendAllText(@"C:\APKError.txt", err + Environment.NewLine);
        }

        protected override void Log(string message) {
            base.Log(message);
            File.AppendAllText(@"C:\APKLog.txt", message + Environment.NewLine);
        }
    }
}

My APK code runs aapt d badging "path.apk" and parses the output to get icon information and GetIcon opens the APK as a zip to get that file. I wrap it all in a try/catch and tied into SharpShell's LogError method but I never get any output. I disabled UAC. I don't think there's a permissions issue on writing to the root of the drive because APKLog.txt DOES show up but with no useful information.

I get the window "Windows Explorer has stopped working" and it lists this information as the "Problem signature"

  Problem Event Name:   CLR20r3
  Problem Signature 01: explorer.exe
  Problem Signature 02: 6.1.7601.17567
  Problem Signature 03: 4d672ee4
  Problem Signature 04: mscorlib
  Problem Signature 05: 4.0.30319.18047
  Problem Signature 06: 5155314f
  Problem Signature 07: 326
  Problem Signature 08: 5d
  Problem Signature 09: System.AccessViolationException
  OS Version:   6.1.7601.2.1.0.256.48
  Locale ID:    1033
  Additional Information 1: 59cf
  Additional Information 2: 59cf5fdf81b829ceb8b613f1f092df60
  Additional Information 3: 6d90
  Additional Information 4: 6d903bfb0ab1d4b858654f0b33b94d7f

I see that it says System.AccessViolationException but none of my code is catching that. Can somebody offer a little insight into what's going on here?

1

There are 1 answers

0
Dave Kerr On

Can I suggest making sure that you've built the server in debug mode and then running the 'server manager' tool from sharpshell. Once you've done that, enable logging from the tools menu. From this point on, any time that the sharpshell code catches an exception that may have been thrown from your code, it'll log it to the event log.

You can use LogMessage and LogError in your own SharpShell server to write messages or errors to the event log, this'll make it easier to diagnose what's going on!