Пример #1
0
 private static void SendStopMessageToProcess(int pid)
 {
     for (var ptr = WindowsNativeMethods.GetTopWindow(IntPtr.Zero); ptr != IntPtr.Zero; ptr = WindowsNativeMethods.GetWindow(ptr, 2))
     {
         uint num;
         WindowsNativeMethods.GetWindowThreadProcessId(ptr, out num);
         if (pid == num)
         {
             var hWnd = new HandleRef(null, ptr);
             WindowsNativeMethods.PostMessage(hWnd, 0x12, IntPtr.Zero, IntPtr.Zero);
             return;
         }
     }
 }
 private void SendStopMessageToProcess(int pid)
 {
     Logger.LogInformation($"Sending shutdown request to {pid}");
     for (var ptr = WindowsNativeMethods.GetTopWindow(IntPtr.Zero); ptr != IntPtr.Zero; ptr = WindowsNativeMethods.GetWindow(ptr, 2))
     {
         WindowsNativeMethods.GetWindowThreadProcessId(ptr, out var windowProcessId);
         if (pid == windowProcessId)
         {
             var hWnd = new HandleRef(null, ptr);
             if (!WindowsNativeMethods.PostMessage(hWnd, 0x12, IntPtr.Zero, IntPtr.Zero))
             {
                 throw new InvalidOperationException($"Unable to PostMessage to process {pid}. LastError: {Marshal.GetLastWin32Error()}");
             }
             return;
         }
     }
     throw new InvalidOperationException($"Unable to find main window for process {pid}");
 }
Пример #3
0
        private void SendStopMessageToProcess(int pid)
        {
            Logger.LogInformation($"Sending shutdown request to {pid}");
            var found = false;

            WindowsNativeMethods.EnumWindows((ptr, param) => {
                WindowsNativeMethods.GetWindowThreadProcessId(ptr, out var windowProcessId);
                if (pid == windowProcessId)
                {
                    // 256 is the max length
                    var className = new StringBuilder(256);

                    if (WindowsNativeMethods.GetClassName(ptr, className, className.Capacity) == 0)
                    {
                        throw new InvalidOperationException($"Unable to get window class name: {Marshal.GetLastWin32Error()}");
                    }

                    if (!string.Equals(className.ToString(), "IISEXPRESS", StringComparison.OrdinalIgnoreCase))
                    {
                        // skip windows without IISEXPRESS class
                        return(true);
                    }

                    var hWnd = new HandleRef(null, ptr);
                    if (!WindowsNativeMethods.PostMessage(hWnd, 0x12, IntPtr.Zero, IntPtr.Zero))
                    {
                        throw new InvalidOperationException($"Unable to PostMessage to process {pid}. LastError: {Marshal.GetLastWin32Error()}");
                    }

                    found = true;
                    return(false);
                }

                return(true);
            }, IntPtr.Zero);

            if (!found)
            {
                throw new InvalidOperationException($"Unable to find main window for process {pid}");
            }
        }
Пример #4
0
        private void SendStopMessageToProcess(int pid)
        {
            var found        = false;
            var extraLogging = false;
            var retryCount   = 5;

            while (!found && retryCount > 0)
            {
                Logger.LogInformation($"Sending shutdown request to {pid}");

                WindowsNativeMethods.EnumWindows((ptr, param) => {
                    WindowsNativeMethods.GetWindowThreadProcessId(ptr, out var windowProcessId);
                    if (extraLogging)
                    {
                        Logger.LogDebug($"EnumWindow returned {ptr} belonging to {windowProcessId}");
                    }

                    if (pid == windowProcessId)
                    {
                        // 256 is the max length
                        var className = new StringBuilder(256);

                        if (WindowsNativeMethods.GetClassName(ptr, className, className.Capacity) == 0)
                        {
                            throw new InvalidOperationException($"Unable to get window class name: {Marshal.GetLastWin32Error()}");
                        }

                        if (!string.Equals(className.ToString(), "IISEXPRESS", StringComparison.OrdinalIgnoreCase))
                        {
                            Logger.LogDebug($"Skipping window {ptr} with class name {className}");
                            // skip windows without IISEXPRESS class
                            return(true);
                        }

                        var hWnd = new HandleRef(null, ptr);
                        if (!WindowsNativeMethods.PostMessage(hWnd, 0x12, IntPtr.Zero, IntPtr.Zero))
                        {
                            throw new InvalidOperationException($"Unable to PostMessage to process {pid}. LastError: {Marshal.GetLastWin32Error()}");
                        }

                        found = true;
                        return(false);
                    }

                    return(true);
                }, IntPtr.Zero);

                if (!found)
                {
                    Thread.Sleep(100);
                }

                // Add extra logging if first try was unsuccessful
                extraLogging = true;
                retryCount--;
            }

            if (!found)
            {
                throw new InvalidOperationException($"Unable to find main window for process {pid}");
            }
        }