SharpShell Documentation
repository·main·Indexed 23 days ago
https://github.com/dwmkerr/sharpshellA .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.
What's inside SharpShell
- SharpShellNativeBridge is a Win32 library designed to host APIs that cannot be directly hosted within the .NET SharpShell library. At runtime, the core SharpShell Assembly loads this native bridge into memory to facilitate calls to these external APIs.
Accessing the current directory in DirectoryBackground handlers
mainWhen using a
DirectoryBackgroundCOM Server Association, the directory you are currently viewing is not included in theSelectedItemPathscollection because it is not technically 'selected'.To retrieve the path of the directory the user is currently in, use the
FolderPathproperty instead.Understand Octokit's client hierarchy
mainOctokit's client structure mirrors the GitHub API documentation. Clients are often organized as "sub-clients" rather than direct properties of the main client.
For example, instead of
Milestonesbeing a direct property of the main client, it is a property of theIssuesclient. You access it viaclient.Issues.Milestones.Core Concepts of SharpShell Server Registration
mainSharpShell servers are assemblies containing a COM server. To be used as shell extensions, they must be registered. There are two primary registration strategies:
- GAC Registration: Install the assembly into the Global Assembly Cache (GAC) using
gacutil, then register it as a COM server. - Codebase Registration: Leave the assembly as a loose file in the filesystem and register it using the
/codebaseoption. This is often preferred for keeping application files together in folders likeProgram 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).- GAC Registration: Install the assembly into the Global Assembly Cache (GAC) using
Considerations for using the CLR for Shell Extensions
mainThere 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.
Install a SharpShell server using regasm
mainYou can use the standard Microsoft
regasmtool to install SharpShell servers.Using the GAC: First install the assembly into the GAC with
gacutil, then runregasm.Not using the GAC: Use the
/codebaseflag to register the assembly as a loose file.Uninstalling: Use the
/uflag 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.dllBuild SharpShellNativeBridge
mainTo build the project, ensure you have the required Windows SDK components installed. You can build by opening the
SharpShellNativeBridge.slnsolution 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.ps1Install a SharpShell server using srm
mainThe Server Registration Manager (
srm.exe) is a dedicated command-line tool for managing SharpShell server installations, uninstalls, and diagnostics.- Install (using the GAC): Use
gacutilto install to the GAC, then use theinstallverb withsrm. - Install (not using the GAC): Use the
installverb with the-codebaseflag. - Uninstall: Use the
uninstallverb.
# Install (using the GAC) gacutil -i <serverpath> srm install <serverpath> # Install (not using the GAC) srm install <serverpath> -codebase # Uninstall srm uninstall <serverpath>- Install (using the GAC): Use
Configure SharpShell logging via Registry
mainBy 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.exeprocess for changes to take effect.Registry Path:
HKEY_LOCAL_MACHINE\Software\SharpShellConfiguration 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 Output2: Windows Event Log4: File Output
Example: To enable both Debug Output and File logging, set
LoggingModeto5(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 |Install the Server Registration Manager (SRM) tool
mainThe Server Registration Manager tool (
srm.exe) is a standalone console application used for administrative tasks like installing/uninstalling servers and managing SharpShell configuration.You can obtain the tool via:
Set up a Desk Band Extension project
mainTo build a Desk Band extension using SharpShell, create a new .NET Class Library project and configure the following requirements:
- Add References: Include
System.Windows.FormsandSystem.Drawing. - Install SharpShell: Use the NuGet Package Manager console to add the SharpShell package.
- 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- Add References: Include
Configure Shortcut Keys for menu items
mainWhen setting the
ShortcutKeysproperty on aToolStripMenuItem, 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 aSystem.ComponentModel.InvalidEnumArgumentException. To avoid this, use bitwise OR to include a modifier, such asKeys.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 };