Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:20:33+00:00 2026-06-15T17:20:33+00:00

I want to receive an event in my C# application when a different C#

  • 0

I want to receive an event in my C# application when a different C# application (Form) is about to close.

How can I accomplish this? Can this be done with Reflections?

Edit: detailed description

I realize that my original question is not very specific.
I will try to describe my goal in more detail.

So I have 3 applications. Let’s name them Container, Placer and ToPlace.
I am developing Contaner and Placer in C#, Placer is a dll while Container is a WinForm. I have no access to the source code of ToPlace. In Container I have a custom control where I put in the main window of ToPlace with SetParent called from Placer. The goal would be to restore the parent window for the ToPlace before the Contaner app is closed, or to filter out the WM_DESTROY message (or other message) sent to the ToPlace main window. In the end the goal is not to destroy the main window of ToPlace when Container exits.

I tried to override the WndProc of the custom control in Container, but the message to the child window is not sent via the parent window.

I also tried to install a message filter on the Container app, but this was also not successfull.

My last try before writing this question was the SetWindowsHookEx, but after installing the hook successfully the hook procedure is never called. Maybe because, I read somewhere, that the hook function must be in a win32 dll, not in a managed one. The next try would be to use the SetWinEventHook, I read about this that it is easier to get it working from C#.

The code I tried with SetWindowsHookEx is below, maybe somebody who is more experienced in C# interop sees a bug and can get it working:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace AppMonitor
{
    public class AppMonitor
    {
        private const int WH_GETMESSAGE = 3;
        private const int HC_ACTION = 0;
        private const int PM_NOREMOVE = 0x0000;
        private const int PM_REMOVE = 0x0001;
        private const int WM_QUIT = 0x0012;

        [DllImport("user32.dll")]
        private static extern int SetWindowsHookEx(int idHook, GetMsgProcDelegate lpfn, int hMod, int dwThreadId);

        [DllImport("user32.dll")]
        private static extern bool UnhookWindowsHookEx(int hhk);

        [DllImport("user32.dll")]
        private unsafe static extern int CallNextHookEx(int hhk, int nCode, int wParam, void* lParam);

        [DllImport("user32.dll")]
        private static extern int GetWindowThreadProcessId(int hWnd, out int lpdwProcessId);

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

        private struct Msg {
            public int        hwnd;
            public int        message;
            public int      wParam;
            public int      lParam;
            public int       time;
            public int       pt;
        };

        [DllImport("kernel32.dll")]
        public static extern int LoadLibrary(string dllToLoad);

        public AppMonitor()
        { 
        }

        private int hHook;
        private int hMod;

        public event EventHandler AppClosing;

        private unsafe delegate int GetMsgProcDelegate(int code, int wParam, void* lParam);
        private unsafe GetMsgProcDelegate m_dlgt;

        private unsafe int GetMsgProc(int code, int wParam, void* lParam)
        {
            if (code != HC_ACTION || wParam != PM_REMOVE)
               return CallNextHookEx(this.hHook, code, wParam, lParam);

            Msg* msg = (Msg*)lParam;

            //if (msg.message == WM_QUIT)
            //    OnAppClosing(new EventArgs());

            return CallNextHookEx(this.hHook, code, wParam, lParam);
        }

        protected virtual void OnAppClosing(EventArgs e)
        {
            EventHandler h = AppClosing;
            if (h != null)
            {
                h(this, e);
            }
        }

        public unsafe bool setHook(int hWnd)
        {
            hMod = LoadLibrary("AppMonitor.dll"); //this dll
            int procId = 0;

            int threadId = GetWindowThreadProcessId(hWnd, out procId);
            if (threadId == 0)
                throw new System.Exception("Invalid thread Id");

            m_dlgt = GetMsgProc;
            this.hHook = SetWindowsHookEx(WH_GETMESSAGE, m_dlgt, hMod, threadId);

            if (this.hHook == 0)
                throw new System.Exception("Hook not successfull! Error code: " + GetLastError());

            return this.hHook != 0;
        }

        public bool unSetHook()
        {
            bool result = false;
            if (hHook != 0)
                result = UnhookWindowsHookEx(hHook);
            return result;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:20:34+00:00Added an answer on June 15, 2026 at 5:20 pm

    No, this can’t be done with reflection.

    You provide very little information to give a specific answer… for example:

    • IF you have both process “under development” (i.e. can change source code for both) then the solution might be easy
      Otherwise it gets complicated and potentially unreliable

    • IF both processes are on the same machine THEN it might be possible by hooking
      Otherwise you will need a much more complex implementation.

    • IF you just want to be informed that the form closes THEN that is possible
      IF you want to execute something in the context of the other process or even handle that event yourself that is very complicated and depending on what exactly you want it might be impossible…

    Please provide more details so that a specific answer is possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to raise an event from a .NET class, and receive this event
I have a form with: Check this if you Want to receive our newsletter:
I want to receive email to my email address through my asp.net application. Its
I want users to receive 'points' for completing various tasks in my application -
Is it possible to receive event/notification in my app when user uninstall my application
The application has a contextmenu opened and I want to close/dismiss it without using
I've got quiz application. Where robot ask different questions in chat, this questions belong
I created an iPhone application that I want to receive notifications at a custom
I'm working on an application in WCF and want to receive events in the
I have an class which should send/receive data in packet form. This class contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.