skip to Main Content

Application A (for example: AutoCAD, Word,…) when runs, it’ll call a DLL X at runtime to perform tasks → open form Login

Application B (for example: Photoshop, Excel…) when runs, it’ll call a DLL Y at runtime to perform tasks → I would like to close the above login form can do this?

Is that possible?

Image description

3

Answers


  1. well if its just about closing the running proccess the easiest way would probably be like something like

    using System.Diagnostics;
    var autocad = Process.GetProcesses().FirstOrDefault(w=>w.ProcessName == "AutoCAD");
    autocad.Close();
    

    for opening you could probably cycle the proccesses for the needed names. its not very efficent tough but it would get the job done if its just opening and closing you´re after

    Login or Signup to reply.
  2. You could keep checking for new processes with a (just an example) windowless console application. I have an example:

    1. Make a new project with a console application and the login form

    2. Make the Console application a windows application like this:

    enter image description here
    Press properties
    enter image description here
    Select Windows application

    Make a timer in the console application that checks for processes:

    public static bool IsOpen = false;
    
    static void Main(string[] args)
    {
        Timer t = new Timer(TimerCallback, null, 0, 1200);
        while ( true )
        {
            //keep it running
        }
    }
    
    private static void TimerCallback(Object o)
    {
        if ( !IsOpen && Process.GetProcesses().Select( r => r.ProcessName ).Contains( "EXCEL" ) )
        {
            //Excel is running, show the form
            Form1 loginForm = new Form1();
            loginForm.ShowDialog();
            //Stop it from spawning a million forms by setting bool
            IsOpen = true;
        }
    }
    

    The logic for closing a Form when an application closes can be put inside this timer too, use it the same way.

    To open a form from a console window however you need to add references to the console application:

    enter image description here

    If you want process names:
    Just use a form and add this:

    foreach ( var proc in Process.GetProcesses() )
    {
        MessageBox.Show( proc.ProcessName );
    }
    

    This will show you the names of all processes currently running.

    Login or Signup to reply.
  3. Here is a little sample that shows how to close a Window in another process. Create a new Windows Forms project, Add two Forms in it and add two buttons on the first one and put that code in Form1 :

    public partial class Form1 : Form
    {
        delegate bool EnumWindowsProc(IntPtr Hwnd, IntPtr lParam);
    
        [DllImport("user32", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private extern static bool EnumThreadWindows(int threadId, EnumWindowsProc callback, IntPtr lParam);
    
        [DllImport("user32", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
    
        [DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
        private extern static int GetWindowText(IntPtr hWnd, StringBuilder text, int maxCount);
    
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
    
        private const int WM_CLOSE = 0x10;
    
        public Form1()
        {
            InitializeComponent();
            button1.Click += button1_Click;
            button2.Click += button2_Click;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            var frm = new Form2();
            frm.Show(this);
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            Process[] processes = Process.GetProcessesByName("FindWindowSample");
    
            foreach (Process p in processes)
            {
                var handle = FindWindowInProcess(p, x => x == "Form2");
    
                if (handle != IntPtr.Zero)
                {
                    SendMessage(handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
                }
            }
        }
    
        public static IntPtr FindWindowInProcess(Process process, Func<string, bool> compareTitle)
        {
            IntPtr windowHandle = IntPtr.Zero;
    
            foreach (ProcessThread t in process.Threads)
            {
                windowHandle = FindWindowInThread(t.Id, compareTitle);
                if (windowHandle != IntPtr.Zero)
                {
                    break;
                }
            }
    
            return windowHandle;
        }
    
        private static IntPtr FindWindowInThread(int threadId, Func<string, bool> compareTitle)
        {
            IntPtr windowHandle = IntPtr.Zero;
            EnumThreadWindows(threadId, (hWnd, lParam) =>
            {
                StringBuilder text = new StringBuilder(200);
                GetWindowText(hWnd, text, 200);
                if (compareTitle(text.ToString()))
                {
                    windowHandle = hWnd;
                    return false;
                }
                return true;
            }, IntPtr.Zero);
    
            return windowHandle;
        }
    }
    

    Compile and then run two instances of the application. On the first one click on Button1 to show Form2. On the second instance click on Button2 to close Form2 from the first instance.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search