Karen Tazayan
Karen Tazayan

Reputation: 136

Is it possible to set a title for a .NET Core host process (dotnet.exe) in console app?

I'm trying to set it via command argument – --title="Host1" but it doesn't work. As you can see it is possible for ASP.NET Core application:

enter image description here

Any other options? I have a lot of console processes on my machine and want to assign friendly names to them.

Upvotes: 3

Views: 380

Answers (1)

LOKI
LOKI

Reputation: 312

Add this class in your project

#if DEBUG
    delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
    public class DebuggerForm
    {
        const UInt32 WS_EX_NOACTIVATE = 0x08000000;
        const UInt32 WS_MINIMIZE = 0x20000000;
        const int SW_SHOWMINNOACTIVE = 7;
        const UInt32 WM_DESTROY = 2;

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        struct WNDCLASSEX
        {
            [MarshalAs(UnmanagedType.U4)]
            public int cbSize;
            [MarshalAs(UnmanagedType.U4)]
            public int style;
            public IntPtr lpfnWndProc;
            public int cbClsExtra;
            public int cbWndExtra;
            public IntPtr hInstance;
            public IntPtr hIcon;
            public IntPtr hCursor;
            public IntPtr hbrBackground;
            public string lpszMenuName;
            public string lpszClassName;
            public IntPtr hIconSm;
        }

        private WndProc delegWndProc = myWndProc;

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        static extern bool DestroyWindow(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true, EntryPoint = "CreateWindowEx")]
        public static extern IntPtr CreateWindowEx(UInt32 dwExStyle, UInt16 lpClassName, string lpWindowName, UInt32 dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

        [DllImport("user32.dll", SetLastError = true, EntryPoint = "RegisterClassEx")]
        static extern System.UInt16 RegisterClassEx([In] ref WNDCLASSEX lpWndClass);

        [DllImport("kernel32.dll")]
        static extern uint GetLastError();

        [DllImport("user32.dll")]
        static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);

        internal bool Create(string name)
        {
            WNDCLASSEX windowClass = new WNDCLASSEX();
            windowClass.cbSize = Marshal.SizeOf(typeof(WNDCLASSEX));
            windowClass.style = 0;
            windowClass.hbrBackground = IntPtr.Zero;
            windowClass.cbClsExtra = 0;
            windowClass.cbWndExtra = 0;
            windowClass.hInstance = Marshal.GetHINSTANCE(this.GetType().Module);
            windowClass.hIcon = IntPtr.Zero;
            windowClass.hCursor = IntPtr.Zero;
            windowClass.lpszMenuName = null;
            windowClass.lpszClassName = "DebuggerForm";
            windowClass.lpfnWndProc = Marshal.GetFunctionPointerForDelegate(delegWndProc);
            windowClass.hIconSm = IntPtr.Zero;
            ushort regResult = RegisterClassEx(ref windowClass);

            if (regResult == 0)
            {
                uint error = GetLastError();
                return false;
            }
            string wndClass = windowClass.lpszClassName;

            IntPtr hWnd = CreateWindowEx(WS_EX_NOACTIVATE, regResult, name, WS_MINIMIZE, 0, 0, 200, 200, IntPtr.Zero, IntPtr.Zero, windowClass.hInstance, IntPtr.Zero);

            if (hWnd == ((IntPtr)0))
            {
                uint error = GetLastError();
                return false;
            }
            ShowWindow(hWnd, SW_SHOWMINNOACTIVE);

            return true;
        }

        private static IntPtr myWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            switch (msg)
            {
                case WM_DESTROY:
                    DestroyWindow(hWnd);
                    break;

                default:
                    break;
            }
            return DefWindowProc(hWnd, msg, wParam, lParam);
        }
    }
#endif

And in Startup.cs in the constructor add new DebuggerForm().Create("WebApp");

this will show the processs title as WebApp.

Upvotes: 1

Related Questions