如何在SSMS 2017加载完成后自动加载DLL扩展及获取[ProvideAutoLoad]参数
Hey Ron, let's tackle your two questions one by one—here's how to get your DLL extensions loaded properly in SSMS 2017:
1. Loading a DLL Extension After SSMS 2017 Finishes Starting Up
If you need a straightforward way to load your DLL once SSMS is fully up and running, you have two main options:
Manual Loading (Great for Testing)
Once SSMS is open, head toTools>Extensions and Updates. From the left pane, pickInstalled>Manage Extensions, then clickInstalland select your DLL file. Restart SSMS, and your extension will be active. This is perfect for quick tests but requires manual steps each time.Registry-Based Loading
To avoid manual clicks, you can configure the Windows Registry to load your DLL automatically after SSMS starts. Navigate to this registry path:HKEY_CURRENT_USER\Software\Microsoft\SQL Server Management Studio\14.0_Config\AddIns
(Note: SSMS 2017 uses version 14.0, so that's why the path includes14.0_Config.)Create a new subkey named after your extension, then add these string values to it:
LoadBehavior: Set to3(tells SSMS to load the extension on startup)CommandLineSafe: Set to0Description: A short description of your extensionName: Your extension's display nameAssembly: The full file path to your DLL (e.g.,C:\SSMSExtensions\MyCustomExtension.dll)
Keep in mind this loads the extension as SSMS starts—if you need it to trigger right after SSMS is fully loaded (not just during startup), jump to the second question's solution.
2. Automatically Loading the DLL Immediately After SSMS 2017 Fully Loads (With Event Registration)
To make your DLL load the moment SSMS finishes initializing all its core services, you'll use the ProvideAutoLoad attribute from the Visual Studio Shell framework (since SSMS 2017 is built on the VS 2017 shell). Here's how to do it:
Step-by-Step Implementation
Reference Required Assemblies
In your DLL project, add references to these SSMS/VS Shell assemblies:Microsoft.SqlServer.Management.UI.VSIntegration.dllMicrosoft.VisualStudio.Shell.15.0.dll
Use
ProvideAutoLoadwith the Correct GUID
The key is to target the UI context that fires when SSMS is fully initialized. For SSMS 2017, use theShellInitializedcontext GUID:{ADFC4E64-0397-11D1-9F4E-00A0C911004F}. This ensures your DLL loads only after SSMS has finished setting up its main window and core features.Here's a code example for your extension package class:
using Microsoft.VisualStudio.Shell; using Microsoft.SqlServer.Management.UI.VSIntegration; using System.Windows.Forms; [PackageRegistration(UseManagedResourcesOnly = true)] [ProvideAutoLoad("{ADFC4E64-0397-11D1-9F4E-00A0C911004F}")] // Triggers on full SSMS load [Guid("YOUR-EXTENSION-GUID-HERE")] // Replace with your own unique GUID public class MySSMSExtension : Package { protected override void Initialize() { base.Initialize(); // This code runs right after SSMS is fully loaded // Add your extension logic here—register menus, tools, etc. MessageBox.Show("My extension loaded successfully after SSMS startup!"); } }Deploy the DLL
Compile your project, then place the DLL in SSMS's extension directory:C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\Extensions
Alternatively, use the registry method from question 1 to specify the DLL path.
Quick Verification
Add a simple MessageBox (like in the example) to the Initialize method. When you start SSMS, you'll see the pop-up right after the main window fully loads—confirming your DLL was loaded at the correct time.
内容的提问来源于stack exchange,提问作者ron

