Create a gist now

Instantly share code, notes, and snippets.

Disable exceptions in Visual Studio programamtically using F#
// list of exceptions that will be disabled in the script.
// if an exception is not yet known to Visual Studio the exception will be added to VS's list of exceptions automatically.
let exceptionsToDisable = [|"Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException"
// references
#r @"EnvDTE.dll"
#r @"EnvDTE80.dll"
#r @"EnvDTE90.dll"
open EnvDTE
open EnvDTE80
open EnvDTE90
open System
open System.Diagnostics
open System.Runtime.InteropServices
open System.Runtime.InteropServices.ComTypes
In order to connect to the running VS-process its process-id must be known.
The following code determines that process-id.
The code was taken from
Using VisualBasic the process-id is not necessary because VB automatically has access to the currently running instance of VS.
Since F# runs in a separate process (a child-process of the running instance of VS) the process-id of the parent VS-process must be determined.
let getParentProcess (processId:int) =
let proc = Process.GetProcessById processId
let myProcID = new PerformanceCounter("Process", "ID Process", proc.ProcessName)
let myParentID = new PerformanceCounter("Process", "Creating Process ID", proc.ProcessName)
myParentID.NextValue() |> int
let currentProcess = Process.GetCurrentProcess().Id
let myVS = getParentProcess currentProcess
module Msdev =
extern int GetRunningObjectTable([<In>]int reserved, [<Out>] IRunningObjectTable& prot)
extern int CreateBindCtx([<In>]int reserved, [<Out>]IBindCtx& ppbc)
let tryFindInRunningObjectTable (name:string) =
//let result = new Dictionary<_,_>()
let mutable rot = null
if Msdev.GetRunningObjectTable(0,&rot) <> 0 then failwith "GetRunningObjectTable failed."
let mutable monikerEnumerator = null
let mutable numFetched = IntPtr.Zero
let monikers = Array.init<ComTypes.IMoniker> 1 (fun _ -> null)
let mutable result = None
while result.IsNone && (monikerEnumerator.Next(1, monikers, numFetched) = 0) do
let mutable ctx = null
if Msdev.CreateBindCtx(0, &ctx) <> 0 then failwith "CreateBindCtx failed"
let mutable runningObjectName = null
monikers.[0].GetDisplayName(ctx, null, &runningObjectName)
if runningObjectName = name then
let mutable runningObjectVal = null
if rot.GetObject( monikers.[0], &runningObjectVal) <> 0 then failwith "GetObject failed"
result <- Some runningObjectVal
//result.[runningObjectName] <- runningObjectVal
let getVS2010ROTName id =
sprintf "!VisualStudio.DTE.10.0:%i" id
The above code was all taken from someone else. Following some of my stuff.
Two functions are defined:
First, all exceptions will be enabled (which takes rather long - even when done via the GUI.
Then the exceptions from the list defined above will be disabled.
If an exception is not yet known to VS it will be made known to be able disable it.
let enableAll (excSettings:EnvDTE90.ExceptionSettings) =
for excSetting in excSettings do
excSettings.SetBreakWhenThrown(false, excSetting)
let disableSelected (excSettings:EnvDTE90.ExceptionSettings) exceptionsToDisable =
for exceptionToDisble in exceptionsToDisable do
excSettings.SetBreakWhenThrown(false, (excSettings.Item exceptionToDisble))
| :? System.Runtime.InteropServices.COMException ->
printfn "Error occurred disabling %s" exceptionToDisble
excSettings.SetBreakWhenThrown(false, excSettings.NewException(exceptionToDisble, 0u))
// Instead of SetBreakWhenThrown it is also possible to SetBreakWhenUserUnhandled
// ------------------------------------------------------------------------------------------------------
// Getting some objects from Visual Studio's object-model for automation...
let dte = (tryFindInRunningObjectTable (getVS2010ROTName myVS) |> Option.get) :?> DTE2
let dbg = dte.Debugger :?> EnvDTE90.Debugger3
let excSettings = dbg.ExceptionGroups.Item "Common Language Runtime Exceptions"
//enableAll excSettings; // commented out because it really takes long.
disableSelected excSettings exceptionsToDisable
printfn "Exceptions were disabled."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment