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 7662543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:49:43+00:00 2026-05-31T13:49:43+00:00

I am trying to intercept event_object_create and event_object_destroy events through following code: class NameChangeTracker

  • 0

I am trying to intercept event_object_create and event_object_destroy events through following code:

class NameChangeTracker
{
private const uint EVENT_OBJECT_CREATE = 0x00008000;
private const uint EVENT_OBJECT_DESTROY = 0x00008001;
private const uint WINEVENT_OUTOFCONTEXT = 0;

//DLL imports

private static WinEventDelegate procDelegate = new WinEventDelegate(NameChangeTracker.WinEventProc);

private delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);

private static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
  if(idObject==0 && idChild==0)
  {
    if(hwnd.ToInt32() == getspotify().ToInt32())
    {
      switch(eventType)
      {
        case EVENT_OBJECT_CREATE:
        Console.WriteLine("inside create case"); 
        break;
        case EVENT_OBJECT_DESTROY:
        Console.WriteLine("inside destroy case"); 
        break;
      }
    }
  }

}

Inside main method

public static void Main()
{
   NameChangeTracker tracker = new NameChangeTracker();
   IntPtr hwnd = tracker.getspotify(); // returns hwnd using "FindWindow()" method.
   int num = tracker.getprocessid(hwnd); //returns processid using "GetWindowThreadProcessId()" method.

   IntPtr hWinEventHook = SetWinEventHook(0x00008000,0x00008001,IntPtr.Zero, procDelegate, 0, 0, 0);

   Message msg = new Message();
   while(GetMessage(ref msg,hwnd,0,0))

   UnhookWinEvent(hWinEventHook);
}
}

My above code captures the event when I manually close my application(Spotify) and prints a combination of inside create case and inside destroy case but when I restart my application it prints nothing in the console.

So, how do i make sure that my program continously listen for create & destroy events fired from Application(Spotify) and is the above mentioned approach correct approach.

Here is my ConsoleOutput.

EDIT-1

So, I changed idProcess paramater to listen to events from all process. But my program now only prints inside create case when I start my application it prints nothing when I manually close it.

  • 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-05-31T13:49:44+00:00Added an answer on May 31, 2026 at 1:49 pm

    What’s likely happening here is that when you restart the target app (Spotify), it gets a new Process ID – so your code, which is still listening to the old process ID – ignores it.

    You can’t change the PID you’re listening to events from, so you basically have two choices:

    • listen to events from all processes, all the time, or

    • listen to events from a specific process, and when it dies, listen to create events from all processes, checking to see if any if any of the Create events are for a top-level HWND that’s from the process you care about – and then listen to just that one. (Be careful, as there’s a potential race condition here; after you get the destroy event that tells you that the old instance has disappeared, a new instance could get created before you start listening to events from all processes, so you should do a sweep of all top-level HWNDs after you start listening to be on the safe side.)

    If this is just a utility for personal use, the simplest thing might be to always just listen to events from all processes – but still only listen to the few types of events you care about, and then see if there’s actually any performance issues. If not, and it runs fine without any apparent effect on system performance, you’re done!

    (For a more professional approach, you might want to measure just how many surplus messages you’re getting that you’re ignoring and otehrwise get more data to determine if it could potentially be a performance issue or not. Create events don’t happen all that often when the UI is in steady-state, so it may not be all that of an issue – but you can get a flurry of them when an app is opened.)

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

Sidebar

Related Questions

So I have the following code class UserDB { private: AccountInfo* _accounts[200] ; //
I'm trying to intercept events using Gdk.Window.AddFilter(Gdk.FilterFunc) in Mono. So far, I have been
I'm trying to intercept a method call (afterInsert() of a domain class) in a
Scenario: I'm trying to intercept paste events inside a textarea/input text, and filter the
I'm trying to intercept WindowsAuthenticationModule to login into application as another user, but I
i'm trying to intercept ajax requests with jquery, to display a waiting message like
I am trying to intercept form submits from webpages I dont control. My current
I'm trying to programmatically intercept and cancel a user's attempt to check out a
What I'm trying to achieve: Intercept requests for .asp files using an asp.net application
I'm trying to use jQuery to intercept control-A keypresses on my web page, like

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.