Helpful Visual Studio Macros

These macros were written by Joseph Styons and myself. I may have gotten one or two from Jared Scarbrough a few years ago as well. Any who, I use these on a daily basis and they can make developing a little quicker and fancier. (This is more than less an extension or part 2 of the blog here: here

The Common Macros:

Public Module Common
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwAccess As Long, ByVal fInherit As Integer, ByVal hObject As Long) As Long
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, ByVal lpExitCode As Long) As Long

    Sub AttachClient()
        Dim sHostName As String = Environ$("computername")

        Try
            Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
            Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
            Dim dbgeng(3) As EnvDTE80.Engine
            dbgeng(0) = trans.Engines.Item("T-SQL")
            dbgeng(1) = trans.Engines.Item("T-SQL")
            dbgeng(2) = trans.Engines.Item("Managed")
            Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, sHostName).Item("Blackbaud.AppFx.Browser.exe")
            proc2.Attach2(dbgeng)
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub AttachServer()
        Dim sHostName As String = Environ$("computername")

        Try
            Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
            Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
            Dim dbgeng(3) As EnvDTE80.Engine
            dbgeng(0) = trans.Engines.Item("T-SQL")
            dbgeng(1) = trans.Engines.Item("T-SQL")
            dbgeng(2) = trans.Engines.Item("Managed")
            Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, sHostName).Item("w3wp.exe")
            proc2.Attach2(dbgeng)
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub ClearIISCache()
        RunCommand("iisreset /stop")

        Dim windir As String = Environ("WINDIR")
        DeleteFolderAndEverythingInIt(windir + "\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\")
        DeleteFolderAndEverythingInIt(windir + "\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\")

        Dim userprofile As String = Environ$("USERPROFILE")
        DeleteFolderAndEverythingInIt(userprofile + "\Local Settings\Apps\")
        RunCommand("iisreset /start")
    End Sub

    Sub InsertGUID()
        DTE.ActiveDocument.Selection.Text = System.Guid.NewGuid().ToString("D").ToLower()
    End Sub

    Sub ReplaceAuthor()
        DTE.ExecuteCommand("Edit.Replace")
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.FindWhat = "Author=""BBNT\ChrisWh"""
        DTE.Find.ReplaceWith = "Author=""Blackbaud Professional Services"""
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = False
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            'Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close()
    End Sub

    Sub RestartSqlServer()
        Service("mssqlserver", "stop")
        Service("mssqlserver", "start")
    End Sub

    Sub ResetIIS()
        RunCommand("iisreset")
    End Sub

    Function Service(ByVal ServiceName As String, ByVal Action As String) As Boolean
        Service = RunCommand("net " & Action & " " & ServiceName & " /y")
    End Function

    Function RunCommand(ByVal cmdstring As String) As Boolean
        Dim ExitCode As Long, ProcHwnd As Long
        ExitCode = -1
        ProcHwnd = OpenProcess(&H400, False, Shell(cmdstring, vbHide, True))
        Do
            GetExitCodeProcess(ProcHwnd, ExitCode)
        Loop While ExitCode = &H103
        RunCommand = (ExitCode = 0)
    End Function

    Function DeleteFolderAndEverythingInIt(ByVal folderpath As String) As Boolean
        Try
            'make sure we have a trailing "\" on the path.
            'c:\myfolder becomes c:\myfolder\
            If folderpath.Substring(folderpath.Length - 1, 1) <> "\" Then
                folderpath = folderpath + "\"
            End If

            Dim filesystem, folder, file, fileList, folderList, subfolder
            filesystem = CreateObject("Scripting.FileSystemObject")
            folder = filesystem.GetFolder(folderpath)
            fileList = folder.Files
            For Each file In fileList
                filesystem.DeleteFile(folderpath & file.name)
            Next

            folderList = folder.SubFolders
            For Each subfolder In folderList
                subfolder.Delete()
            Next

            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Sub KillProcess(ByVal procname As String)
        RunCommand("taskkill /im " & procname & " /f")
    End Sub

    Sub SuperDuperResetAndRunWithDebuggingWithDelegate(ByVal myDelegate As [Delegate])
        GetStuffDone(myDelegate)

        MsgBox("attaching debugger")
        AttachServer()
    End Sub

    Sub SuperDuperResetAndRunWithDelegate(ByVal myDelegate As [Delegate])
        GetStuffDone(myDelegate)
    End Sub

    Private Sub GetStuffDone(ByVal myDelegate As [Delegate])
        'go away, bbec
        KillProcess("Blackbaud.Appfx.Browser.exe")

        ClearIISCache()

        RestartSqlServer()

        myDelegate.DynamicInvoke(Nothing)
    End Sub

    Sub LoadSpecLocal(ByVal loadspecPath As String, ByVal deployPath As String, ByVal server As String, ByVal database As String)
        If DTE.ActiveDocument.Saved = False Then DTE.ActiveDocument.Save()

        Dim loadspecArguments As String = "/s=" & server & " /db=" & database & " /spec=""" & DTE.ActiveDocument.FullName & """ /DIR=""" & deployPath & ""
        Dim fullLoadspecCommand As String = "/K """ & loadspecPath & " " & loadspecArguments & """"

        System.Diagnostics.Process.Start("cmd.exe", fullLoadspecCommand)
    End Sub

    Sub LaunchBBEC(ByVal url As String)
        System.Diagnostics.Process.Start("C:\Program Files\Internet Explorer\iexplore.exe", url)
    End Sub
End Module
As you can see you can have multiple modules setup for multiple projects as long as they use the Common module. Such as this one I wrote up for my work on UM:
Public Module UM
    Sub LaunchLocal()
        Common.LaunchBBEC("http://localhost/UM/browser/Blackbaud.AppFx.Browser.application")
    End Sub

    Sub LoadSpecLocal()
        Dim loadspecPath As String = "C:\BBEC\SDK\UM\SDK\Tools\LoadSpec.exe"
        Dim deployPath As String = "C:\BBEC\Instances\UM\bbappfx\vroot\bin\custom"
        Dim server As String = "(local)"
        Dim database As String = "UM"

        Common.LoadSpecLocal(loadspecPath, deployPath, server, database)
    End Sub

    Public Delegate Sub MyDelegate()

    Sub SuperDuperResetAndRunWithDebugging()
        Common.SuperDuperResetAndRunWithDebuggingWithDelegate(New MyDelegate(AddressOf LaunchLocal))
    End Sub

    Sub SuperDuperResetAndRun()
        Common.SuperDuperResetAndRunWithDelegate(New MyDelegate(AddressOf LaunchLocal))
    End Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s