How to: Create a C# Test Project Add-In 

The following sections explain how to create and run C# project add-in for trapping events in Visual SourceSafe.

Create a C# Test Project Add-In

To create a test project in C# add-in that demonstrates how to trap the SourceSafe Events, do the following:

  1. Open Visual Studio and, from the Start page, choose New Project.

  2. Select Visual C# Projects from the tree view on the left side of the screen.

  3. Select Class Library as the project template.

  4. Set the name of the application to VSSEvents and click OK to create the project.

  5. Highlight the file called Class1.cs in the Solution Explorer window and rename it to IvssEventSample.cs. Completely delete the code inserted by Visual Studio into this file.

  6. In project properties Build Configuration set Register for COM Interop to true.

  7. To use the Runtime Callable Wrapper (RCW) for the SSAPI.DLL COM component, select References under VSSEvents Project, use right mouse button and then select Add Reference. Make sure that the .NET tab is selected, and use Select button to select IVSS Microsoft.VisualStudio.SourceSafe.Interop.dll PIA. This will add the component to the Selected Components list. Click OK.

  8. To have access to System.Windows.Forms namespace classes, select References under VSSEvents Project, use right mouse button and then select Add Reference. Make sure that the .NET tab is selected, and use Select button to select System.Windows.Forms.dll. This will add the component to the Selected Components list. Click OK.

Add Sample Code

The following example demonstrates how to trap VSS events

  • Paste the code below into IvssEventSample.cs:

    using System;
    using System.Runtime.InteropServices;
    using System.Runtime.InteropServices.ComTypes;
    using System.Windows.Forms;
    using Microsoft.VisualStudio.SourceSafe.Interop;
    
    namespace IvssEventSample 
    {
        // Use attributes to declare a fixed Prog ID and Guid for the Add-In.
        [ProgId("Microsoft.SourceSafe.EventSample")]
        [Guid("E1916BD8-A8AA-47b7-A862-C1AF995E64EF")]
        [ComVisible(true)]
        public class IvssEventSample : IVSSEventHandler, IVSSEvents 
        {
            private VSSApp vssApp;
            IConnectionPoint vssEvents;
            int cookie;
    
            public IvssEventSample() 
            {
            }
    
            ~IvssEventSample() 
            {}
    
            public void Init(VSSApp app) 
            {
                MessageBox.Show("Init");
    
                // by saving the VSSApp pointer you can drive the database during events
                this.vssApp = app;
    
                // Wire up the COM connection point manually
                IConnectionPointContainer cpc = (IConnectionPointContainer) app;
                Guid guid = typeof(IVSSEvents).GUID;
                cpc.FindConnectionPoint(ref guid, out vssEvents);
                vssEvents.Advise(this, out cookie);
            }
    
            public void AfterAdd(VSSItem vssItem, string localSpec, string comment) 
            {
                MessageBox.Show("AfterAdd");
            }
    
            public void AfterBranch(VSSItem vssItem, string comment) 
            {
                MessageBox.Show("AfterBranch");
            }
    
            public void AfterCheckin(VSSItem vssItem, string localSpec, string comment) 
            {
                MessageBox.Show("AfterCheckin");
            }
    
            public void AfterCheckout(VSSItem vssItem, string localSpec, string comment) 
            {
                MessageBox.Show("AfterCheckout");
            }
    
            public void AfterEvent(int eventNum, VSSItem vssItem, string str, object var) 
            {
                // Use of this event is deprecated
            }
    
            public void AfterRename(VSSItem vssItem, string oldName) 
            {
                MessageBox.Show("AfterRename");
            }
    
            public void AfterUndoCheckout(VSSItem vssItem, string localSpec) 
            {
                MessageBox.Show("AfterUndoCheckout");
            }
    
            public bool BeforeAdd(VSSItem vssItem, string localSpec, string comment) 
            {
                MessageBox.Show("BeforeAdd");
                return true;
            }
    
            public bool BeforeBranch(VSSItem vssItem, string comment) 
            {
                MessageBox.Show("BeforeBranch");
                return true;
            }
    
            public bool BeforeCheckin(VSSItem vssItem, string localSpec, string comment) 
            {
                MessageBox.Show("BeforeCheckin");
                return true;
            }
    
            public bool BeforeCheckout(VSSItem vssItem, string localSpec, string comment) 
            {
                MessageBox.Show("BeforeCheckout");
                return true;
            }
    
            public bool BeforeEvent(int eventNum, VSSItem vssItem, string str, object var) 
            {
                // Use of this event is deprecated
                return true;
            }
    
            public bool BeforeRename(VSSItem vssItem, string oldName) 
            {
                MessageBox.Show("BeforeRename");
                return true;
            }
    
            public bool BeforeUndoCheckout(VSSItem vssItem, string localSpec) 
            {
                MessageBox.Show("BeforeUndoCheckout");
                return true;
            }
    
            public bool BeginCommand(int unused) 
            {
                // use of this event is deprecated
                return true;
            }
    
            public void EndCommand(int unused) 
            {
                // Use of this event is deprecated
            }
        }
    }
    

Configure Visual SourceSafe

To configure Visual SourceSafe do the following:

  1. Create a file named ssaddin.ini that contains the following statement:

    Microsoft.SourceSafe.EventSample = 1
    

    Double check that the ProgID in IvssEventSample.cs is the same as this statement in your ssaddin.ini file.

  2. Place this file into the same folder as ssapi.dll (usually \Microsoft Visual Studio\VSS\win32).

Test the Application

To test the application do the following:

  1. Build the VSSEvents DLL.

  2. Start Visual SourceSafe Explorer. While performing the operations that have corresponding event handlers in IvssEventSample.cs you should be able to observe message boxes pop up. For example, while checking out a file you will observe two dialogs. The first dialog will contain “BeforeCheckout” message, the second dialog – “AfterCheckout”.

See Also

Other Resources

Trapping SourceSafe Events
Driving a SourceSafe Database