Click here to Skip to main content
15,888,293 members
Home / Discussions / C#
   

C#

 
AnswerRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 8:26
mveOriginalGriff26-Jan-16 8:26 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 8:32
professionalKevin Marois26-Jan-16 8:32 
GeneralRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 8:41
mveOriginalGriff26-Jan-16 8:41 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 8:38
professionalKevin Marois26-Jan-16 8:38 
GeneralRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 8:41
mveOriginalGriff26-Jan-16 8:41 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 8:45
professionalKevin Marois26-Jan-16 8:45 
GeneralRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 8:57
mveOriginalGriff26-Jan-16 8:57 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 9:01
professionalKevin Marois26-Jan-16 9:01 
Works great! Here's the entire class. I'm going to add to it to accept a list of processes to attach, then make it a VS addin that I can launch from a toolbar.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using EnvDTE;

namespace MyApp
{
    public static class IISDebugAttach
    {
        public static void AttachToIIS()
        {
            // Register the IOleMessageFilter to handle any threading errors.
            MessageFilter.Register();

            DTE dte = (DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.14.0");

            var listProcess = new List<string>();
            listProcess.Add("iisexpress");

            var processes = dte.Debugger.LocalProcesses;

            try
            {
                foreach (Process process in processes)
                {
                    foreach (var processName in listProcess)
                    {
                        if (!string.IsNullOrEmpty(process.Name))
                        {
                            string procName = process.Name;

                            if (procName.ToLower().IndexOf(processName, StringComparison.Ordinal) != -1)
                            {
                                process.Attach();
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                int x = 1;
            }

            // Clear thread issue filter
            MessageFilter.Revoke();
        }

        public class MessageFilter : IOleMessageFilter
        {
            //
            // Class containing the IOleMessageFilter
            // thread error-handling functions.

            // Start the filter.
            public static void Register()
            {
                IOleMessageFilter newFilter = new MessageFilter();
                IOleMessageFilter oldFilter = null;
                CoRegisterMessageFilter(newFilter, out oldFilter);
            }

            // Done with the filter, close it.
            public static void Revoke()
            {
                IOleMessageFilter oldFilter = null;
                CoRegisterMessageFilter(null, out oldFilter);
            }

            //
            // IOleMessageFilter functions.
            // Handle incoming thread requests.
            int IOleMessageFilter.HandleInComingCall(int dwCallType,
              System.IntPtr hTaskCaller, int dwTickCount, System.IntPtr
              lpInterfaceInfo)
            {
                //Return the flag SERVERCALL_ISHANDLED.
                return 0;
            }

            // Thread call was rejected, so try again.
            int IOleMessageFilter.RetryRejectedCall(System.IntPtr
              hTaskCallee, int dwTickCount, int dwRejectType)
            {
                if (dwRejectType == 2)
                // flag = SERVERCALL_RETRYLATER.
                {
                    // Retry the thread call immediately if return >=0 & 
                    // <100.
                    return 99;
                }
                // Too busy; cancel call.
                return -1;
            }

            int IOleMessageFilter.MessagePending(System.IntPtr hTaskCallee,
              int dwTickCount, int dwPendingType)
            {
                //Return the flag PENDINGMSG_WAITDEFPROCESS.
                return 2;
            }

            // Implement the IOleMessageFilter interface.
            [DllImport("Ole32.dll")]
            private static extern int
              CoRegisterMessageFilter(IOleMessageFilter newFilter, out
              IOleMessageFilter oldFilter);
        }

        [ComImport(), Guid("00000016-0000-0000-C000-000000000046"),
        InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
        interface IOleMessageFilter
        {
            [PreserveSig]
            int HandleInComingCall(
                int dwCallType,
                IntPtr hTaskCaller,
                int dwTickCount,
                IntPtr lpInterfaceInfo);

            [PreserveSig]
            int RetryRejectedCall(
                IntPtr hTaskCallee,
                int dwTickCount,
                int dwRejectType);

            [PreserveSig]
            int MessagePending(
                IntPtr hTaskCallee,
                int dwTickCount,
                int dwPendingType);
        }

    }
}
If it's not broken, fix it until it is

GeneralRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 9:16
mveOriginalGriff26-Jan-16 9:16 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 9:19
professionalKevin Marois26-Jan-16 9:19 
GeneralRe: Programmatically Attach Debugger Pin
John Torjo26-Jan-16 20:54
professionalJohn Torjo26-Jan-16 20:54 
Questionpost and manipulate result JSON WebService Pin
Member 1228688325-Jan-16 12:58
Member 1228688325-Jan-16 12:58 
AnswerRe: post and manipulate result JSON WebService Pin
Garth J Lancaster25-Jan-16 13:33
professionalGarth J Lancaster25-Jan-16 13:33 
GeneralRe: post and manipulate result JSON WebService Pin
Member 1228688326-Jan-16 14:22
Member 1228688326-Jan-16 14:22 
GeneralRe: post and manipulate result JSON WebService Pin
Garth J Lancaster26-Jan-16 14:31
professionalGarth J Lancaster26-Jan-16 14:31 
QuestionRe-Assignable Keyboard Shortcuts Pin
Eiredrake25-Jan-16 11:05
Eiredrake25-Jan-16 11:05 
AnswerRe: Re-Assignable Keyboard Shortcuts Pin
John Torjo25-Jan-16 23:20
professionalJohn Torjo25-Jan-16 23:20 
AnswerRe: Re-Assignable Keyboard Shortcuts Pin
Pete O'Hanlon26-Jan-16 0:09
mvePete O'Hanlon26-Jan-16 0:09 
GeneralRe: Re-Assignable Keyboard Shortcuts Pin
Richard MacCutchan26-Jan-16 0:28
mveRichard MacCutchan26-Jan-16 0:28 
GeneralRe: Re-Assignable Keyboard Shortcuts Pin
Pete O'Hanlon26-Jan-16 0:52
mvePete O'Hanlon26-Jan-16 0:52 
QuestionData disappears on postback Pin
Carl Cummings25-Jan-16 7:21
Carl Cummings25-Jan-16 7:21 
AnswerRe: Data disappears on postback Pin
Richard Deeming25-Jan-16 8:51
mveRichard Deeming25-Jan-16 8:51 
QuestionHow to use WeakEventManager with reflection Pin
Kenneth Haugland23-Jan-16 23:21
mvaKenneth Haugland23-Jan-16 23:21 
SuggestionRe: How to use WeakEventManager with reflection Pin
Kornfeld Eliyahu Peter24-Jan-16 1:18
professionalKornfeld Eliyahu Peter24-Jan-16 1:18 
GeneralRe: How to use WeakEventManager with reflection Pin
Sascha Lefèvre24-Jan-16 1:34
professionalSascha Lefèvre24-Jan-16 1:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.