SharpShell Documentation

repository·main·Indexed 23 days ago

https://github.com/dwmkerr/sharpshell

A .NET library for creating Windows Shell extensions, including Shell Context Menus, Desk Bands, and Property Sheet Extensions. Includes documentation on COM server associations, strong-name signing requirements, and the SharpShellNativeBridge Win32 library for hosting external APIs.

Tokens
9K
Snippets
24
Records
52
Agent score
82%

What's inside SharpShell

  1. Accessing the current directory in DirectoryBackground handlers

    main

    When using a DirectoryBackground COM Server Association, the directory you are currently viewing is not included in the SelectedItemPaths collection because it is not technically 'selected'.

    To retrieve the path of the directory the user is currently in, use the FolderPath property instead.

  2. Core Concepts of SharpShell Server Registration

    main

    SharpShell servers are assemblies containing a COM server. To be used as shell extensions, they must be registered. There are two primary registration strategies:

    1. GAC Registration: Install the assembly into the Global Assembly Cache (GAC) using gacutil, then register it as a COM server.
    2. Codebase Registration: Leave the assembly as a loose file in the filesystem and register it using the /codebase option. This is often preferred for keeping application files together in folders like Program Files.

    Bitness Requirement: Most shell extensions run 'in-proc' within explorer.exe. Therefore, you must register your server with the same bitness as your operating system (32-bit or 64-bit).

  3. Considerations for using the CLR for Shell Extensions

    main

    There is conflicting guidance regarding whether the Common Language Runtime (CLR) should be used to develop in-process Shell Extensions.

    • Official MSDN Guidance: Generally recommends against using the CLR for in-process extensions.
    • Expert Opinion: Raymond Chen (Microsoft developer) advises against using the CLR for this purpose.
    • Counter-evidence: Microsoft's 'All-In-One Code Framework' provides samples for Managed Shell Extensions in C# and VB, and some MSDN resources suggest it is possible.

    Recommendation: For critical scenarios where stability and adherence to official Microsoft guidance are paramount, use C or C++. Use SharpShell (Managed Shell Extensions) if you are willing to trial the approach and accept the potential risks associated with CLR/Native interoperability in the shell.

  4. Install a SharpShell server using regasm

    main

    You can use the standard Microsoft regasm tool to install SharpShell servers.

    Using the GAC: First install the assembly into the GAC with gacutil, then run regasm.

    Not using the GAC: Use the /codebase flag to register the assembly as a loose file.

    Uninstalling: Use the /u flag to remove the registration.

    # Install (using the GAC)
    gacutil -i ExampleContextMenuExtension.dll
    regasm ExampleContextMenuExtension.dll
    
    # Install (not using the GAC)
    regasm /codebase ExampleContextMenuExtension.dll
    
    # Uninstall
    regasm /u ExampleContextMenuExtension.dll
  5. Build SharpShellNativeBridge

    main

    To build the project, ensure you have the required Windows SDK components installed. You can build by opening the SharpShellNativeBridge.sln solution in Visual Studio 2019 (or later) or by using the provided PowerShell script.

    Requirements:

    • Windows 10 SDK
    • Windows Universal CRT SDK
    • Windows Universal C Runtime

    Note: Windows 8.1 is no longer supported. If you need to target older systems, you must manually install legacy SDKs and re-target the project.

    ./build.ps1
  6. Install a SharpShell server using srm

    main

    The Server Registration Manager (srm.exe) is a dedicated command-line tool for managing SharpShell server installations, uninstalls, and diagnostics.

    • Install (using the GAC): Use gacutil to install to the GAC, then use the install verb with srm.
    • Install (not using the GAC): Use the install verb with the -codebase flag.
    • Uninstall: Use the uninstall verb.
    # Install (using the GAC)
    gacutil -i <serverpath>
    srm install <serverpath>
    
    # Install (not using the GAC)
    srm install <serverpath> -codebase
    
    # Uninstall
    srm uninstall <serverpath>
  7. Configure SharpShell logging via Registry

    main

    By default, SharpShell servers do not log any information. Logging is configured via the Windows Registry. Note that log settings are read on server startup; if a server is already running, you must restart the explorer.exe process for changes to take effect.

    Registry Path: HKEY_LOCAL_MACHINE\Software\SharpShell

    Configuration Keys:

    • LoggingMode (Type: DWORD): A bitmask representing the desired logging modes.
    • LogPath (Type: String): The file path where logs should be saved (required if using File logging mode).

    Logging Mode Values (Bitmask):

    • 1: Debug Output
    • 2: Windows Event Log
    • 4: File Output

    Example: To enable both Debug Output and File logging, set LoggingMode to 5 (1 + 4).

    Registry Key: HKEY_LOCAL_MACHINE\Software\SharpShell
    
    | Value Name | Value Type | Notes |
    |-------------|-------------|-------|
    | `LoggingMode` | `DWORD` | Any combination of the 'Logging Modes' below. |
    | `LogPath` | `String` | The path to save the log file to, if the File logging mode is used. |
    
    | Value | Output |
    |-------|--------|
    | `1` | Debug |
    | `2` | Windows Event Log |
    | `4` | File |
  8. Set up a Desk Band Extension project

    main

    To build a Desk Band extension using SharpShell, create a new .NET Class Library project and configure the following requirements:

    1. Add References: Include System.Windows.Forms and System.Drawing.
    2. Install SharpShell: Use the NuGet Package Manager console to add the SharpShell package.
    3. Sign the Assembly: Desk Band extensions must have a strong name to be registered. In your project properties, navigate to the 'Signing' section and select 'Sign the Assembly'.
    Install-Package SharpShell
  9. Configure Shortcut Keys for menu items

    main

    When setting the ShortcutKeys property on a ToolStripMenuItem, the key must be a valid shortcut, which typically requires a modifier key (e.g., Alt, Control).

    Setting a shortcut key without a modifier, such as Keys.C, will result in a System.ComponentModel.InvalidEnumArgumentException. To avoid this, use bitwise OR to include a modifier, such as Keys.Alt | Keys.C.

    // This will fail with InvalidEnumArgumentException
    new ToolStripMenuItem
    {
        Text = "Count Lines...",
        Image = Properties.Resources.CountLines,
        ShortcutKeys = Keys.C
    };
    
    // This will succeed
    new ToolStripMenuItem
    {
        Text = "Count Lines...",
        Image = Properties.Resources.CountLines,
        ShortcutKeys = Keys.Alt | Keys.C
    };